• 根据规则去掉List 对象数组中的重复元素


    package container.main;
    
    import java.util.ArrayList;
    import java.util.Comparator;
    import java.util.List;
    import java.util.Set;
    import java.util.TreeSet;
    
    import container.bean.GxyJobEntity;
    
    public class RemoveSame {
    	public static void main(String[] args) {
    		List<GxyJobEntity> list = new ArrayList<>();
    		GxyJobEntity gxyJobEntity1  = new GxyJobEntity();
    		gxyJobEntity1.setUserId("user001");
    		gxyJobEntity1.setPlanId("plan001");
    		gxyJobEntity1.setStudentId("stu001");
    		
    		GxyJobEntity gxyJobEntity2  = new GxyJobEntity();
    		gxyJobEntity2.setUserId("user002");
    		gxyJobEntity2.setPlanId("plan002");
    		gxyJobEntity2.setStudentId("stu002");
    		
    		GxyJobEntity gxyJobEntity3  = new GxyJobEntity();
    		gxyJobEntity3.setUserId("user003");
    		gxyJobEntity3.setPlanId("plan001");
    		gxyJobEntity3.setStudentId("stu001");
    		
    		list.add(gxyJobEntity1);  list.add(gxyJobEntity2);  list.add(gxyJobEntity3);
    		
    		List<GxyJobEntity> list1 = removeDuplicate(list);
    		System.out.println(list);
    		System.out.println(list1);
    	}
    	
        private static List<GxyJobEntity> removeDuplicate(List<GxyJobEntity> list) {
            Set<GxyJobEntity> set = new TreeSet<GxyJobEntity>(new Comparator<GxyJobEntity>() {
                public int compare(GxyJobEntity a, GxyJobEntity b) {
                	System.out.println("a:" + a);
                	System.out.println("b:" + b);
                	
                    // 字符串则按照asicc码升序排列
                    if ( a.getStudentId().equals(b.getStudentId()) && 
                    		a.getPlanId().equals(b.getPlanId()) ) {
                    	return 0;
                    } else {
                    	return 1;
                    }
                }
            });
            System.out.println("set:" + set.size() + "-->" + set);
            set.addAll(list);
            System.out.println("set:" + set.size() + "-->" + set);
            return new ArrayList<GxyJobEntity>(set);
        }
    
    }  

    如果 对象 GxyJobEntity 中两个属性  studentId 和 planId 相等,则保留一个,去掉多余的。

    输出:

    set:0-->[]
    a:GxyJobEntity [jobId=null, studentId=stu001, planId=plan001, userId=user001]
    b:GxyJobEntity [jobId=null, studentId=stu001, planId=plan001, userId=user001]
    a:GxyJobEntity [jobId=null, studentId=stu002, planId=plan002, userId=user002]
    b:GxyJobEntity [jobId=null, studentId=stu001, planId=plan001, userId=user001]
    a:GxyJobEntity [jobId=null, studentId=stu001, planId=plan001, userId=user003]
    b:GxyJobEntity [jobId=null, studentId=stu001, planId=plan001, userId=user001]
    set:2-->[GxyJobEntity [jobId=null, studentId=stu001, planId=plan001, userId=user001], GxyJobEntity [jobId=null, studentId=stu002, planId=plan002, userId=user002]]
    [GxyJobEntity [jobId=null, studentId=stu001, planId=plan001, userId=user001], GxyJobEntity [jobId=null, studentId=stu002, planId=plan002, userId=user002], GxyJobEntity [jobId=null, studentId=stu001, planId=plan001, userId=user003]]
    [GxyJobEntity [jobId=null, studentId=stu001, planId=plan001, userId=user001], GxyJobEntity [jobId=null, studentId=stu002, planId=plan002, userId=user002]]

  • 相关阅读:
    python中常用的数据类型之整型(int),浮点型(float), 布尔值(bool), 复数(complex)
    requests库的基础使用
    socket,urllib,urllib3,request多种方法请求网页首页
    nginx日志切割
    gitlab社区版安装
    批量修改文件编码
    lvm磁盘扩展及添加磁盘lvm分区
    函数
    集合
    数据类型练习题
  • 原文地址:https://www.cnblogs.com/z360519549/p/11497287.html
Copyright © 2020-2023  润新知