list转map(JDK8-Lambda表达式)

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 list = Lists.newArrayList();
    list.add(student1);
    list.add(student2);

    Map studentMap = list.stream().collect(Collectors.toMap(Student:: getId, (k) -> k));

    System.out.println(JSON.toJSONString(studentMap));
}

你可能感兴趣的:(list转map(JDK8-Lambda表达式))