public static void main(String[] args) { Student student1 = new Student(); student1.setId(110); student1.setName("李明"); student1.setAge(23); Student student2 = new Student(); student2.setName("明浩"); student2.setId(119); student2.setAge(24); List<Student> list = Lists.newArrayList(); list.add(student1); list.add(student2); Map<Integer, Student> studentMap = list.stream().collect(Collectors.toMap(Student:: getId, (k) -> k)); System.out.println(JSON.toJSONString(studentMap)); }
![](https://img2024.cnblogs.com/blog/35695/202408/35695-20240826070647288-243310440.jpg)