list转map(根据某个或多个属性分组)
需要将对应的list换成本地list,和对象换成本地对象
1、List转Map<String,List>
// 根据一个字段分组
Map<String, List<String>> map = objectLists.stream()
.collect(Collectors.groupingBy(
Object::getName,
Collectors.mapping(Object::getId, Collectors.toList())));
// 根据多个字段分组
Map<String, List<Object>> groupMap = objectLists.stream().collect(
Collectors.groupingBy(s->s.getId()+s.getName()));
// 把某属性作为key,对应的Object对象作为value
Map<String, List<Object>> groupMap = objectLists.stream().collect(
Collectors.groupingBy(Object::getName));
2、List转Map<String,List>
Map<String, List<String>> studentMap = studentList.stream()
.collect(Collectors.groupingBy(
Object::getId,
Collectors.mapping(Object::getName, Collectors.toList())
));