-1

Below is the program to compare two different list implementations. When I run this, getting true as a result.

I want to understand the behaviour of this equals() method in this case.

package com.tests;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class ListsTest {
    public static void main(String[] args) {
        List listA = new ArrayList<>();
        List listB = new LinkedList<>();
        Object object = new Object();
        Integer integer = new Integer(4);
        listA.add(object);
        listB.add(object);
        System.out.println(listA.equals(listB));
    }
}
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
EagerToLearn
  • 89
  • 12
  • You probably want to add `integer` in one list... not the same `Object object = new Object();` ... unless this is not what you tried ... but for that it would be nice to ask a question. – AxelH Nov 15 '17 at 13:09
  • Check this: https://stackoverflow.com/questions/1075656/simple-way-to-find-if-two-different-lists-contain-exactly-the-same-elements#answer-1075699 – sSaroj Nov 15 '17 at 13:09

3 Answers3

5

When you have a question on a specific method, the best place to start is the javadoc - extract:

In other words, two lists are defined to be equal if they contain the same elements in the same order. This definition ensures that the equals method works properly across different implementations of the List interface.

Side comment: you are using raw types when you write List listA = new ArrayList<>(); - you should use something like List<Object> listA = new ArrayList<>(); instead.

assylias
  • 321,522
  • 82
  • 660
  • 783
3

ArrayList and LinkedList are both AbstractLists. From the documentation:

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.

Both of your lists contains one element - the variable object, so they are same as they are both lists.

Papershine
  • 4,995
  • 2
  • 24
  • 48
0

The behaviour of .equals() method here will be governed by the class implementing the List interface (in your case the ArrayList class)

Following is a snippet from the Oracle JDK 1.8 implementation of ArrayList:

public boolean equals(Object o) {
    if (o == this)
        return true;
    if (!(o instanceof List))
        return false;

    ListIterator<E> e1 = listIterator();
    ListIterator<?> e2 = ((List<?>) o).listIterator();
    while (e1.hasNext() && e2.hasNext()) {
        E o1 = e1.next();
        Object o2 = e2.next();
        if (!(o1==null ? o2==null : o1.equals(o2)))
            return false;
    }
    return !(e1.hasNext() || e2.hasNext());
}

As is evident from here, the method will return true for any object implementing the List interface where the contents of the list are same as the current object or if both are pointing to the same object. It will return false if the passed object is not an implementation of List

Yash
  • 11,486
  • 4
  • 19
  • 35