public class Student {
private String id;
private String name;
private String level;
}
List<Student> list = new ArrayList<>();
list.add(new Student("1","张三" + 1,"A" + 1));
list.add(new Student("2","张三" + 2,"A" + 2));
list.add(new Student("1","张三" + 3,"A" + 3));
list.add(new Student("4","张三" + 4,"A" + 4));
list.add(new Student("5","张三" + 5,"A" + 5));
Map<String, String> map1 = list.stream()
.collect(
Collectors
.toMap(
Student::getId,
Student::getName));
Map<String, String> map2 = list.stream()
.collect(
Collectors
.toMap(
item -> item.getId(),
item -> item.getName(),
(oldVal, currVal) -> oldVal));
Map<String, String> map3 = list.stream()
.collect(
Collectors
.toMap(
item -> item.getId(),
item -> item.getName(),
(oldVal, currVal) -> oldVal,
HashMap::new));