PearlJamFanatic
asked on
How can i compare two arraylists in JAVA
The 2 arraylists contains objects of classes as items.
I tried arrallist1.equals(arraylis t2) but it gives false een when the contents are same. Can someone tell why?
This is how the lists are created and populated.
ArrayList a=new ArrayList();
a.add(new Person (name, dateOfJoining);
I tried arrallist1.equals(arraylis
This is how the lists are created and populated.
ArrayList a=new ArrayList();
a.add(new Person (name, dateOfJoining);
After overriding the equals and hasdcode method in Person class, you need to loop through a list, pickup an item one by one and check if the other list contains that element.
Your elements needs to be in the same order in both list.
And the class of your objects in the lists needs the have the equals method implemented.
(If not, the Object.equals() will be used, and that method compares the memorylocation for the objects. )
And the class of your objects in the lists needs the have the equals method implemented.
(If not, the Object.equals() will be used, and that method compares the memorylocation for the objects. )
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If you are using Eclipse, your can choose 'Generate hashcode and equals..' in your person class.
check these links to know why hashcode and equals both are important
http://www.ibm.com/developerworks/java/library/j-jtp05273.html
http://www.javaworld.com/community/?q=node/1006
http://www.javabeat.net/tips/9-hashcode-and-equals-methods.html
http://www.ibm.com/developerworks/java/library/j-jtp05273.html
http://www.javaworld.com/community/?q=node/1006
http://www.javabeat.net/tips/9-hashcode-and-equals-methods.html
Pls look at this example..
public static void main(String args[]){
ArrayList<Person> a1 = new ArrayList<Person>();
ArrayList<Person> a2 = new ArrayList<Person>();
Date d1 = null;
Date d2 = null;
try {
d1 = new SimpleDateFormat("MM/dd/yy").parse("05/18/05");
d2 = new SimpleDateFormat("MM/dd/yy").parse("06/18/85");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
a1.add(new Person("Barbara","Obama",d1));
a1.add(new Person("Boby","Johns",d1));
a1.add(new Person("Larry","Stephan",d2));
a2.add(new Person("Boby","Johns",d1));
a2.add(new Person("Larry","Stephan",d2));
System.out.println(a1.containsAll(a2));
for(int i=0;i<a1.size();i++){
Person p1 = a1.get(i);
Person p2 = a2.get(i);
String name = p1.getFname();
String name2 = p2.getFname();
Date d3 = p1.getDob();
Date d4 = p2.getDob();
if(d3.equals(d4)){
System.out.println("Both are equal");
}else{
System.out.println("Not equal");
}
if(name.equals(name2)){
System.out.println("Both equal");
}else{
System.out.println("Not Equal");
}
}
}
u can easily implement ur logic in above example..by just using some flags.If both name&string are equal then we can say that both are equal...
if u need simpler method..refer this..
http://www.javabeat.net/tips/100-comparing-objects-in-java.html
http://www.javabeat.net/tips/100-comparing-objects-in-java.html
ASKER
@gurvinder372 as suggested by you in 33821310 If i check that the
a) element count is equal in both and
b) each element in 2nd arraylist exists in 1st (a.contains(b.get(i))
will this be sufficient to declare the 2 equals?
a) element count is equal in both and
b) each element in 2nd arraylist exists in 1st (a.contains(b.get(i))
will this be sufficient to declare the 2 equals?
Yes, if you have overridden hashcode and equals method, then a.contains(b.get(i)) should be enough.
ASKER
Ok I generated them in eclipse. Quick question does contains use hashcode or equals by any chance? just want to know why we are overriding them
ASKER
There is an arraylist within this class and when i try to generate the hashcode() and equals() it gives me warning saying the arraylist does not implement either and hence it may not work correctly. should this be ignored?
If you want to know why we are overriding them, please check links i posted in #33821321.
contains method will probably loop through the list and check each item (using overridden equals method), to check if they are same.
contains method will probably loop through the list and check each item (using overridden equals method), to check if they are same.
either you have to override eaquals method of Person class to compare by its value or compare each item individually by traversing list!