试图理解我从对象相等中得到的结果......好吧,首先让我描述需要从数据库中的两个两个表中过滤重复对象的问题(Hibenate所有的cluses都已设置)预设条件是firstName,lastName,dateOfbirth
package com.equality.types;
import com.google.common.base.Objects;
import java.util.Date;
public class AsignoreTypes{
private String firstname;
private String lastname;
private Date doa;
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public Date getDoa() {
return doa;
}
public void setDoa(Date doa) {
this.doa = doa;
}
/* @Override
public boolean equals(Object obj) {
if (obj instanceof AsignoreTypes == false) {
return false;
}
if (this == obj) {
return true;
}
AsignoreTypes other = (AsignoreTypes) obj;
return new EqualsBuilder().append(this.firstname , other.firstname)
.append(this.lastname, other.lastname)
.append(this.doa , other.doa).isEquals();
}
@Override
public int hashCode() {
return new HashCodeBuilder().append(this.firstname)
.append(this.lastname)
.append(this.doa)
.hashCode();
}
*/
@Override
public int hashCode(){
return Objects.hashCode(firstname, lastname, doa);
}
@Override
public boolean equals(final Object obj){
if(obj instanceof AsignoreTypes){
final AsignoreTypes other = (AsignoreTypes) obj;
return Objects.equal(firstname, other.firstname)
&&Objects.equal(lastname, other.lastname)
&& Objects.equal(doa, other.doa);
} else{
return false;
}
}
所以在我的服务类中,我在 2 个表上调用 Query 并创建对象列表迭代它
在其中一个列表中创建一个嵌套迭代和第二个列表
所以在我的服务类中,我在 2 个表上调用 Query 并创建自定义列表迭代列表和对象
所以在我的服务类中,我在 2 个表上调用 Query 并创建自定义列表迭代列表和对象
Session session = service.getDataServiceManager().getSession();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Query query = session.createQuery("select upper(ass.firstName), upper(ass.lastName), ass.doa from AssignorInfo ass");
List < Object > ass1 = null;
ass1 = query.list();
List < AsignoreTypes > res1 = null;
res1 = new ArrayList < AsignoreTypes > ();
Iterator < Object > it = ass1.iterator();
while (it.hasNext()) {
Object[] row = (Object[]) it.next();
AsignoreTypes ass = new AsignoreTypes();
if (row[0] != null) ass.setFirstname(row[0].toString());
if (row[1] != null) ass.setLastname(row[1].toString());
if (row[2] != null) ass.setDoa(formatter.parse(row[2].toString()));
res1.add(ass);
}
log(INFO, "List one size " + res1.size());
Query q = session.createQuery("select upper(g.fname), upper(g.lname), g.doa from GeneralInfo g where g.arbitrator like '%Bulel%' ");
List < Object > ass2 = null;
ass2 = q.list();
log(INFO, " Size from wizard " + ass2.size());
List < AsignoreTypes > res2 = null;
res2 = new ArrayList < AsignoreTypes > ();
Iterator < Object > iterator = ass2.iterator();
while (iterator.hasNext()) {
Boolean f = null;
Object[] row = (Object[]) iterator.next();
AsignoreTypes ass = new AsignoreTypes();
if (row[0] != null) ass.setFirstname(row[0].toString());
if (row[1] != null) ass.setLastname(row[1].toString());
if (row[2] != null) ass.setDoa(formatter.parse(row[2].toString()));
for (AsignoreTypes a1: res1) {
f = ass.equals(a1);
}
if (f == false) {
res2.add(ass);
log(INFO, "Got matchig element " + ass.getLastname());
}
}
log(INFO, "List tow size " + res2.size() + " List hash set size ");
HashSet < AsignoreTypes > asigors = new HashSet < AsignoreTypes > ();
HashSet < AsignoreTypes > wiz = new HashSet < AsignoreTypes > ();
asigors.addAll(res1);
wiz.addAll(res2);
wiz.removeAll(asigors);
log(INFO, "Added to hash set " + asigors.size());
log(INFO, "Added assignors to the list " + wiz.size());
所以这是我不理解的部分,我在对象上实现了相等方法,但相等返回一两个匹配项... 2012-09-01 18:49:09,910 INFO [com.equality.service.CopareToServices] - (线程 1008调用 CopareToServices.compareObjects) Equality 方法只找到一个重复记录 >>>
但是当最后我在列表上实现 removeAll 时,结果
这是在 heshSet 2012-09-01 18:49:09,918 INFO [com.equality.service.CopareToServices] 上实施 removeAll 之后 - (线程 1008 调用 CopareToServices.compareObjects)
删除了 171 个听起来正确的重复项...
如何在列表上实现 removeAll 正确过滤列表但相等方法不起作用
尝试使用 apache commons 的 equals builder 也具有相同的输出
真诚地感谢我能得到的任何帮助