Java实现list根据实体类的某个属性去重,保留实体类属性最多的

public static void main(String[] args) throws IllegalAccessException {
        List<PaperItemVo> paperItemVos = new ArrayList<>();
        PaperItemVo paperItemVo = new PaperItemVo();
        paperItemVo.setPaperName("营业执照");
        PaperItemVo paperItemVo1 = new PaperItemVo();
        paperItemVo1.setPaperName("营业执照");
        paperItemVo1.setAgency("12321");
        paperItemVo1.setAmount("234234");
        PaperItemVo paperItemVo2 = new PaperItemVo();
        paperItemVo2.setPaperName("营业执照");
        paperItemVo2.setAgency("12321");
        paperItemVo2.setAmount("234234");
        paperItemVo2.setDiscipline("shjkdfhsdkjf");
        paperItemVo2.setGrade("hsijfjsdk");

        paperItemVos.add(paperItemVo);
        paperItemVos.add(paperItemVo1);
        paperItemVos.add(paperItemVo2);

        Map<String, List<PaperItemVo>> groupByPaperName = paperItemVos.stream().collect(Collectors.groupingBy(PaperItemVo::getPaperName));
        List<PaperItemVo> list = filterList(groupByPaperName, PaperItemVo.class);
        list.forEach(System.out::println);
    }

/**
     * 根据实体类的某个属性去重,保留去重的实体类属性不为空最多的类
     * 如果集合中的对象全是满属性,默认返回第一个
     * @param groupByPaperName 已近通过某个属性分组的集合
     * @param classType 类的字节码对象
     * @param <T> 泛型
     * @return 过滤后的集合
     */
    public static <T> List<T> filterList(Map<String, List<T>> groupByPaperName, Class<T> classType) {
    	if (groupByPaperName == null || groupByPaperName.size() == 0){
            return null;
        }
        List<T> filterList = new ArrayList<>();
        Field[] fields = classType.getDeclaredFields();
        int oldNotNullFieldNum = 0;
        int thisNotNullFieldNum = 0;
        for (String paperName : groupByPaperName.keySet()) {
            List<T> paperItemVos = groupByPaperName.get(paperName);
            T paperItemVo = paperItemVos.get(0);
            for (Field field : fields) {
                Object fieldValue = ReflectUtil.getFieldValue(paperItemVo, field);
                if (Objects.nonNull(fieldValue)) {
                    oldNotNullFieldNum ++;
                }
            }
            for (T itemVo : paperItemVos) {
                for (Field field : fields) {
                    Object fieldValue = ReflectUtil.getFieldValue(itemVo, field);
                    if (Objects.nonNull(fieldValue)) {
                        thisNotNullFieldNum ++;
                    }
                }
                if (thisNotNullFieldNum > oldNotNullFieldNum) {
                    oldNotNullFieldNum = thisNotNullFieldNum;
                    paperItemVo = itemVo;
                }
                //重置
                thisNotNullFieldNum = 0;
            }
            filterList.add(paperItemVo);
            //重置
            oldNotNullFieldNum = 0;
        }
        return filterList;
    }

运行效果:
在这里插入图片描述

解释一下这段代码:public R autoSort2(@RequestParam Map<String, Object> params,CaipinxinxiEntity caipinxinxi, HttpServletRequest request){ String userId = request.getSession().getAttribute("userId").toString(); String goodtypeColumn = "caipinfenlei"; List<OrdersEntity> orders = ordersService.selectList(new EntityWrapper<OrdersEntity>().eq("userid", userId).eq("tablename", "caipinxinxi").orderBy("addtime", false)); List<String> goodtypes = new ArrayList<String>(); Integer limit = params.get("limit")==null?10:Integer.parseInt(params.get("limit").toString()); List<CaipinxinxiEntity> caipinxinxiList = new ArrayList<CaipinxinxiEntity>();// List<OrdersEntity> ordersDist = new ArrayList<OrdersEntity>(); for(OrdersEntity o1 : orders) { boolean addFlag = true; for(OrdersEntity o2 : ordersDist) { if(o1.getGoodid()==o2.getGoodid() || o1.getGoodtype().equals(o2.getGoodtype())) { addFlag = false; break; } } if(addFlag) ordersDist.add(o1); } if(ordersDist!=null && ordersDist.size()>0) { for(OrdersEntity o : ordersDist) { caipinxinxiList.addAll(caipinxinxiService.selectList(new EntityWrapper<CaipinxinxiEntity>().eq(goodtypeColumn, o.getGoodtype()))); } } EntityWrapper<CaipinxinxiEntity> ew = new EntityWrapper<CaipinxinxiEntity>(); params.put("sort", "id"); params.put("order", "desc"); //调用caipinxinxi对象的queryPage方法 PageUtils page = caipinxinxiService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, caipinxinxi), params), params)); List<CaipinxinxiEntity> pageList = (List<CaipinxinxiEntity>)page.getList(); if(caipinxinxiList.size()<limit) { int toAddNum = (limit-caipinxinxiList.size())<=pageList.size()?(limit-caipinxinxiList.size()):pageList.size(); for(CaipinxinxiEntity o1 : pageList) { boolean addFlag = true; for(CaipinxinxiEntity o2 : caipinxinxiList) { if(o1.getId().intValue()==o2.getId().intValue()) { addFlag = false; break; } } if(addFlag) { caipinxinxiList.add(o1); if(--toAddNum==0) break; } } } page.setList(caipinxinxiList); return R.ok().put("data", page); }
04-24
, && ordersDist.size()>0) {//判断是不是空集合 for(OrdersEntity o : ordersDist) { String goodTypeValue = DictUtils.getDictLabel(o.getGoodtype(), goodtypeColumn); if(goodTypeValue!=null && !"".equals(goodTypeValue) && !goodtypes.contains(goodTypeValue)) { goodtypes.add(goodTypeValue); } } //根据商品分类查询商品信息 caipinxinxi.setCaipinfenleiList(goodtypes); caipinxinxi.setLimit(limit); caipinxinxiList = caipinxinxiService.selectList(caipinxinxi); } return R.ok().put("data", caipinxinxiList); } 这段代码是一个Java方法,名字为autoSort2,它是一个Controller中的一个方法,作用是根据用户的订单信息进行商品分类并返回相关商品的信息。 该方法有三个参数:params、caipinxinxi和request。其中,params是一个Map类型的参数,用于存储一些请求参数;caipinxinxi是一个实体类,用于封装查询条件;request是一个HttpServletRequest类型的参数,用于获当前用户的ID。 该方法的执行流程如下: 1.从session中获当前用户的ID。 2.定义一个String类型的变量goodtypeColumn,用于指定商品分类对应的字典表。 3.根据用户ID从订单表中查询该用户的所有订单,按照下单时间倒序排列,并将结果保存在orders集合中。 4.定义一个空的List集合goodtypes,用于保存所有不复的商品分类。 5.定义一个整型变量limit,用于指定最多返回的商品数目。 6.定义一个空的List集合caipinxinxiList,用于保存查询到的商品信息,并进行处理。 7.定义一个空的List集合ordersDist,用于保存后的订单信息。 8.遍历orders集合中的每个订单,判断该订单对应的商品是否已经存在于ordersDist集合中,如果存在则不做处理,否则将该订单添加到ordersDist集合中。 9.遍历ordersDist集合中的每个订单,获该订单的商品分类,并将其添加到goodtypes集合中(确保不复)。 10.根据商品分类查询商品信息,并将结果保存到caipinxinxiList集合中。 11.返回caipinxinxiList集合作为查询结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值