List<Dog> → Map<name, List<Dog>>
按属性分组(groupingBy
)
# List<Dog> → Map<name, List<Dog>> → 方法3・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));
# List<Dog> → Map<name, List<Dog>> → 方法3・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()));
List<Dog> → Map<name, List<Age>>
按属性分组并映射(groupingBy
+maping
)
# List<Dog> → Map<name, List<Dog>> → 方法3・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>> 方法1・1
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())));
List<Dog> → Map<name, Dog>
# List<Dog> → Map<name, Dog> 方法1・1
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>
# List<Dog> → Map<name, age> → 方法2・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));
# List<Dog> → Map<name, age> → 方法2・2
Map<String, Integer> res = list.stream().collect(HashMap::new, (map, per) -> map.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));