1.toMap的几个方法
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); }
2.参数含义分别是:
-
keyMapper:Key 的映射函数
-
valueMapper:Value 的映射函数
-
mergeFunction:当 Key 冲突时,调用的合并方法
-
mapSupplier:Map 构造器,在需要返回特定的 Map 时使用
3.
@Test public void tpMap() throws JsonProcessingException { List<User> userList = Lists.newArrayList( new User().setId("A").setName("张三"), new User().setId("B").setName("李四"), new User().setId("C").setName("王五") ); // 格式:A-> 张三 Map<String, String> collect = userList.stream().collect(Collectors.toMap(User::getId, User::getName)); log.info("collect:{}", mapper.writerWithDefaultPrettyPrinter().writeValueAsString(collect)); // 格式:得到 Map 的 value 为对象本身时 Map<String, User> collect1 = userList.stream().collect(Collectors.toMap(User::getId, t -> t)); log.info("collect1:{}", mapper.writerWithDefaultPrettyPrinter().writeValueAsString(collect1)); // 格式:得到 Map 的 value 为对象本身时 Map<String, User> collect2 = userList.stream().collect(Collectors.toMap(User::getId, Function.identity())); log.info("collect2:{}", mapper.writerWithDefaultPrettyPrinter().writeValueAsString(collect2)); }
效果:
14:29:51.734 [main] INFO com.jun.prove.common.test.MapTest - collect:{ "A" : "张三", "B" : "李四", "C" : "王五" } 14:29:51.782 [main] INFO com.jun.prove.common.test.MapTest - collect1:{ "A" : { "id" : "A", "name" : "张三" }, "B" : { "id" : "B", "name" : "李四" }, "C" : { "id" : "C", "name" : "王五" } } 14:29:51.782 [main] INFO com.jun.prove.common.test.MapTest - collect2:{ "A" : { "id" : "A", "name" : "张三" }, "B" : { "id" : "B", "name" : "李四" }, "C" : { "id" : "C", "name" : "王五" } }
4.调用第二个重载方法,传入合并函数
如果 List 中 userId 有相同的,使用上面的写法会抛异常
@Test public void tpMapExtend() throws JsonProcessingException { List<User> userList = Lists.newArrayList( new User().setId("A").setName("张三"), new User().setId("A").setName("李四"), // Key 相同 new User().setId("C").setName("王五") ); // Map<String, String> collect = userList.stream().collect(Collectors.toMap(User::getId, User::getName, (n1, n2) -> n1 + n2)); log.info("collect:{}", mapper.writeValueAsString(collect)); // TreeMap<String, String> collect1 = userList.stream().collect(Collectors.toMap(User::getId, User::getName, (n1, n2) -> n1, TreeMap::new)); log.info("collect1:{}", mapper.writeValueAsString(collect1)); }
效果
14:31:46.127 [main] INFO com.jun.prove.common.test.MapTest - collect:{"A":"张三李四","C":"王五"} 14:31:46.135 [main] INFO com.jun.prove.common.test.MapTest - collect1:{"A":"张三","C":"王五"}
5.第三个方法
第四个参数(mapSupplier)用于自定义返回 Map 类型,比如我们希望返回的 Map 是根据 Key 排序的
在上面的程序中,已经写过了