List<Student> 转 Map<Id,List<Student>>

发布时间:2026/7/24 2:40:45
List<Student> 转 Map<Id,List<Student>> 在 Java 中将ListStudent转换为MapId, ListStudent按 ID 分组有多种方式。我按推荐度从高到低列出一、使用 Stream Collectors.groupingByJava 8最推荐javaimport java.util.List; import java.util.Map; import java.util.stream.Collectors; MapLong, ListStudent map list.stream() .collect(Collectors.groupingBy(Student::getId));说明Collectors.groupingBy()会自动将相同 ID 的学生分到同一个List中。返回的 Map 中每个 Key 对应一个ListStudent。二、使用 Stream Collectors.toMap手动处理重复 KeyjavaMapLong, ListStudent map list.stream() .collect(Collectors.toMap( Student::getId, // Key 映射 student - { ListStudent list new ArrayList(); list.add(student); return list; }, // Value 映射单个元素转 List (existingList, newList) - { // 合并冲突将新 List 合并到已存在的 List 中 existingList.addAll(newList); return existingList; } ));这种方式比较繁琐不如groupingBy简洁不推荐。三、使用Map.computeIfAbsent()Java 8手动循环javaMapLong, ListStudent map new HashMap(); for (Student student : list) { map.computeIfAbsent(student.getId(), k - new ArrayList()) .add(student); }说明computeIfAbsent在 Key 不存在时执行传入的 Lambda 创建一个新ArrayList。如果 Key 已存在直接返回已存在的 List。四、使用Map.putIfAbsent()Java 8另一种写法javaMapLong, ListStudent map new HashMap(); for (Student student : list) { ListStudent studentList map.get(student.getId()); if (studentList null) { studentList new ArrayList(); map.put(student.getId(), studentList); } studentList.add(student); }代码略显冗余不如computeIfAbsent优雅。五、使用 Google Guava外部依赖如果项目引入了 Guavajavaimport com.google.common.collect.ArrayListMultimap; import com.google.common.collect.Multimap; MultimapLong, Student multimap ArrayListMultimap.create(); for (Student student : list) { multimap.put(student.getId(), student); } MapLong, CollectionStudent map multimap.asMap();Multimap天然支持一个 Key 对应多个 Value但返回的是MapLong, CollectionStudent需要额外转换。六、使用Map.computeIfAbsent的一种更简洁写法javaMapLong, ListStudent map new HashMap(); list.forEach(student - map.computeIfAbsent(student.getId(), k - new ArrayList()).add(student) );与第三种写法本质相同只是用了forEach。七、性能对比方法时间复杂度内存开销推荐度groupingByO(n)中等流式操作⭐⭐⭐⭐⭐computeIfAbsentO(n)低直接循环⭐⭐⭐⭐⭐toMap手动合并O(n)中等⭐⭐GuavaMultimapO(n)中等⭐⭐⭐八、完整示例代码javaimport java.util.*; public class Demo { public static void main(String[] args) { // 准备数据 ListStudent students Arrays.asList( new Student(1L, 张三), new Student(1L, 李四), new Student(2L, 王五), new Student(2L, 赵六), new Student(3L, 孙七) ); // 方式一groupingBy推荐 MapLong, ListStudent map1 students.stream() .collect(Collectors.groupingBy(Student::getId)); // 方式二computeIfAbsent推荐 MapLong, ListStudent map2 new HashMap(); for (Student s : students) { map2.computeIfAbsent(s.getId(), k - new ArrayList()).add(s); } // 输出结果 map2.forEach((id, list) - { System.out.println(ID: id); list.forEach(s - System.out.println( s.getName())); }); } } class Student { private Long id; private String name; public Student(Long id, String name) { this.id id; this.name name; } // getter/setter 省略 }九、总结场景推荐方式代码简洁追求可读性list.stream().collect(Collectors.groupingBy(Student::getId))高性能避免 Stream 开销map.computeIfAbsent(id, k - new ArrayList()).add(student)已引入 GuavaMultimap方案需要对 Map 做后续处理groupingBy配合Collectors.toCollection()等最终推荐javaMapLong, ListStudent map list.stream() .collect(Collectors.groupingBy(Student::getId));这是最简洁、最易读的写法也是 Java 8 的标准做法。