import org.junit.Test;
import java.text.Collator;
import java.util.*;
import java.util.stream.Collectors;
public class Stream {
/**
* 中间操作
*/
@Test
public void filter(){
List list = Arrays.asList("米哈游a","阿里a","未知c","华为d","天美a","京东d","饿了么s","网易s","腾讯a","百度a");
//1,创建流 ①创建普通流stream(); ②创建并行流 parallelStream();
//2,中间操作: filter()设置过滤条件 str是集合里的每一个元素
//3,最终操作:collect()
// 将含有字母a的元素返回组成一个新的集合result
List result = list.stream().filter(str -> str.contains("a")).collect(Collectors.toList());
System.out.println(result); // [米哈游a, 阿里a, 天美a, 腾讯a, 百度a]
}
@Test
public void distinct1(){
List list = Arrays.asList("米哈游a","米哈游a","米哈游a","阿里a","未知c","未知c","华为d","天美a","京东d","饿了么s","网易s","腾讯a","百度a");
//distinct()去重
List result = list.stream().distinct().collect(Collectors.toList());
System.out.println(result); //[米哈游a, 阿里a, 未知c, 华为d, 天美a, 京东d, 饿了么s, 网易s, 腾讯a, 百度a]
}
@Test
public void distinct2(){
//如果集合是对象,distinct()操作的地址引用,只要地址不同那么对象都是不同的
List users = new ArrayList<>();
users.add(new User("a","33"));
users.add(new User("a","33"));
users.add(new User("a","33"));
users.add(new User("a","33"));
List resUser = users.stream().distinct().collect(Collectors.toList());
//[User{name='a', age='33'}, User{name='a', age='33'}, User{name='a', age='33'}, User{name='a', age='33'}]
System.out.println(resUser);
}
@Test
public void limit(){
//limit()从集合的前面开始拿n个元素
List prices = Arrays.asList("13RMB","free","37RMB","free","8RMB","free","8RMB","20RMB","free","2RMB");
List collect = prices.stream().limit(4).collect(Collectors.toList());
System.out.println(collect); //[13RMB, free, 37RMB, free]
}
@Test
public void skip(){
//skip()返回扔掉前n个元素后的集合
List prices = Arrays.asList("13RMB","free","37RMB","free","8RMB","free","8RMB","20RMB","free","2RMB");
List collect = prices.stream().skip(5).collect(Collectors.toList());
System.out.println(collect); //[free, 8RMB, 20RMB, free, 2RMB]
}
@Test
public void map(){
//map() 对流中所有数据做统一处理
List prices = Arrays.asList("13RMB","free","37RMB","free","8RMB","free","8RMB","20RMB","free","2RMB");
List result = prices.stream().map(str ->"¥"+str).collect(Collectors.toList());
//[¥13RMB, ¥free, ¥37RMB, ¥free, ¥8RMB, ¥free, ¥8RMB, ¥20RMB, ¥free, ¥2RMB]
System.out.println(result);
}
@Test
public void flatMap(){
//flatMap()扁平化操作
}
@Test
public void sorted(){
//sorted()将流排序后输出 默认从升序
// 数字排序
List ins = Arrays.asList(234,34,25,45,26,-235,-56,232,67,2,45,134,0,453,23);
List collect = ins.stream().sorted().collect(Collectors.toList());
//[-235, -56, 0, 2, 23, 25, 26, 34, 45, 45, 67, 134, 232, 234, 453]
System.out.println(collect);
// 字母排序
List strs = Arrays.asList("as","ad","gea","jws","afr","cvw","te","yue");
List collect1 = strs.stream().sorted().collect(Collectors.toList());
// [ad, afr, as, cvw, gea, jws, te, yue]
System.out.println(collect1);
//汉字排序 传入一个比较器
List characters = Arrays.asList("张","杨","找","哈","啊","你","哦","都","回");
List collect2 = characters.stream().sorted(Collator.getInstance(Locale.CHINA)).collect(Collectors.toList());
// [啊, 都, 哈, 回, 你, 哦, 杨, 张, 找]
System.out.println(collect2);
}
/**
* 终止操作
*/
@Test
public void anyMatch(){
//anyMatch()集合中是否有至少一个元素满足条件 返回boolean
List prices = Arrays.asList("13RMB","free","37RMB","free","8RMB","free","8RMB","20RMB","free","2RMB");
boolean isFree = prices.stream().anyMatch(str -> str.contains("free"));
System.out.println(isFree); //true
}
@Test
public void allMatch(){
//allMatch()元素是否都满足条件
List prices = Arrays.asList("13RMB","free","37RMB","free","8RMB","free","8RMB","20RMB","free","2RMB");
boolean b = prices.stream().allMatch(str -> str.length() > 3);
System.out.println(b); //true
}
@Test
public void noneMatch(){
//noneMatch() 集合所有的元素都不满足条件
List prices = Arrays.asList("13RMB","free","37RMB","free","8RMB","free","8RMB","20RMB","free","2RMB");
boolean b = prices.stream().noneMatch(str -> str.length() > 13);
System.out.println(b); //true
}
@Test
public void findAny(){
//findAny()返回集合中的任意元素 如何使用的是stream()串行流则总是拿到第一个,并行流则会是随机的
List prices = Arrays.asList("13RMB","free","37RMB","free","8RMB","free","8RMB","20RMB","free","2RMB");
Optional any = prices.stream().findAny();
if (any.isPresent()){
System.out.println(any.get()); //13RMB
}
Optional any1 = prices.parallelStream().findAny();
System.out.println(any1.get()); //8RMB
}
@Test
public void findFirst(){
List prices = Arrays.asList("13RMB","free","37RMB","free","8RMB","free","8RMB","20RMB","free","2RMB");
Optional any = prices.stream().findFirst();
System.out.println(any); //Optional[13RMB]
}
@Test
public void forEach(){
//forEach()遍历集合
List list = Arrays.asList("2022-6-1","2022-3-20","2022-7-12","2022-4-26","2022-5-9","2022-5-28","2022-10-3");
//传统
list.forEach(System.out::println);
//流
list.stream().forEach(System.out::println);
}
@Test
public void collect(){
//collect()收集器
List list = Arrays.asList("2022-6-1","2022-3-20","2022-3-20","2022-4-26","2022-4-26","2022-5-28","2022-10-3");
//收集为Set集合
Set collect = list.stream().collect(Collectors.toSet());
//[2022-6-1, 2022-10-3, 2022-3-20, 2022-4-26, 2022-5-28]
System.out.println(collect);
//收集为Map集合
//参数说明: k-> k使用key v->v 使用的value (oldV, newV) -> newV当值重复的时候用新值贴换
Map collect1 = list.stream().collect(Collectors.toMap(k -> k, v -> v, (oldV, newV) -> newV));
//{2022-10-3=2022-10-3,
// 2022-6-1=2022-6-1,
// 2022-3-20=2022-3-20,
// 2022-4-26=2022-4-26,
// 2022-5-28=2022-5-28}
System.out.println(collect1);
}
@Test
public void reduce(){
//reduce()可以将元素中的值反复结合起来得到一个结果
List characters = Arrays.asList("张","杨","找","哈","啊","你","哦","都","回");
/*
(accumulator, item) -> { //第一个参数是计算结果,第二个参数是集合中的每个元素
return accumulator+item;
}
*/
Optional reduce = characters.stream().reduce((accumulator, item) -> {
return accumulator+item;
});
System.out.println(reduce.get()); //张杨找哈啊你哦都回
}
@Test
public void count(){
//count()获取元素中的个数
List characters = Arrays.asList("张","杨","找","哈","啊","你","哦","都","回");
long count = characters.stream().count();
System.out.println(count); //9
}
}
emm,因为最近半年一直在忙于学习各种技术,没时间写博客,最近在自己准备写一个权限框架,想着复习一下基础,故此今日写了点小demo,希望大家以后学以致用!!