class MyObject {Long id;String name;// Constructorpublic MyObject(Long id, String name) {this.id = id;this.name = name;}// Getterspublic Long getId() {return id;}public String getName() {return name;}
}
第一种 List 转 Map<Long, String>
List<MyObject> myObjectList=new ArrayList<>();
// 自行赋值Map<Long, String> map = myObjectList.stream().collect(Collectors.toMap(MyObject::getId, // key mapperMyObject::getName// value mapper));
第二种 List 转 Map<Long, List>
List<MyObject> myObjectList=new ArrayList<>();
// 自行赋值Map<Long, List<String>> map = myObjectList.stream().collect(Collectors.groupingBy(MyObject::getId, // key mapperCollectors.mapping(MyObject::getName, Collectors.toList()) // value mapper));