美文网首页
stream-groupby

stream-groupby

作者: Raral | 来源:发表于2022-04-25 16:01 被阅读0次
    /**
     * 背景
     * 当list映射成map时,会出现key重复异常?
     *使用分组
     *
     * */
    public class Test {
    
        public static List<MerchPO> merchPOS;
    
    
        public static void main(String[] args) {
            MerchPO merchPO1 = new MerchPO("001", "888", "1", 0);
            MerchPO merchPO2 = new MerchPO("002", "888", "1", 100);
            MerchPO merchPO3 = new MerchPO("003", "888", "2", 200);
            MerchPO merchPO4 = new MerchPO("004", "999", "0", 300);
    
            //添加数据
            merchPOS = Stream.of(merchPO1, merchPO2, merchPO3, merchPO4).collect(Collectors.toList());
    
            Map<String, Map<String, List<MerchPO>>> stringMapMap = handleGroupByType5(merchPOS);
            System.out.println(stringMapMap);
    
        }
    
        //按照店铺类型分组-- 默认map={key, List<T>}
        public static Map<String, List<MerchPO>> handleGroupByType(List<MerchPO> list) {
            Map<String, List<MerchPO>> collect = list.stream().collect(Collectors.groupingBy(MerchPO::getType));
            return collect;
        }
    
        //按照店铺类型分组-- 改变映射结构 map={key, List<string>} Collectors.mapping()
        public static Map<String, List<String>> handleGroupByType2(List<MerchPO> list) {
            Map<String, List<String>> collect = list.stream().collect(Collectors.groupingBy(MerchPO::getType, Collectors.mapping(MerchPO::getMid, Collectors.toList())));
            return collect;
        }
    
        //按照店铺类型-统计个数 Collectors.counting()
        public static Map<String, Long> handleGroupByType3(List<MerchPO> list) {
            Map<String, Long> collect = list.stream().collect(Collectors.groupingBy(MerchPO::getType, Collectors.counting()));
            return collect;
        }
    
    
        //按照店铺类型-统计平均数距离 Collectors.counting()
        public static Map<String, Double> handleGroupByType4(List<MerchPO> list) {
            Map<String, Double> collect = list.stream().collect(Collectors.groupingBy(MerchPO::getType, Collectors.averagingDouble(MerchPO::getDistance)));
            return collect;
        }
    
        //嵌套分组--先分类,mno
        public static Map<String, Map<String, List<MerchPO>>> handleGroupByType5(List<MerchPO> list) {
            Map<String, Map<String, List<MerchPO>>> collect = list.stream().collect(Collectors.groupingBy(MerchPO::getType, Collectors.groupingBy(MerchPO::getMno)));
            return collect;
    
        }
    
        //分组后得到线程安全--ConcurrentMap
        public static Map<String, List<MerchPO>> handleGroupByType6(List<MerchPO> list) {
            ConcurrentMap<String, List<MerchPO>> collect = list.stream().collect(Collectors.groupingByConcurrent(MerchPO::getType));
            return collect;
        }
    
        //分组后,按照 类型排序,TreeMap 默认是升序
        public static Map<String, List<String>> handleGroupByType7(List<MerchPO> list) {
            TreeMap<String, List<String>> collect = list.stream().collect(Collectors.groupingBy(MerchPO::getType, TreeMap::new, Collectors.mapping(MerchPO::getMid, Collectors.toList())));
            Map<String, List<String>> stringListNavigableMap = collect.descendingMap();//反转一下就是,降序
            return stringListNavigableMap;
    
        }
    
    }
    
    

    相关文章

      网友评论

          本文标题:stream-groupby

          本文链接:https://www.haomeiwen.com/subject/axofyrtx.html