一、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);
参数含义分别是:
keyMapper:Key 的映射函数
valueMapper:Value 的映射函数
mergeFunction:当 Key 冲突时,调用的合并方法
mapSupplier:Map 构造器,在需要返回特定的 Map 时使用
业务场景最多的还是map的键为一个唯一的标识符,值为对象本身!
二、例子使用:
Person:
查看代码
package com.mayikt.entity;
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
测试代码:
package com.mayikt.stream;
import com.mayikt.entity.Person;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* 1.参考博客:https://blog.csdn.net/liuerchong/article/details/120279530
*/
public class TestCollectorsToMap {
public static void main(String[] args) {
List<Person> persons = new ArrayList<>();
persons.add(new Person("zhangsan", 15));
persons.add(new Person("lisi", 20));
persons.add(new Person("wangwu", 30));
persons.add(new Person("wangmazi", 40));
Map<String, Integer> personMap = persons.stream().collect(Collectors.toMap(Person::getName, Person::getAge));
System.out.println(personMap);
Map<String, Person> personMap2 = persons.stream().collect(Collectors.toMap(Person::getName, person -> person));
System.out.println(personMap2);
//当persons中有key重复时:会报错:Duplicate key Person{name='lisi', age=20}
persons.add(new Person("lisi", 21));
try {
Map<String, Person> personMap3 = persons.stream().collect(Collectors.toMap(Person::getName, person -> person));
}catch (Exception e) {
e.printStackTrace();
}
//重复的键中,取后者的值
Map<String, Person> personMap4 = persons.stream().collect(Collectors.toMap(Person::getName, person -> person, (p1, p2) -> p2));
System.out.println(personMap4);
//你也可以把异常抛出来
Map<String, Person> personMap5 = persons.stream().collect(Collectors.toMap(Person::getName, person -> person, (p1, p2) -> {
throw new RuntimeException("存在相同的键:" + p1.getName());
}));
System.out.println(personMap5);
}
}
console:
{lisi=20, zhangsan=15, wangwu=30, wangmazi=40}
{lisi=Person{name='lisi', age=20}, zhangsan=Person{name='zhangsan', age=15}, wangwu=Person{name='wangwu', age=30}, wangmazi=Person{name='wangmazi', age=40}}
{lisi=Person{name='lisi', age=21}, zhangsan=Person{name='zhangsan', age=15}, wangwu=Person{name='wangwu', age=30}, wangmazi=Person{name='wangmazi', age=40}}
java.lang.IllegalStateException: Duplicate key Person{name='lisi', age=20}
at java.util.stream.Collectors.lambda$throwingMerger$0(Collectors.java:133)
at java.util.HashMap.merge(HashMap.java:1254)
at java.util.stream.Collectors.lambda$toMap$58(Collectors.java:1320)
at java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169)
at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1382)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at com.mayikt.stream.TestCollectorsToMap.main(TestCollectorsToMap.java:31)
Exception in thread "main" java.lang.RuntimeException: 存在相同的键:lisi
at com.mayikt.stream.TestCollectorsToMap.lambda$main$5(TestCollectorsToMap.java:42)
at java.util.HashMap.merge(HashMap.java:1254)
at java.util.stream.Collectors.lambda$toMap$58(Collectors.java:1320)
at java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169)
at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1382)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at com.mayikt.stream.TestCollectorsToMap.main(TestCollectorsToMap.java:41)
Process finished with exit code 1
----
标签:toMap,java,Collectors,stream,age,util,Person,使用,name From: https://www.cnblogs.com/tenWood/p/16882818.html