java 对List集合中元素对象按字段分组,并收集指定字段的值

一、实现:对已有对象集合List<Persion> ,需要获取Persion对象的字段 name分组, 并对年龄age字段值做收集

二、字段分组收集方法

 注:由于实际业务只有String类型跟数字类型,所以只对String跟Object两种类型判空


    /**
     * 分组并字段收集
     *
     * @param list
     * @param groupFunction  类型现在只能为 String or Object
     * @param getFiledFunction
     * @return
     */
    public static <T, K, V> Map<K, Set<V>> groupAndCollectionField(List<T> list, Function<T, K> groupFunction, Function<T, V> getFiledFunction) {
        if (Objects.isNull(list)) {
            return new HashMap<>();
        }
        //按寄收类型分组, 并收集区号(机场)
        Map<K, Set<V>> setMap = list.stream().filter(r-> {
                    K groupValue = groupFunction.apply(r);
                    V filedValue = getFiledFunction.apply(r);
                    //分组判空
                    boolean groupNull = Objects.isNull(groupValue);
                    if (!groupNull && groupValue instanceof String) {
                        groupNull = ((String) groupValue).length() == 0;
                    }
                    //字段判空
                    boolean filedNull = Objects.isNull(filedValue);
                    if (filedValue instanceof String) {
                        filedNull = ((String) filedValue).length() == 0;
                    }
                    //分组非空 and 字段非空 返回true; 否则返回false
                    return !groupNull && !filedNull;
                })
                .collect(Collectors.groupingBy(groupFunction, Collectors.mapping(getFiledFunction, Collectors.toSet())));
        return setMap;
    }

三、测试代码

        //场景1
        System.out.println("场景1");
        List<Persion> persionList = new ArrayList<>();
        persionList.add(new Persion(1, "李二", null));
        persionList.add(new Persion(2, null, 30));
        persionList.add(new Persion(3, "王五", 15));
        persionList.add(new Persion(4, "陈十一", 11));
        //分组并收集字段
        Map<String, Set<Integer>> setMap = Main.groupAndCollectionField(persionList, Persion::getName, Persion::getAge);
        //遍历
        setMap.entrySet().stream().forEach(r-> System.out.println(String.format("name:%s; age:%s",r.getKey(),r.getValue())));

        System.out.println("");
        
        //场景2
        System.out.println("场景2");
        persionList = new ArrayList<>();
        persionList.add(new Persion(1, "李二", 22));
        persionList.add(new Persion(2, "李二", 30));
        persionList.add(new Persion(3, "王五", 15));
        persionList.add(new Persion(4, "陈十一", 11));
        //分组并收集字段
        setMap = Main.groupAndCollectionField(persionList, Persion::getName, Persion::getAge);
        //遍历
        setMap.entrySet().stream().forEach(r-> System.out.println(String.format("name:%s; age:%s",r.getKey(),r.getValue())));
    }

四、结果

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值