当前位置: 首页 > 技术研究 > Java > java使用Set为对象bean去重

java使用Set为对象bean去重

发布于:2020-3-21 Java 0条评论 2,795 views
欢迎光临小站,愿能为您提供帮助与启发,热爱分享、享受分享、乐于分享,让我们携手共同进步。
最近需要用到对一个对象集合序列去重,而且去重是根据这个对象里面的两个字段,以下是代码可供参考,其实很简单,通过重写实体的hashCode和equals即可实现,jdk1.8之后set的底层实现方式也是通过hashMap,所以采用此方法来处理即可,备注留用!

@Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; if (obj instanceof HttpMtInfo) { HttpMtInfo other = (HttpMtInfo) obj; if (content == null) { if (other.content != null) return false; } else if (!content.equals(other.content)) return false; if (destid == null) { if (other.destid != null) return false; } else if (!destid.equals(other.destid)) return false; // 比较每个属性的值 一致时才返回true if (other.content.equals(this.content) && other.destid.equals(this.destid)) return true; } return true; } /** * 重写hashcode 方法,返回的hashCode不一样才再去比较每个属性的值 */ @Override public int hashCode() { return content.hashCode() * destid.hashCode(); }

测试代码:


public static void main(String[] args) { HttpMtInfo httpMtInfo = new HttpMtInfo(); httpMtInfo.setUsercode("wkl"); httpMtInfo.setPassword("324"); httpMtInfo.setMsgid("dafasdfasdfads"); httpMtInfo.setContent("测试1"); httpMtInfo.setDestid("15210086628"); HttpMtInfo httpMtInfo1 = new HttpMtInfo(); httpMtInfo1.setUsercode("wklds"); httpMtInfo1.setPassword("324"); httpMtInfo1.setMsgid("dafasdfasdfads"); httpMtInfo1.setContent("测试2"); httpMtInfo1.setDestid("15210086628"); HttpMtInfo httpMtInfo2 = new HttpMtInfo(); httpMtInfo2.setUsercode("wklds"); httpMtInfo2.setPassword("324"); httpMtInfo2.setMsgid("dafasdfasdfads"); httpMtInfo2.setContent("测试3"); httpMtInfo2.setDestid("18401555228"); HttpMtInfo httpMtInfo3 = new HttpMtInfo(); httpMtInfo3.setUsercode("wklds"); httpMtInfo3.setPassword("324"); httpMtInfo3.setMsgid("dafasdfasdfads"); httpMtInfo3.setContent("测试3"); httpMtInfo3.setDestid("18401555228"); HttpMtInfo httpMtInfo4 = new HttpMtInfo(); httpMtInfo4.setUsercode("wklds"); httpMtInfo4.setPassword("324"); httpMtInfo4.setMsgid("dafasdfasdfads"); httpMtInfo4.setContent("测试5"); httpMtInfo4.setDestid("18401553228"); Set<HttpMtInfo> sets = new HashSet<>(); sets.add(httpMtInfo); sets.add(httpMtInfo1); sets.add(httpMtInfo2); sets.add(httpMtInfo3); sets.add(httpMtInfo4); for (HttpMtInfo item : sets) { System.out.println(item.toString()); } }

输出结果:

HttpMtInfo [msgid=dafasdfasdfads, servicecodeadd=null, destid=18401553228, datacode=null, contentlen=null, content=测试5, usercode=wklds, password=324, serverid=null, senddate=null]
HttpMtInfo [msgid=dafasdfasdfads, servicecodeadd=null, destid=15210086628, datacode=null, contentlen=null, content=测试2, usercode=wklds, password=324, serverid=null, senddate=null]
HttpMtInfo [msgid=dafasdfasdfads, servicecodeadd=null, destid=15210086628, datacode=null, contentlen=null, content=测试1, usercode=wkl, password=324, serverid=null, senddate=null]
HttpMtInfo [msgid=dafasdfasdfads, servicecodeadd=null, destid=18401555228, datacode=null, contentlen=null, content=测试3, usercode=wklds, password=324, serverid=null, senddate=null]

顺带着从别的地方看到的:

在阿里巴巴Java开发是手册的集合处理中需要强制遵循如下规则:
1)只要重写equals,就必须重写hashCode;
2)因为Set存储的是不重复的对象,依据hashCode和equals进行判断,所以Set存储的对象必须重写这两个方法。
3)如果自定义对象做为Map的键,那么必须重写hashCode和equals。
正例:String重写了hashCode和equals方法,所以我们可以方便的使用String对象作为key来使用。

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据