1.集合过滤
Lists.newArrayList(1,2,3).stream().filter(k -> k >1) ;
2.遍历
Lists.newArrayList(1,2,3).stream().forEach(System.out::println);
3.映射
Lists.newArrayList(1,2,3).stream().map(k -> k + "haha") ;
4.排序
Lists.newArrayList(1,2,3).stream().sorted(Comparator.comparing(Integer::intValue).reversed()) ;
5.reduce
//eg1
Integer integer1 = Lists.newArrayList(-1, 2, 3).stream()
.reduce(0,//x的初始值
(x, k) -> x = k * 100,//k是函数的参数,x是变量
(x1, x2) -> x1 + x2//函数结果
);
//eg2
Integer integer2 = Lists.newArrayList(-1, 2, 3).stream()
.reduce((x1, x2) -> (x1 + x2) * 100)//
.orElse(0);
6.收集list
Lists.newArrayList(-1, 2, 3).stream().collect(Collectors.toList()) ;
7.收集器Set
Lists.newArrayList(-1, 2, 3).stream().collect(Collectors.toSet()) ;
8.收集器map
//1对多聚合map
Map<String, List<User>> userMap1 = users.stream()
.collect(Collectors.groupingBy(User::getAddress, Collectors.toList()));
//1对1聚合map,最后一个方法指的是重复值处理策略
Map<String, User> userMap2 = users.stream()
.collect(Collectors.toMap(u -> u.getAddress(), u -> u, (u1, u2) -> u2));
9.字符串拼接
String str = new ArrayList<String>().stream().collect(Collectors.joining("-"));
10.并行处理
strings.stream().parallel().forEach(System.out::println);