List<A> as = Arrays.asList(
A.builder().name("a").age(10).build(),
A.builder().name("a").age(11).build(),
A.builder().name("b").age(12).build(),
A.builder().name("b").age(13).build()
);
BiFunction<List<A>, List<A>, List<A>> func = (x, y) -> {
x.addAll(y);
return x;
};
as.stream().collect(Collectors.toMap(A::getName, t -> Lists.newArrayList(t),
(List<A> x, List<A> y) -> func.apply(x, y)
));
但不建议以上用法, 如果key冲突会报错, 对于有key冲突的处理方式如下
/**
* 对于Collectors.toMap其中对应的key如果是多个,采用如下的解决冲突的方式
*/
List<String> dataList = Arrays.asList("a", "a", "b", "c"); dataList.stream().collect(Collectors.toMap(c->c, String::toLowerCase, (oldKey, newKey)->oldKey));