Raghu
Raghu

Reputation: 1161

Object comparison for equality : JAVA

public ClassA
{
private String firstId;
private String secondId;

public void setFirstId(String firstId) {
           this.firstId = firstId;
   }


   public String getFirstId() {
           return id;
   }

   public void setSecondId(String secondId) {
           this.secondId = secondId;
   }


   public String getSecondId() {
           return secondId;
   }
}



public ClassB
{
private String firstId;
private String secondId;


   public void setFirstId(String firstId) {
           this.firstId = firstId;
   }


   public String getFirstId() {
           return id;
   }

   public void setSecondId(String secondId) {
           this.secondId = secondId;
   }

   public String getSecondId() {
           return secondId;
   }
}

I have the above 2 classes(POJOs) which are absolutely the same (except for the name of course) which I am adding to two arraylists: aListA and aListB. I need to compare if the two objects are the same. If they are same I need to add them to another list(commonList) and if they happen to be the different, I need to add them to another list(differentList). I have written the following code:

ClassA clsA = null;
public ArrayList genCommonList(aList, bList)
{

for(int i = 1; i<aList.size(); i++)
{
clsA = new ClassA;
Object obj = aList.get(i);
clsA = (ClassA)obj;
if(bList.contains(clsA)
{
genCommonList.add(clsA);
}
}

public ArrayList genDifferentList(aList, bList)
{

for(int i = 1; i<aList.size(); i++)
{
clsA = new ClassA;
Object obj = aList.get(i);
clsA = (ClassA)obj;
if(!bList.contains(clsA)
{
 genDifferentList.add(clsA);
}
}

My problem is that even when the data(both variables, firstId and secondID) in the 2 different POJOs are the same, they get put in the different list. That means the objects are not being compared for equality using the contains() method of the ArrayList class. I dont want to get the data from each variable inside the POJO and compare. Is there a way to compare the POJOs for both variables and be determined as either equal or unequal?

Upvotes: 2

Views: 4028

Answers (4)

Kuldeep Jain
Kuldeep Jain

Reputation: 8598

What you are trying to do here in not correct. The 2 objects that you are comparing are not only different objects but are also of different types (ClassA and ClassB) which have no relational at all.

Moreover if you try to compare like this

if(bList.contains(clsA))

As your bList contains objects of type ClassB and clsA is of type ClassA, they will be compared using java.lang.Object class's equals() method. So x.equals(y) returns true if and only if x and y refer to the same object (x == y has the value true).

Upvotes: 0

kosa
kosa

Reputation: 66637

Eventhough content is same they are different objects which doesn't satifsy equal condition. You may need to do explicit a.getfirstId().equals(b.getfirsId()) and a.getscondID().equals(b.getSecondId()) by iterating.

The other possibility may be overriding equals and hashcode methods in both POJO classes. Refer this link for equals/hashcode contract.

Upvotes: 1

ptyx
ptyx

Reputation: 4164

With the default implementation of Object.equals, two objects of different classes will never be the same.

If you want that behavior, you will need to override .equals in your classes (and .hashcode as well, look at the javadoc).

(Or make a comparator and pass it to your collections).

Upvotes: 4

akf
akf

Reputation: 39485

You can implement a compareTo(ClassB) or even a compareTo(CommonInterfaceOrSuperclass) method in each that will do the field-level comparison and return true if they meet your comparison criteria. You would then need to iterate over your lists and check against each element.

Another alternative is to implement the equals()/hashcode() pair, which would, when coded properly, allow your List to find and compare for equals-ness successfully. Your contains() test would then work.

Upvotes: 3

Related Questions