List<Object>
转Map<String,List<String>>
Map<String, List<String>> skillAndList = list.stream().
collect(Collectors.groupingBy(Employee::getSkillId, Collectors.mapping(Employee::getStudent, Collectors.toList())));
List<Object>
转 Map<String, List<Object>>
(分组)【以1个字段分/以多个字段分】
//声明一个List集合
List<Student> list = new ArrayList();
list.add(new Student("1001", "小A"));
list.add(new Student("1001", "小B"));
list.add(new Student("1002", "小C"));
list.add(new Student("1003", "小D"));
//将list转map 【key为1个属性,value为相同key的集合】 (以某1个属性来分组,将分组后相同的对象放在一起)
Map<String, List<Student>> map =
list.stream().collect( Collectors.groupingBy(Student::getNo) );
//将list转map 【key为多个属性,value为相同key的集合】 (以多个属性来分组,将分组后相同的对象放在一起)
Map<String, List<Student>> map =
list.stream().collect( Collectors.groupingBy(obj -> obj.getNo + "_" + obj.getName) );
List<Object>
转 Map<String, Object>
(返回对象本身)
//声明一个List集合
List<Student> list = new ArrayList();
list.add(new Student("1001", "小A"));
list.add(new Student("1001", "小B"));
list.add(new Student("1002", "小C"));
list.add(new Student("1003", "小D"));
//将list转map 【key为1个属性,value为对象本身】 (map的键去重)
Map<String, Student> map =
list.stream().collect(Collectors.toMap(
Student::getNo,
obj -> obj,
(key1 , key2) -> key1
));
或者
//将list转map 【key为1个属性,value为对象本身】 (map的键去重)
Map<String, Student> map =
list.stream().collect(Collectors.toMap(
Student::getNo,
Function.identity(),
(key1 , key2) -> key1
));
或者
//将list转map 【key为多个属性,value为对象本身】 (map的键去重)
Map<String, Student> map =
list.stream().collect(Collectors.toMap(
obj -> obj.getNo() + "_" + obj.getName(),
obj -> obj,
(key1 , key2) -> key1
));
List<Object>
转 Map<String, String>
//声明一个List集合 List<Student> list = new ArrayList(); list.add(new Student("1001", "小A")); list.add(new Student("1001", "小B"));//学号重复(下面特殊处理) list.add(new Student("1002", "小C")); list.add(new Student("1003", "小D")); //将list转map 【key为1个属性,value为1个属性】 Map<String, String> map = list.stream().collect(Collectors.toMap( Student::getNo, Student::getName, (key1 , key2) -> key1 //(map的键重复不会报错,下面已经处理) )); //将list转map 【key为多个属性,value为1个属性】 Map<String, String> map = list.stream().collect(Collectors.toMap( obj -> obj.getNo() + "_" + obj.getName(), Student::getName, (key1 , key2) -> key1 //(map的键重复不会报错,下面已经处理) ));
List<Object1>
转 Map<String, Object2>
(返回另一个对象)
//声明一个List集合 List<Student> list = new ArrayList(); list.add(new Student("1001", "小A")); list.add(new Student("1001", "小B")); list.add(new Student("1002", "小C")); list.add(new Student("1003", "小D")); //将list转map 【key为1个属性,value为另一个对象】 (map的键去重) Map<String, Teacher> map = list.stream().collect(Collectors.toMap( Student::getNo, stu -> { Teacher teacher = new Teacher(); teacher.setNo(stu.getNo()); teacher.setName(stu.getName()); return teacher; }, (key1 , key2) -> key1 ));
标签:map,java,Map,list,add,Student,new
From: https://www.cnblogs.com/xzjf/p/17505167.html