Stream 流中 Collectors.toMap 的用法
一、Collectors.toMap 方法使用技巧
Collectors.toMap() 方法是把 List 转 Map 的操作
二、代码案例
1、demo案例
- public static void main(String[] args) {
- List<Student> list = Arrays.asList(
- new Student(1, "张三", 20, "29.8"),
- new Student(2, "李四", 25, "29.5"),
- new Student(3, "赵武", 23, "30.8"),
- new Student(4, "王六", 22, "31.8")
- );
-
- list 打印输出为:[
- Student(id=1, name=张三, age=20, score=29.8),
- Student(id=2, name=李四, age=25, score=29.5),
- Student(id=3, name=赵武, age=23, score=30.8),
- Student(id=4, name=王六, age=22, score=31.8)
- ]
-
-
/**
* id 作为 map 的key,name 作为 value
* 结果集: {1=张三, 2=李四, 3=赵武, 4=王六}
*/- Map<Integer, String> collect = list.stream()
- .collect(Collectors.toMap(Student::getId, Student::getName));
- System.out.println(collect);
-
-
/**
* id 作为 map 的 key,Student 对象作为 map 的 value
* 结果集: {1=Student(id=1, name=张三, age=20, score=29.8),
2=Student(id=2, name=李四, age=25, score=29.5),
3=Student(id=3, name=赵武, age=23, score=30.8),
4=Student(id=4, name=王六, age=22, score=31.8)}
*/- Map<Integer, Student> collect1 = list.stream()
- .collect(Collectors.toMap(Student::getId, v -> v));
- System.out.println(collect1);
-
-
/**
* id 作为 map 的 key,Student 对象作为 map 的 value
* 结果集: {1=Student(id=1, name=张三, age=20, score=29.8),
2=Student(id=2, name=李四, age=25, score=29.5),
3=Student(id=3, name=赵武, age=23, score=30.8),
4=Student(id=4, name=王六, age=22, score=31.8)}
*/- Map<Integer, Student> collect2 = list.stream()
- .collect(Collectors.toMap(Student::getId, Function.identity()));
- System.out.println(collect2);
- }
1.1、如果不是对象,是数组,怎么转成map?
- String typeBanner = "A=1,B=2,C=3";
- String[] typeBannerArray = typeBanner.split(",");
- System.out.println(Arrays.toString(typeBannerArray)); // [A=1, B=2, C=3]
- Map<String, String> typeBannerMap = Arrays.stream(typeBannerArray).collect(Collectors.toMap(
- (array) -> array.split("=")[0],
- (array) -> array.split("=")[1]
- ));
- System.out.println(typeBannerMap); // {A=1, B=2, C=3}
2、当 map 上述 id 如果重复,会报主键重复异常,解决办法:
按照规范来写的话,最好所有toMap,都要将这个异常提前考虑进去,不然有时候会报重复主键异常,这也是正例的写法,上面的属于反例的写法。
2.1、Collectors.toMap 有三个重载方法:
- toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper);
- toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper,
- BinaryOperator<U> mergeFunction);
- toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper,
- BinaryOperator<U> mergeFunction, Supplier<M> mapSupplier);
参数解释:
1. keyMapper:Key 的映射函数,Student:getId 表示选择 Student 的 getId 作为 map 的 key 值。
2. valueMapper:Value的映射函数,Function.identity() 表示选择将原来的对象作为 Map 的value 值。
3. mergeFunction:当 Key 冲突时,调用的合并方法。(n1,n2)->n1 中,如果 n1 与 n2 的 key 值相同,选择 n1 作为那个 key 所对应的 value 值。
4. mapSupplier:Map 构造器,在需要返回特定的 Map 时使用。第四个参数 mapSupplier 用于返回一个任意类型的 Map 实例,比如我们希望返回的 Map 是根据 Key 排序的。TreeMap::new
- public static void main(String[] args) {
- List<Student> list = Arrays.asList(
- new Student(1, "张三", 20, "29.8"),
- new Student(2, "李四", 25, "29.5"),
- new Student(1, "赵武", 23, "30.8"),
- new Student(4, "王六", 22, "31.8")
- );
- /**
- * id 作为 map 的key,重复 id 的 name 合并作为 value
- * 结果集: {1=张三,赵武, 2=李四, 4=王六}
- */
- Map<Integer, String> collect = list.stream()
- .collect(Collectors.toMap(Student::getId, Student::getName, (n1, n2) -> n1 +","+ n2));
- System.out.println(collect);
-
- /**
- * 取前面一个 Student 对象
- * 结果集: {1=Student(id=1, name=张三, age=20, score=29.8),
- 2=Student(id=2, name=李四, age=25, score=29.5),
- 4=Student(id=4, name=王六, age=22, score=31.8)}
- */
- Map<Integer, Student> collect1 = list.stream()
- .collect(Collectors.toMap(Student::getId, Function.identity(), (n1, n2) -> n1));
- System.out.println(collect1);
-
- /**
- * 取后面一个 Student 对象
- * 结果集: {1=Student(id=1, name=赵武, age=23, score=30.8),
- 2=Student(id=2, name=李四, age=25, score=29.5),
- 4=Student(id=4, name=王六, age=22, score=31.8)}
- */
- Map<Integer, Student> collect2 = list.stream()
- .collect(Collectors.toMap(Student::getId, Function.identity(), (n1, n2) -> n2, TreeMap::new));
- System.out.println(collect2);
- }
写案例遇到的问题有,上述第一个输出,如果写成 n1 + n2 ,map 第二个参数类型是对象或者是list集合,都是显示编译报错状态。