java.util.stream.Collectors
一.Collectors.toMap
1.构造List
List<Student> list = new ArrayList<>();
for (int i = 1; i < 4; i++) {
list.add(new Student(i+"","学生"+i));
}
2.将list转成以id为key,value为id对应Sudent对象的map
Map<String, Student> map = list.stream().collect(Collectors.toMap(Student::getId, Function.identity()));
3.假如id存在重复值,则会报错Duplicate key xxx, 解决方案是
// 取原来的值
Map<String, Student> map = list.stream().collect(Collectors.toMap(Student::getId,Function.identity(),(oldValue,newValue) -> newValue));
// 取最新的值
Map<String, Student> map = list.stream().collect(Collectors.toMap(Student::getId,Function.identity(),(oldValue,newValue) -> oldValue));
4.想获得一个id和name对应的Map<String, String>
Map<String, String> map = list.stream().collect(Collectors.toMap(Student::getId,Student::getName));
// 注意:name可以为空字符串但不能为null,否则会报空指针,解决方案:
Map<String, String> map = list.stream().collect(Collectors.toMap(Student::getId, e->e.getName()==null?"":e.getName()));
// 假如存在id重复,两个value可以这样映射到同一个id:
Map<String, String> map = list.stream().collect(Collectors.toMap(Student::getId,Student::getName,(e1,e2)->e1+","+e2));
5.把集合按照group属性分组到map中
Map<String, List<Student>> map = list.stream().collect(Collectors.groupingBy(Student::getGroup));
// 输出
map = {
学生2=[
CollectorsTest.Student(id=2, name=null, group=学生2),
CollectorsTest.Student(id=2, name=null, group=学生2)
],
学生1=[CollectorsTest.Student(id=1, name=null, group=学生1)],
学生3=[CollectorsTest.Student(id=3, name=null, group=学生3)]
}
6.过滤去重,两个List
List<Student> list1 = new ArrayList<>();
List<Student> list2= new ArrayList<>();
HashMap<String, String> hashMap = new HashMap<>();
for (int i = 1; i < 4; i++) {
list1.add(new Student(i+"","学生"+i));
}
for (int i = 2; i < 5; i++) {
list2.add(new Student(i+"","学生"+i));
}
// 过滤出list1的key 与 map2相同的key
Stream<Student> studentStream = list1.stream().map(Student::getId).filter(map2::containsKey).map(map2::get);
List<String> strings = list1.stream().map(Student::getId).filter(map2::containsKey).map(map2::get).map(Student::getName).collect(Collectors.toList());
7.返回 Map<String, Map<Integer, Object>>
List<Person> personList = new ArrayList<>();
personList.add(new Person("hepengju", 28, 20000.0));
personList.add(new Person("lisi" , 44, 40000.0));
personList.add(new Person("wangwu" , 55, 50000.0));
personList.add(new Person("zhaoliu" , 66, 60000.0));
personList.add(new Person("zhangsan", 33, 33333.0));
personList.add(new Person("wgr", 23, 10000.0));
Map<String, Map<Integer, Person>> collect = personList.stream().collect(Collectors.toMap(Person::getName, p -> {
Map<Integer, Person> map = new HashMap<>();
map.put(p.getAge(), p);
return map;
}));
collect.forEach((name, p) -> {
System.out.println(name + ":" + p);
});
// 输出
lisi:{44=CollectorsTest.Person(name=lisi, age=44, group=40000.0)}
wgr:{23=CollectorsTest.Person(name=wgr, age=23, group=10000.0)}
zhaoliu:{66=CollectorsTest.Person(name=zhaoliu, age=66, group=60000.0)}
hepengju:{28=CollectorsTest.Person(name=hepengju, age=28, group=20000.0)}
zhangsan:{33=CollectorsTest.Person(name=zhangsan, age=33, group=33333.0)}
wangwu:{55=CollectorsTest.Person(name=wangwu, age=55, group=50000.0)}
标签:map,toMap,java,name,Collectors,Person,Student,new
From: https://www.cnblogs.com/AlwaysStudying/p/17236975.html