1、按对象属性分组
------------------------------------------------------------------------------------------------------------
# List<Dog> → Map<name, List<Dog>>
------------------------------------------------------------------------------------------------------------
# 方法1
Map<String, List<Dog>> collect1 = list.stream().collect(Collectors.groupingBy(Dog::getName));
# 综上 最安全方式
Map<String, List<Dog>> collect1 = list.stream().filter(a -> a != null && a.getName() != null).collect(Collectors.groupingBy(Dog::getName));
------------------------------------------------------------------------------------------------------------
# 方法2
Map<String, List<Dog>> collect2 = list.stream().collect(Collectors.groupingBy(Dog::getName, HashMap::new, Collectors.toList()));
# 综上 最安全方式
Map<String, List<Dog>> collect2 = list.stream().filter(a -> a != null && a.getName() != null).collect(Collectors.groupingBy(Dog::getName, HashMap::new, Collectors.toList()));
------------------------------------------------------------------------------------------------------------
# 方法3
Map<String, List<Dog>> collect3 = list.stream().collect(Collectors.groupingBy(Dog::getName, Collectors.mapping(Function.identity(), Collectors.toList())));
# 综上 最安全方式
Map<String, List<Dog>> collect3 = list.stream().filter(a -> a != null && a.getName() != null).collect(Collectors.groupingBy(Dog::getName, Collectors.mapping(Function.identity(), Collectors.toList())));
------------------------------------------------------------------------------------------------------------
# List<Dog> → Map<name, List<Age>>
------------------------------------------------------------------------------------------------------------
Map<String, List<Integer>> res = list.stream().collect(Collectors.groupingBy(Dog::getName, Collectors.mapping(Dog::getAge, Collectors.toList())));
# 综上 最安全方式
Map<String, List<Integer>> collect4 = list.stream().filter(a -> a != null && a.getName() != null).collect(Collectors.groupingBy(Dog::getName, Collectors.mapping(Dog::getAge, Collectors.toList())));
2、按对象属性映射
------------------------------------------------------------------------------------------------------------
# List<Dog> → Map<name, Dog>
------------------------------------------------------------------------------------------------------------
Map<String, Dog> res = list.stream().collect(Collectors.toMap(Dog::getName, Function.identity()));
# Key重复报错(包括null) → IllegalStateException: Duplicate key
# Key重复时覆盖
Map<String, Dog> collect8 = list.stream().collect(Collectors.toMap(Dog::getName, Function.identity(), (existing, replacement) -> replacement));
# Key重复时丢弃
Map<String, Dog> collect9 = list.stream().collect(Collectors.toMap(Dog::getName, Function.identity(), (existing, replacement) -> existing));
------------------------------------------------------------------------------------------------------------
# List<Dog> → Map<name, age>
------------------------------------------------------------------------------------------------------------
# 方式1
Map<String, Integer> res = list.stream().collect(Collectors.toMap(Dog::getName, Dog::getAge));
# value为null → NPE
# Key重复报错 → IllegalStateException: Duplicate key
# Key重复时覆盖
Map<String, Integer> collect9 = list.stream().collect(Collectors.toMap(Dog::getName, Dog::getAge, (existing, replacement) -> replacement));
# Key重复时丢弃
Map<String, Integer> collect9 = list.stream().collect(Collectors.toMap(Dog::getName, Dog::getAge, (existing, replacement) -> existing));
# 综上 最安全方式
Map<String, Integer> res = list.stream().filter(a -> a != null && a.getAge() != null).collect(Collectors.toMap(Dog::getName, Dog::getAge, (existing, replacement) -> replacement));
------------------------------------------------------------------------------------------------------------
# 方式2
Map<String, Integer> res = list.stream().collect(HashMap::new, (map1, per) -> map1.put(per.getName(), per.getAge()), HashMap::putAll);
[代码总结] 获取字符串→字符串长度的Map
List<String> list = Arrays.asList("a1", "ae2", "b1bb", "cq2", "c1fgfv");
Map<String, Integer> map = list.stream().collect(Collectors.toMap(str -> str, str -> str.length()));
Map<String, Integer> map = list.stream().collect(Collectors.toMap(Function.identity(), String::length));