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://www.cnblogs.com/JimmyThomas/p/12105272.html