根据规则去掉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]]

转载于:https://www.cnblogs.com/z360519549/p/11497287.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值