本篇博客来自hutool
Java8中Collectors详解
介绍
Java8中的新特性之一就是Stream,Hutool针对常用操作做了一些封装,本文将stream中collection和hutool工具类做了对比,同样的功能封装后是多么的简洁,之前本人都是手写工具,现在知道hutool还有这种功能,这用起来简直绝了
使用
Student
@Data
@AllArgsConstructor
@ToString
public class Student {
private long termId;
private long classId;
private long studentId;
private String name;
}
一、集合转Map
1. 建立一个学生id和学生对象之间的map:
@Test
void toIdentityMapTest() {
List<Student> list = new ArrayList<>();
list.add(new Student(1, 1, 1, "张三"));
list.add(new Student(1, 1, 2, "李四"));
list.add(new Student(1, 1, 3, "王五"));
Map<Long, Student> collect = list.stream().collect(Collectors.toMap(Student::getStudentId, s -> s));
Map<Long, Student> map = CollStreamUtil.toIdentityMap(list, Student::getStudentId);
}
结果 手写方法 hutool工具
2.自定义Map的key和value放的内容
如我们可以将学生信息的id和姓名生成map:
@Test
void toMapTest() {
List<Student> list = new ArrayList<>();
list.add(new Student(1, 1, 1, "张三"));
list.add(new Student(1, 1, 2, "李四"));
list.add(new Student(1, 1, 3, "王五"));
Map<Long, String> collect = list.stream()
.collect(Collectors.toMap(student -> student.getStudentId(),
Student::getName, (s1, s2) -> s2));
Map<Long, String> map2 = CollStreamUtil.toMap(list, Student::getStudentId, Student::getName);
}
结果 手写方法 工具
二、分组
1. 按照班级分组
@Test
void groupByKeyTest() {
List<Student> list = new ArrayList<>();
list.add(new Student(1, 1, 1, "张三"));
list.add(new Student(1, 2, 2, "李四"));
list.add(new Student(2, 1, 1, "擎天柱"));
list.add(new Student(2, 2, 2, "威震天"));
list.add(new Student(2, 3, 2, "霸天虎"));
Map<Long, List<Student>> collect = list.stream().collect(Collectors.groupingBy(Student::getClassId));
Map<Long, List<Student>> map = CollStreamUtil.groupByKey(list, Student::getClassId);
System.out.println("map = " + map);
}
结果 手写方法 使用工具
2、将collection按照两个规则(比如有相同的年级id,班级id)分组成双层map(集合map)
第二层value是集合,将符合条件的数据放入集合中
@Test
void groupBy2Key() {
List<Student> list = new ArrayList<>();
list.add(new Student(1, 1,1, 1, "张三"));
list.add(new Student(1, 1,1, 2, "李四"));
list.add(new Student(1, 2,1, 3, "王五"));
list.add(new Student(1, 2,1, 3, "王流"));
list.add(new Student(1, 2, 2, 3, "汪一"));
list.add(new Student(1, 2, 2, 3, "汪二"));
Map<Long, Map<Long, List<Student>>> longMapMap = CollStreamUtil.groupBy2Key(list, Student::getGradeId, Student::getClassId);
}
结果
3、将collection按照两个规则(比如有相同的年级id,班级id)分组成双层map(单对象map)
@Test
void group2Map() {
List<Student> list = new ArrayList<>();
list.add(new Student(1, 1, 1, 1, "张三"));
list.add(new Student(1, 1, 1, 2, "李四"));
list.add(new Student(1, 2, 1, 3, "王五"));
list.add(new Student(1, 2, 1, 3, "王流"));
list.add(new Student(1, 2, 2, 3, "汪一"));
list.add(new Student(1, 2, 2, 3, "汪二"));
Map<Long, Map<Long, Student>> longMapMap1 = CollStreamUtil.group2Map(list, Student::getGradeId, Student::getClassId);
}
结果 4.作为所有groupingBy的公共方法,更接近于原生,灵活性更强
@Test
void groupBy() {
List<Student> list = new ArrayList<>();
list.add(new Student(1, 1, 1, "张三"));
list.add(new Student(1, 1, 2, "李四"));
list.add(new Student(1, 2, 3, "王五"));
Map<Long, Long> collect = list.stream()
.collect(Collectors.groupingBy(Student::getClassId, Collectors.counting()));
Map<Long, Long> longLongMap = CollStreamUtil.groupBy(list, Student::getClassId, Collectors.counting());
System.out.println("longMapMap = " + longLongMap);
}
结果
三、转换提取
我们可以将学生信息列表转换提取为姓名的列表:
@Test
void toList() {
List<Student> list = new ArrayList<>();
list.add(new Student(1, 1, 1, "张三"));
list.add(new Student(1, 1, 2, "李四"));
list.add(new Student(1, 1, 3, "王五"));
List<String> list2 = CollStreamUtil.toList(list, Student::getName);
}
结果
|