Java stream操作toMap总结

Posted 程序员超时空

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java stream操作toMap总结相关的知识,希望对你有一定的参考价值。

1、map 对象本身,重复的key,放入List。

Map<String, List<Working>> map =
                workings.stream().collect(Collectors.toMap(Working::getInvoicePage,
                        e -> 
                            ArrayList<Working> list = new ArrayList<>();
                            list.add(e);
                            return list;
                        ,
                        (oldList, newList) -> 
                            oldList.addAll(newList);
                            return oldList;
                        ));
		// 或者使用groupBy
		// 存为List
        Map<String, List<BusinessSceneDetail>> collect0 =
                sceneDetailMap.values().stream().collect(
                        Collectors.groupingBy(BusinessSceneDetail::getDataSourceCode));
        // 存为set
        Map<String, Set<BusinessSceneDetail>> collect =
                sceneDetailMap.values().stream().collect(
                        Collectors.groupingBy(BusinessSceneDetail::getDataSourceCode, Collectors.toSet()));
        // 多层map
        Map<String, Map<String, Set<BusinessSceneDetail>>> collect1 = sceneDetailMap.values().stream().collect(
                Collectors.groupingBy(BusinessSceneDetail::getDataSourceCode, Collectors.groupingBy
                        (BusinessSceneDetail::getBusinessSceneCode, Collectors.toSet())));
        // 对象子属性 map
        Map<String, List<String>> collect2 = sceneDetailMap.values().stream().collect(Collectors.groupingBy
                (BusinessSceneDetail::getDataSourceCode, Collectors.mapping(BusinessSceneDetail::getRuleContent,
                        Collectors.toList())));
        // 对象按照规则获取一个
        Map<String, BusinessSceneDetail> collect3 =
                sceneDetailMap.values().stream().collect(Collectors.groupingBy
                        (
                                BusinessSceneDetail::getDataSourceCode,
                                Collectors.collectingAndThen(
                                        Collectors.maxBy(Comparator.comparingInt(BusinessSceneDetail::getCreatorId)),
                                        Optional::get
                                )
                        ));

        // 对象按照规则获取一个里面的值 p肯定存在
        Map<String, String> collect4 = sceneDetailMap.values().stream().collect(Collectors.groupingBy
                (
                        BusinessSceneDetail::getDataSourceCode,
                        Collectors.collectingAndThen(
                                Collectors.maxBy(Comparator.comparingInt(BusinessSceneDetail::getCreatorId)),
                                p -> p.get().getRuleContent()
                        )
                ));
                
        // map的value进行排序
           Map<String, List<SysDictionary>> map = sysDictionaryList.stream().collect(
                Collectors.groupingBy(
                        SysDictionary::getSysCode,
                        Collectors.collectingAndThen(
                                Collectors.toCollection(() -> new TreeSet<>((Comparator<Object>) (o1, o2) -> 0)),
                                ArrayList::new
                        )
                )
        );
        // map的key进行排序
 		Map<String, List<SysDictionary>> map1 = sysDictionaryList.stream().collect(
                Collectors.groupingBy(
                        SysDictionary::getSysCode,
                        TreeMap::new,
                        Collectors.toList()
                )
        );

groupBy其他用法参考 https://blog.csdn.net/u014231523/article/details/102535902
2、map 对象本身,重复的key,替换内容。

Map<String, Working> map =
                workings.stream().collect(Collectors.toMap(Working::getInvoicePage,
                Function.identity(),
                (oldWorking, newWorking) -> newWorking));

3、map 对象成员变量,重复的key,放入List。

Map<String, List<String>> map =
                workings.stream().collect(Collectors.toMap(Working::getInvoicePage,
                        e -> 
                            ArrayList<String> list = new ArrayList<>();
                            list.add(e.getStatus());
                            return list;
                        ,
                        (oldList, newList) -> 
                            oldList.addAll(newList);
                            return oldList;
                        ));

4、map 对象成员变量,重复的key,替换。

Map<String, String> map =
                workings.stream().collect(Collectors.toMap(Working::getInvoicePage,
                        Working::getStatus,
                        (oldWorking, newWorking) -> newWorking));

5、带排序

TreeMap<String, List<Working>> collect =
                workings.stream().collect(Collectors.toMap(Working::getInvoicePage,
                        e -> 
                            ArrayList<Working> list = new ArrayList<>();
                            list.add(e);
                            return list;
                        ,
                        (oldList, newList) -> 
                            oldList.addAll(newList);
                            return oldList;
                        ,
                        () -> new TreeMap<>(Comparator.comparing(Integer::valueOf))));

以上是关于Java stream操作toMap总结的主要内容,如果未能解决你的问题,请参考以下文章

java8的stream中Collectors.toMap空指针问题

JDK8 stream toMap() java.lang.IllegalStateException: Duplicate key异常解决(key重复)

JAVA804_List.stream().collect(Collectors.toMap(User::getId, k1->k1,(k1,k2)->k2))key重复问题

Java 8 Streams 中的过滤器映射

《Java8实战》 - 读书笔记 - Stream流操作2:用流收集数据

Java8新特性Stream之list转map及问题解决