java8 stream的groupingBy笔记

1.业务需求

       当时期望在通过groupingBy后,进行字段有序排列。但是一直未找到单使用groupingBy无法实现分组后对,分组的list进行内部直接排序的方。

       通过groupingBy后转为LinkedHashMap已实现有序排列,通过遍历,对目标字段进行排序。实现要求的不同排序需求。

    public static void main(String[] args) {
        List<PcSystem> arrayList = new ArrayList();

        arrayList.add(new PcSystem("11","3"));
        arrayList.add(new PcSystem("12","4"));
        arrayList.add(new PcSystem("12","5"));
        arrayList.add(new PcSystem("12","6"));
        arrayList.add(new PcSystem("13","7"));
        arrayList.add(new PcSystem("13","8"));
        arrayList.add(new PcSystem("13","9"));
        arrayList.add(new PcSystem("13","10"));
        arrayList.add(new PcSystem("11","1"));
        arrayList.add(new PcSystem("11","2"));

        List<PcSystem> collect1 = arrayList.stream().sorted(Comparator.comparing(pcSystem -> pcSystem.getUserId())).collect(Collectors.toList());
        List<PcSystem> collect2 = arrayList.stream().sorted(Comparator.comparing(PcSystem::getUserId).reversed()).collect(Collectors.toList());
        LinkedHashMap<String, List<PcSystem>> groupMap1 = collect1.stream().collect(Collectors.groupingBy(PcSystem::getId,LinkedHashMap::new,Collectors.toList()));
        LinkedHashMap<String, List<PcSystem>> groupMap2 = collect2.stream().collect(Collectors.groupingBy(PcSystem::getId,LinkedHashMap::new,Collectors.toList()));
        System.out.println(groupMap1);
        System.out.println(groupMap2);
        for(Map.Entry<String, List<PcSystem>> entry : groupMap1.entrySet()){
            List<PcSystem> value = entry.getValue();
            groupMap1.put(entry.getKey(),value.stream().sorted(Comparator.comparing(PcSystem::getUserId).reversed()).collect(Collectors.toList()));

        }
        for(Map.Entry<String, List<PcSystem>> entry : groupMap2.entrySet()){
            List<PcSystem> value = entry.getValue();
            groupMap2.put(entry.getKey(),value.stream().sorted(Comparator.comparing(PcSystem::getUserId).reversed()).collect(Collectors.toList()));

        }
        System.out.println(groupMap1);
        System.out.println(groupMap2);
    }

2.输出日志

输出结果1
{11=[PcSystem(id=11, userId=1), PcSystem(id=11, userId=2), PcSystem(id=11, userId=3)], 
13=[PcSystem(id=13, userId=10), PcSystem(id=13, userId=7), PcSystem(id=13, userId=8),PcSystem(id=13, userId=9)], 
12=[PcSystem(id=12, userId=4), PcSystem(id=12, userId=5), PcSystem(id=12, userId=6)]}
输出结果2
{13=[PcSystem(id=13, userId=9), PcSystem(id=13, userId=8), PcSystem(id=13, userId=7), 
PcSystem(id=13, userId=10)], 
12=[PcSystem(id=12, userId=6), PcSystem(id=12, userId=5), PcSystem(id=12, userId=4)], 
11=[PcSystem(id=11, userId=3), PcSystem(id=11, userId=2), PcSystem(id=11, userId=1)]}
输出结果3
{11=[PcSystem(id=11, userId=3), PcSystem(id=11, userId=2), PcSystem(id=11, userId=1)], 
13=[PcSystem(id=13, userId=9), PcSystem(id=13, userId=8), PcSystem(id=13, userId=7), 
PcSystem(id=13, userId=10)], 
12=[PcSystem(id=12, userId=6), PcSystem(id=12, userId=5), PcSystem(id=12, userId=4)]}
输出结果4
{13=[PcSystem(id=13, userId=9), PcSystem(id=13, userId=8), PcSystem(id=13, userId=7),PcSystem(id=13, userId=10)], 
12=[PcSystem(id=12, userId=6), PcSystem(id=12, userId=5), PcSystem(id=12, userId=4)], 
11=[PcSystem(id=11, userId=3), PcSystem(id=11, userId=2), PcSystem(id=11, userId=1)]}

仅为笔记!仅为笔记!仅为笔记!仅为笔记!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值