Java用stream根据多字段去重及获取重复数据

时间:2025-04-04 15:31:54

1、多条件去重 

        List<user> infoList = new ArrayList<>();
        //实体类  userName  orderNumber   age
        (new user("11111", "xc", "22"));
        (new user("11111", "xc", "33"));
        (new user("11111", "xy", "22"));
        (new user("22222", "xc", "23"));
        (new user("22222", "xc", "33"));
        (new user("22222", "xy", "22"));
        List<user> list = ()
                .collect(((() -> new TreeSet<>(
                        (f -> () + ()))), ArrayList::new));
        (list);

2、多条件判断是否存在重复数据

 List<SupplierPerformanceBillHead> list = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            SupplierPerformanceBillHead bean = new SupplierPerformanceBillHead();
            ((i));
            if (i==2){
                ((i-1));
                ("测试");//这里修改,会出现不重复
            }else {
                ("测试");
            }
            ("www");
            (bean);
        }
        for (SupplierPerformanceBillHead head : list) {
            (head);
        }

        // 使用stream和Lambda表达式进行分组和过滤
        Map<String, Long> duplicates = ()
                .collect((person -> () + "_" + ()+ "_" + (), ()))
                .entrySet().stream()
                .filter(entry -> () > 1)
                .collect((::getKey, ::getValue));
       if (()){
           ("没有重复数据");
       }else {
           ("存在重复数据");
       }
        // 打印重复的数据
        ((key, value) -> ("重复字符组: " + key + ", 条数: " + value));