List.stream 归组排序,在一个限制层数的递归中使用,count

 private List<AntdTree> getBanJiTree(List<Map<String, String>> maps, int level
            , List<Map<String, String>> childList
            , AntdTree treeNode) {
        List<AntdTree> resultList = new LinkedList<>();
        String idName = "yxid";
        String mcName = "yxmc";
        String type = "xy";
        String nextName = "zymc";
        switch (level) {
            case 1:
                idName = "yxid";
                mcName = "yxmc";
                type = "xy";
                nextName = "zymc";
                break;
            case 2:
                idName = "zyid";
                mcName = "zymc";
                type = "zy";
                nextName = "njmc";
                break;
            case 3:
                idName = "njid";
                mcName = "njmc";
                type = "nj";
                nextName = "bjmc";
                break;
            case 4:
                idName = "bjid";
                mcName = "bjmc";
                type = "bj";
                nextName = "bjmc";
                break;

        }

        final String groupIdName = idName;
        final String sortName = nextName;

        Map<String, List<Map<String, String>>> groupList = null;

        //归组,保持原顺序
        if (level == 1) {
            groupList = maps.stream()
                    .collect(Collectors.groupingBy(f -> f.get(groupIdName), LinkedHashMap::new, Collectors.toList()));
        } else {
            groupList = childList.stream()
                    .collect(Collectors.groupingBy(f -> f.get(groupIdName), LinkedHashMap::new, Collectors.toList()));
        }

        for (Map.Entry<String, List<Map<String, String>>> entry :
                groupList.entrySet()) {
            AntdTree node = new AntdTree();
            node.setKey(entry.getKey());
            node.setTitle(maps.stream().filter(f -> f.get(groupIdName).equals(entry.getKey())).findFirst().get().get(mcName));
            node.setType(type);

            if (level < 4) {
                node.setChildren(new LinkedList<>());
                childList = entry.getValue();
                //排序
                List<Map<String, String>> collect = childList.stream()
                        .sorted(Comparator.comparing(item -> item.get(sortName)))
                        .collect(Collectors.toList());
                getBanJiTree(maps, level + 1, collect, node);
            }

            if (level == 1) {
                resultList.add(node);
            } else {
                treeNode.getChildren().add(node);
            }
        }
        return resultList;
    }

使用

 List<Map<String, String>> maps = banJiService.listAll(); 
        //排序
        List<Map<String, String>> collect = maps.stream()
                .sorted(Comparator.comparing(item -> item.get("yxmc")))
                .collect(Collectors.toList());

        List<AntdTree> banJiTree = getBanJiTree(collect, 1, null, null);

实现了一个将学院,专业,年级,班级组合起来的树,每个层级都按名称进行升序排列

归组统计:

 Map<String, Long> collect = list.stream().collect(Collectors.groupingBy(EnquirySign::getPublishId, Collectors.counting()));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值