Java8 stream 之groupingBy() 分组排序

	List<Matchs> matchsList = new ArrayList();
	
	Map<String,List<Matchs>> MatchsListMap=matchsList.stream()
	.collect(Collectors.groupingBy(Matchs::getMatchDate));	

此时MatchsListMap的排序规则是根据Hash值排序一般来讲认为是无序即可,那么如果需要根据MatchDate升/降序该怎么办呢?

答案是:


TreeMap<String, List<Matchs>> matchsListMap = matchsList.stream()
.collect(Collectors.groupingBy(Matchs::getMatchDate,TreeMap::new,Collectors.toList()));

此时返回的为TreeMap类型数据,TreeMap默认为按照key升序,matchsListMap.descendingMap()可以降序输出

2021年1月4日 19:53:19
补充另一种修改比较器的写法

List<Integer> resultList = IntStream.rangeClosed(1, 24).map(operand -> 0).boxed().collect(Collectors.toList());
        resultList.set(1,9);
        resultList.set(3,3);
        resultList.set(7,22);
        TreeMap<Integer, List<Integer>> collect = resultList.stream()
                .collect(Collectors.groupingBy(Function.identity(), () -> {
                    Comparator<Integer> comparing = Comparator.comparing(Function.identity());
                    return new TreeMap<>(comparing.reversed());
                }, Collectors.toList()));
  • 12
    点赞
  • 56
    收藏
    觉得还不错? 一键收藏
  • 19
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值