I want to compare two lists. Since we code to interface using List
, which does not inherit the equals
from from the Object class. How do I do this?
-
2Every implementation of List does directly or indirectly inherit from Object. Sounds like you got some misunderstandings of OO here.– whiskeysierraMay 22, 2010 at 22:03
3 Answers
Even though the List
interface does not contain an equals
method, the list-classes may (and does) still implement the equals
method.
From the API docs on AbstractList
(inherited by for instance ArrayList
, LinkedList
, Vector
):
public boolean equals(Object o)
Compares the specified object with this list for equality. Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal.
The same applies to for instance the toString
, hashCode
method and so on.
As @Pascal mentions in the comments, the List interface mentions the equals
method and states the following in the documentation:
The List interface places additional stipulations, beyond those specified in the Collection interface, on the contracts of the iterator, add, remove, equals, and hashCode methods.
-
I realize I wasn't clear and you are correct (and so does the answer now). May 22, 2010 at 22:39
It's the usual story: you have to consider "shallow equals" and "deep equals".
The default behavior that you get out of java.lang.Object is "shallow equals". It'll check to see if list1 and list2 are the same references:
List list1 = new ArrayList();
List list2 = list1;
list1.equals(list2); // returns true;
If you want "deep equals", instantiate anything that extends AbstractList, such as ArrayList.
List<String> list1 = new ArrayList<>();
List<String> list2 = new ArrayList<>();
list1.add("hello");
list2.add("hello");
System.out.println(list1.equals(list2)); // will print true
list1.add("foo");
list2.add("bar");
System.out.println(list1.equals(list2)); // will print false
-
3According to the Lists.equals() JavaDoc, implementations of the
List
interface must check equality element by element. So even thoughObject.equals()
does "shallow equals",List.equals()
(and any conforming implementation) does "deep equals", and there is no need to write your own method.– markuskAug 7, 2012 at 12:18 -
Agreed about List, but if the element doesn't override equals you don't get "deep equals". It's not worded very clearly, but I believe that's the truth.– duffymoAug 7, 2012 at 14:02
-
Object.equals() is just a placeholder for deep equals; it is not meant to be used for shallow equals. The standard contract requires that every object (not just List) implements equals() to do deep equality. That's what the equals() method is for. '==' is for shallow comparison.– SigmaXSep 14, 2014 at 12:49
-
Sorry, the default implementation in java.lang.Object is shallow equals. That's what you get if you do nothing. This answer is FOUR YEARS OLD. You add nothing with this useless comment. Go do something else, like answer a question, to boost your reputation.– duffymoSep 14, 2014 at 13:09
-
The above example was trying to instantiate an interface. Changed to actually make it compile. Jun 26, 2016 at 15:01
You can still use equals
. All objects implement it, and your lists are still objects and override equals
as you need it.