5

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?

1
  • 2
    Every implementation of List does directly or indirectly inherit from Object. Sounds like you got some misunderstandings of OO here. May 22, 2010 at 22:03

3 Answers 3

16

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.

1
  • I realize I wasn't clear and you are correct (and so does the answer now). May 22, 2010 at 22:39
3

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
7
  • 3
    According to the Lists.equals() JavaDoc, implementations of the List interface must check equality element by element. So even though Object.equals() does "shallow equals", List.equals() (and any conforming implementation) does "deep equals", and there is no need to write your own method.
    – markusk
    Aug 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.
    – duffymo
    Aug 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.
    – SigmaX
    Sep 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.
    – duffymo
    Sep 14, 2014 at 13:09
  • The above example was trying to instantiate an interface. Changed to actually make it compile.
    – KRK Owner
    Jun 26, 2016 at 15:01
2

You can still use equals. All objects implement it, and your lists are still objects and override equals as you need it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.