嗨客网搜索
Java List转Map

Java List转Map教程

平时工作中,在很多场景下面会用到将 List 集合转换成 Map 对象的格式,提高效率。我们可以使用 Collectors.groupingBy 对指定的属性进行分组,转换成 Map 格式。

数据准备

学生类

package com.haicoder.net.array; public class Student { /** * 班级ID */ private Integer calssId; /** * 学生学号 */ private String stuNo; /** * 学生姓名 */ private String stuName; /** * 学生年龄 */ private Integer age; public Student(Integer calssId, String stuNo, String stuName, Integer age) { this.calssId = calssId; this.stuNo = stuNo; this.stuName = stuName; this.age = age; } @Override public String toString() { return "Student{" + "calssId=" + calssId + ", stuNo='" + stuNo + '\'' + ", stuName='" + stuName + '\'' + ", age=" + age + '}'; } //.... 省略 get set 方法 }

构造集合数据

List<Student> studentList = new ArrayList<>(); studentList.add(new Student(1, "001", "小明",20)); studentList.add(new Student(1, "002", "小军",18)); studentList.add(new Student(1, "003", "小杰",19)); studentList.add(new Student(2, "001", "小亮",20)); studentList.add(new Student(3, "001", "小珍",18)); studentList.add(new Student(2, "002", "小琦",19)); studentList.add(new Student(3, "002", "小甲",15));

案例

根据某个字段进行分组

按照班级分组,以班级 Id 为 key,将数据转换成 Map

Map<Integer, List<Student>> classIdAndStudentMap = studentList.stream().collect(Collectors.groupingBy(Student::getCalssId));

按照属性拼接进行分组

将班级和年龄进行拼接分组

Map<String, List<Student>> classIdAndStudentMap = studentList.stream().collect(Collectors.groupingBy(student -> student.getCalssId() + "-" + student.getAge()));

根据不同条件分组

年龄小于 19 岁分一个组,大于等于 19 岁分另外一个组

Map<String, List<Student>> classIdAndStudentMap = studentList.stream().collect(Collectors.groupingBy(student -> { if (student.getAge() < 19) { return "未成年"; } else { return "成年"; } }));

求对应key下面的数据总数

获取每个班级下面的学生总数

Map<Integer, Long> classIdAndStudentMap = studentList.stream().collect(Collectors.groupingBy(Student::getCalssId, Collectors.counting()));

完整代码

按照属性拼接进行分组

package com.haicoder.net.array; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class StudentTest { public static void main(String[] args) { System.out.println("嗨客网(www.haicoder.net)"); List<Student> studentList = new ArrayList<>(); studentList.add(new Student(1, "001", "小明", 20)); studentList.add(new Student(1, "002", "小军", 18)); studentList.add(new Student(1, "003", "小杰", 19)); studentList.add(new Student(2, "001", "小亮", 20)); studentList.add(new Student(3, "001", "小珍", 18)); studentList.add(new Student(2, "002", "小琦", 19)); studentList.add(new Student(3, "002", "小甲", 15)); Map<Integer, List<Student>> classIdAndStudentMap = studentList.stream().collect(Collectors.groupingBy(Student::getCalssId)); classIdAndStudentMap.entrySet().forEach(tmp -> { System.out.println("班级ID:" + tmp.getKey()); System.out.println("班级学生:" + tmp.getValue().toString()); }); } }

运行结果如下:

01 class_id_map.png

按照属性拼接进行分组

package com.haicoder.net.array; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class StudentTest { public static void main(String[] args) { System.out.println("嗨客网(www.haicoder.net)"); List<Student> studentList = new ArrayList<>(); studentList.add(new Student(1, "001", "小明", 20)); studentList.add(new Student(1, "002", "小军", 18)); studentList.add(new Student(1, "003", "小杰", 19)); studentList.add(new Student(2, "001", "小亮", 20)); studentList.add(new Student(3, "001", "小珍", 18)); studentList.add(new Student(2, "002", "小琦", 19)); studentList.add(new Student(3, "002", "小甲", 15)); Map<String, List<Student>> classIdAndStudentMap = studentList.stream().collect(Collectors.groupingBy(student -> student.getCalssId() + "-" + student.getAge())); classIdAndStudentMap.entrySet().forEach(tmp -> { System.out.println("key 值:" + tmp.getKey()); System.out.println("班级学生:" + tmp.getValue().toString()); }); } }

运行结果如下

02 classid_age_map.png

根据不同条件分组

package com.haicoder.net.array; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class StudentTest { public static void main(String[] args) { System.out.println("嗨客网(www.haicoder.net)"); List<Student> studentList = new ArrayList<>(); studentList.add(new Student(1, "001", "小明", 20)); studentList.add(new Student(1, "002", "小军", 18)); studentList.add(new Student(1, "003", "小杰", 19)); studentList.add(new Student(2, "001", "小亮", 20)); studentList.add(new Student(3, "001", "小珍", 18)); studentList.add(new Student(2, "002", "小琦", 19)); studentList.add(new Student(3, "002", "小甲", 15)); Map<String, List<Student>> classIdAndStudentMap = studentList.stream().collect(Collectors.groupingBy(student -> { if (student.getAge() < 19) { return "未成年"; } else { return "成年"; } })); classIdAndStudentMap.entrySet().forEach(tmp -> { System.out.println("key 值:" + tmp.getKey()); System.out.println("value 值:" + tmp.getValue().toString()); }); } }

运行结果如下

03 class_condition.png

获取每个班级下面的学生总数

package com.haicoder.net.array; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class StudentTest { public static void main(String[] args) { System.out.println("嗨客网(www.haicoder.net)"); List<Student> studentList = new ArrayList<>(); studentList.add(new Student(1, "001", "小明", 20)); studentList.add(new Student(1, "002", "小军", 18)); studentList.add(new Student(1, "003", "小杰", 19)); studentList.add(new Student(2, "001", "小亮", 20)); studentList.add(new Student(3, "001", "小珍", 18)); studentList.add(new Student(2, "002", "小琦", 19)); studentList.add(new Student(3, "002", "小甲", 15)); Map<Integer, Long> classIdAndStudentMap = studentList.stream().collect(Collectors.groupingBy(Student::getCalssId, Collectors.counting())); classIdAndStudentMap.entrySet().forEach(tmp -> { System.out.println("班级ID 值:" + tmp.getKey() + "====》" + "班级学生数量:" + tmp.getValue()); }); } }

运行结果如下

04 map_count.png

嗨客网顶部