Comparing the Objects of two lists

Hi guys,

I have a list full of objects. I make changes to those objects and I want to know which objects I have changed. My solution was to have a duplicate list whose objects never change and to compare that to the list whose objects do change. However I am having trouble comparing the objects in each list.

I create the objects and the lists to hold the objects and then I add the objects to the lists;

Test1 test1a =new Test1();

Test1 test1b =new Test1();

List list =new ArrayList();

List compareList =new ArrayList();

list.add(test1a);

compareList.add(test1b);

at first I thought I could simply compare the two using the .equals() method. However this simply compares the address of the objects and so would always come out as different;

System.out.println("Comparing objects straight from the lists");

for(int i=0; i<list.size(); i++){

if(list.get(i).equals.(compareList.get(i))){

System.out.println("The objects "+list.get(i)+" and "+compareList.get(i)+" are the same");

}

else{

System.out.println("Objects "+list.get(i)+" and "+compareList.get(i)+" are completely different");

}

}

then I thought I could use the "==" method, but this doesn't seem to work either. Basically I want to be able to check whether two objects are the same or not. Not whether they both reside in the same memory location. Is there a way of doing this?

Message was edited by:

DontKnowJack>

[2115 byte] By [DontKnowJacka] at [2007-11-27 3:57:12]
# 1

Have the Test1 class override the equals() method - that is, provide your own interpretation of what "equals" means rather than inheriting the default equals() of Object which, as you've seen, just compares the reference values.

Overriding equals() is discussed here: http://www.javaworld.com/javaworld/jw-06-2004/jw-0614-equals.html

pbrockway2a at 2007-7-12 9:01:31 > top of Java-index,Java Essentials,New To Java...
# 2
Well, how about overriding equals() for your objects then, to do what you want it to do?
CeciNEstPasUnProgrammeura at 2007-7-12 9:01:31 > top of Java-index,Java Essentials,New To Java...
# 3

I made a quick fix for this, although I dont know if this is the best way to go about doings this. I made a method that takes two Test1 objects as arguments. It compares the attributes of the two objects, returns true is they are the same and false if they are not. My code is shown below;

System.out.println("Comparing objects straight from the lists");

for(int i=0; i<list.size(); i++){

if(compare((Test1) list.get(i),(Test1) compareList.get(i))){

System.out.println("The objects "+list.get(i)+" and "+compareList.get(i)+" are the same");

}

else{

System.out.println("Objects "+list.get(i)+" and "+compareList.get(i)+" are completely different");

}

}

}

public static boolean compare(Test1 test1, Test1 test2){

if(test1.getVar() == test2.getVar()){

return true;

}

else{

return false;

}

}

this seems to give me the desired result. However is there a better way of doing this? or is this the standard way to check if two objects are the same? Any advice would be welcome.

Message was edited by:

DontKnowJack>

DontKnowJacka at 2007-7-12 9:01:31 > top of Java-index,Java Essentials,New To Java...
# 4

> However is there a better way of doing this? or is this the standard way to check if

> two objects are the same?

The standard way - using equals() - plays nicely with Java's Collections framework. Some type like Set, and algorithms like frequency() depend on there being a method called equals() that reports on object equality.

(But that's not to say you can't use your own compare() for certain uses and intend, by design, that it not be used as the basis for forming a Set<Test1>.)

pbrockway2a at 2007-7-12 9:01:31 > top of Java-index,Java Essentials,New To Java...
# 5
If it's okay to compare var for identity (==) instead of equality (equals)...There is no standard way to check whether two objects are equal, because equality needs to be defined for each class.
CeciNEstPasUnProgrammeura at 2007-7-12 9:01:31 > top of Java-index,Java Essentials,New To Java...
# 6
Thank for you help and advice. :)
DontKnowJacka at 2007-7-12 9:01:31 > top of Java-index,Java Essentials,New To Java...