I have an Object Class called Record that consists of a Vector of Data Class objects
Object Class Data has nothing but two variables
Object value;
String name;
I override the equals method in the Record class as follows:
public boolean equals(Object obj) {
boolean check = true;
for(int i=0; i<this.coloumnsOfData.size();i++) {
System.out.println( ((Record) obj).coloumnsOfData.get(i).name + " OBJECT " + ((Record) obj).coloumnsOfData.get(i).value );
System.out.println( coloumnsOfData.get(i).name + " THIS " + coloumnsOfData.get(i).value );
if( !((((Record) obj).coloumnsOfData.get(i).name).equals(this.coloumnsOfData.get(i).name)) || !((((Record) obj).coloumnsOfData.get(i).value).equals(this.coloumnsOfData.get(i).value))) {
check=false;
}
}
return (obj instanceof Record && check);
}
I initialize HashSet as follows:
Set<Record> answer= new HashSet<Record>()
and start testing
Record r1 = new Record();
r1.coloumnsOfData.add(new Data(new Double( 1.5 ),"gpa"));
r1.coloumnsOfData.add(new Data(new String("John"),"name"));
r1.coloumnsOfData.add(new Data(new Integer( 2 ),"id"));
Record r2 = new Record();
r2.coloumnsOfData.add(new Data(new Double( 1.5 ),"gpa"));
r2.coloumnsOfData.add(new Data(new String("John"),"name"));
r2.coloumnsOfData.add(new Data(new Integer( 2 ),"id"));
System.out.println(r1.equals(r2)); //RETURNS TRUE
answer.add(r1);
System.out.println(answer.contains(r2)); //RETURNS FALSE
Any help understanding where the issue is would be greatly appreciated.