Stream ToMap(Collectors.toMap)

Stream ToMap(Collectors.toMap)

List TO Map

List Stream 转换 Map时向collect()方法中传递Collector对象,对象由Collectors.toMap()方法返回。

实例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
List<GroupBrandCateBO> list = new ArrayList<>(  
Arrays.asList(
new GroupBrandCateBO("v1", "g1", "b1"),
new GroupBrandCateBO("v1", "g1", "b1"),
new GroupBrandCateBO("v3", "g3", "b3")
)
);
Map<String, String> map = list.stream().collect(Collectors.toMap(item -> item.getVersion(), item -> item.getGroupCode(), (oldVal, currVal) -> oldVal, LinkedHashMap::new));
System.out.println(map.getClass());
Map<String, String> map0 = list.stream().collect(Collectors.toMap(item -> item.getVersion(), item -> item.getGroupCode(), (oldVal, currVal) -> oldVal));
System.out.println(map0.getClass());
System.out.println(map0.toString());
Map<String, String> map1 = list.stream().collect(Collectors.toMap(GroupBrandCateBO::getVersion, GroupBrandCateBO::getGroupCode));
System.out.println(map1.toString());

问题

当存在Key相同的情况下会出现如下异常:

Exception in thread “main” java.lang.IllegalStateException: Duplicate key……


原因分析

toMap()函数重载:
未指定合并函数mergeFunction情况下,传入throwingMerger()返回BinaryOperator对象,当出现key重复时,调用合并函数!
未指定Supplier实例情况下,默认生成HashMap实例!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public static <T, K, U>  
Collector<T, ?, Map<K,U>> toMap(Function<? super T, ? extends K> keyMapper,
Function<? super T, ? extends U> valueMapper) {
return toMap(keyMapper, valueMapper, throwingMerger(), HashMap::new);
}

public static <T, K, U>
Collector<T, ?, Map<K,U>> toMap(Function<? super T, ? extends K> keyMapper,
Function<? super T, ? extends U> valueMapper,
BinaryOperator<U> mergeFunction) {
return toMap(keyMapper, valueMapper, mergeFunction, HashMap::new);
}

public static <T, K, U, M extends Map<K, U>>
Collector<T, ?, M> toMap(Function<? super T, ? extends K> keyMapper,
Function<? super T, ? extends U> valueMapper,
BinaryOperator<U> mergeFunction,
Supplier<M> mapSupplier) {
BiConsumer<M, T> accumulator
= (map, element) -> map.merge(keyMapper.apply(element),
valueMapper.apply(element), mergeFunction);
return new CollectorImpl<>(mapSupplier, accumulator, mapMerger(mergeFunction), CH_ID);
}

private static <T> BinaryOperator<T> throwingMerger() {
return (u,v) -> { throw new IllegalStateException(String.format("Duplicate key %s", u)); };
}


解决方案:
加上:

1
2
(oldVal, currVal) -> currVal) // key相同时当前值替换原始值  
(oldVal, currVal) -> oldVal + currVal //key相同时保留原始值和当前值,合并value!!!!!!

注:合并的是值value,而不是key!!!


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!