随着JDK8使用的普及,现在大家对Stream流的一些常见API及写法也越来越熟悉了。
其中很多API写的时候感觉很繁琐,想要更快捷一些,这时候工具类就出来了,对一些常见写法的进一步封装,简化代码量,真的谁用谁爽。
我今天就是想把这个好用的工具类推给你们的,收了我这波安利吧!!!
1. 对Collectors.groupingBy函数逐步增加参数。
List<Student> list = Arrays.asList(
new Student("张三", 18, "男"),
new Student("李四", 28, "男"),
new Student("张三", 18, "女")
);
Map<String, List<Student>> map1 = list.stream().collect(Collectors.groupingBy(Student::getName));
Map<String, Map<String, List<Student>>> map2 = list.stream().collect(
Collectors.groupingBy(Student::getName,
Collectors.groupingBy(Student::getSex, Collectors.toList()))
);
Map<String, Map<String, List<Integer>>> map3 = list.stream(