Java:List转Map (用stream实现)

 前言:JDK1.8及其以上,才支持下面案例
 
//实体类
public class Student {
    private  String no;  //学号
    private  String name;	//姓名
    private  Integer age; //年龄
    //构造方法忽略
    //set、get 方法忽略
}
public class Teacher{
    private  String no;  //教师号
    private  String name; //姓名
    private  Integer age; //年龄
}

一、List<Object>Map<String, String>

//注意事项 
//在使用java.util.stream.Collectors 类的 toMap()方法转为 Map集合时,一定要注意当Map的value为null时会抛空指针异常!!!(如:下面示例的getName不可为空!!!)

//声明一个List集合
List<Student> list = new ArrayList();  
	 list.add(new Student("1001", "小A", 15));  
	 list.add(new Student("1001", "小B", 20));//学号重复(下面特殊处理)
	 list.add(new Student("1002", "小C", 15));
	 list.add(new Student("1003", "小D", 30));
       
//将list转map 【key为1个属性,value为1个属性】  (map的键重复不会报错,下面已经处理)(注意:getName不可为空!!!)
Map<String, String> map =
	list.stream().collect(Collectors.toMap( 
		Student::getNo, 
		Student::getName,
	    (key1 , key2) -> key1 
	));
	
//将list转map 【key为多个属性,value为1个属性】  (map的键重复不会报错,下面已经处理)(注意:getName不可为空!!!)
Map<String, String> map =
	list.stream().collect(Collectors.toMap( 
		obj -> obj.getNo() + "_" + obj.getName(),
		Student::getName,
	    (key1 , key2) -> key1 
	));

二、List<Object>Map<String, Object> (返回对象本身)

//声明一个List集合
List<Student> list = new ArrayList();
	list.add(new Student("1001", "小A", 15));  
	 list.add(new Student("1001", "小B", 20));
	 list.add(new Student("1002", "小C", 15));
	 list.add(new Student("1003", "小D", 30));

//将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<Object1>Map<String, Object2> (返回另一个对象)

//声明一个List集合
List<Student> list = new ArrayList();
	list.add(new Student("1001", "小A", 15));  
	list.add(new Student("1001", "小B", 20));
	list.add(new Student("1002", "小C", 15));
	list.add(new Student("1003", "小D", 30));

//将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
    ));

四、List<Object>Map<String, List<Object>> (分组)【以1个字段分/以多个字段 只分1次组】

//声明一个List集合
List<Student> list = new ArrayList();
   list.add(new Student("1001", "小A", 15));  
   list.add(new Student("1001", "小B", 20));
   list.add(new Student("1002", "小C", 15));
   list.add(new Student("1003", "小D", 30));

//将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) );

//tips: 上面遍历的时候,可以这样写:
map.forEach( (oneKey, students) -> {
          
});

五、List<Object>Map<String, List<String>> (分组)【针对后面的集合,只提取Object里面的某个字段,而不是源对象Object】

//声明一个List集合
List<Student> list = new ArrayList();
   list.add(new Student("1001", "小A", 15));  
   list.add(new Student("1001", "小B", 20));
   list.add(new Student("1002", "小C", 15));
   list.add(new Student("1003", "小D", 30));

//将list转map  【key为1个属性,value为相同key的集合对象中的某个字段】 (以某1个属性来分组,将分组后相同键对应的对象 中的某个字段放在一起)
Map<String, List<String>> map = 
   list.stream().collect(  Collectors.groupingBy(Student::getNo, Collectors.mapping(Student::getName,Collectors.toList())) );

//tips: 上面遍历的时候,可以这样写:
map.forEach( (no, names) -> {
          
});

六、List<Object>Map<String, Map<String, List<Student>>> (分组)【先根据某个字段分组,再根据另一个字段分组,一共分2个层级的组】

//声明一个List集合
List<Student> list = new ArrayList();
   list.add(new Student("1001", "小A", 15));  
   list.add(new Student("1001", "小B", 15));
   list.add(new Student("1001", "小C", 20));  
   list.add(new Student("1002", "小D", 15));
   list.add(new Student("1003", "小E", 30));

//将list转map  【第一个key为1个属性,第二个key为另1个属性,value为分组后的对象集合】
//下面例子解释:第一个key是学号,第二个key是年龄,最后的值是相同学号,相同年龄的对象集合
Map<String, Map<String, List<Student>>>  map = 
   list.stream().collect(  Collectors.groupingBy(Student::getNo, Collectors.groupingBy(Student::getAge)) );


五、学习链接:

https://blog.csdn.net/qq_39629277/article/details/83012548
https://blog.csdn.net/lu930124