File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Comparing Objects containing List

 
Samy Ganou
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a class :
class A {
public List<String> values;
}
I want to compare two instances .

A a1 = new A();
ArrayList<String> arr1 = new ArrayList<String>();
arr1.add("1");
arr1.add("2");
a1.values= arr1;

A a2 = new A();
ArrayList<String> arr2 = new ArrayList<String>();
arr2.add("1");
arr2.add("2");
a2.values= arr2;

a1 and a2 are equals but a2.equals(a1) give me false !
how i can compare ?
thank you in advance.
 
Joanne Neal
Rancher
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your A class does not override the equals method, therefore it inherits it from the Object class, which means that a1.equals(a2) is exactly the same as a1 == a2. i.e. it will only be true if a1 and a2 refer to the same object, which in this case they don't.
You need to override equals and return the result of comparing the two lists
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic