Trying to understand the results I am getting from object equality …
Ok first let me describe the problem
need to filter duplicates objects from two two tables from db (Hibenate all of the cluses are set up) preset condition is 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;
}
}
So in my service class I call Query on 2 tables and create object list Iterate over it
In one of the list creat a nested iteration anainst second list
So in my service class I call Query on 2 tables and create custom list Iterate over list and objects
So in my service class I call Query on 2 tables and create custom list Iterate over list and objects
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());
So here is a part I dont undestand i implement equality method on the object but the equality returnus one or two matches…
2012-09-01 18:49:09,910 INFO [com.equality.service.CopareToServices] – (thread 1008 invoke CopareToServices.compareObjects)
Equality method only found a single duplicate record >>>
But when at the end I implement removeAll on the list the results
And this is after Implementing removeAll on the heshSet
2012-09-01 18:49:09,918 INFO [com.equality.service.CopareToServices] – (thread 1008 invoke CopareToServices.compareObjects)
171 Duplicates were removed which sound about right…
How come implementing removeAll on the list filters the list properly but equality method does not work
Tryed equals builder with apache commons too with the same output
Will Apriciate any help i can get
Sincerily
If I understand well, I think the problem is:
because this is like testing only the last element of
res1.EDIT: proposal for correction:
see List#contains(Object)