The equals() method of AbstractList class in Java

Java 8Object Oriented ProgrammingProgramming

The equals() method of the AbstractList class is used to compare the specified object with this list for equality. The value TRUE is returned if both the lists are same i.e the same size and elements.

The syntax is as follows

public boolean equals(Object ob)

Here, ob is the object is to be compared for equality. To work with the AbstractList class, import the following package

import java.util.AbstractList;

The following is an example to implement equals() method of the AbstractlList class in Java

Example

 Live Demo

import java.util.ArrayList;
import java.util.AbstractList;
public class Demo {
   public static void main(String[] args) {
      AbstractList<Integer> myList = new ArrayList<Integer>();
      myList.add(75);
      myList.add(100);
      myList.add(150);
      myList.add(200);
      myList.add(250);
      myList.add(300);
      myList.add(350);
      myList.add(400);
      System.out.println("Elements in the AbstractList = " + myList);
      AbstractList<Integer> myList2 = new ArrayList<Integer>();
      myList2.add(75);
      myList2.add(100);
      myList2.add(150);
      myList2.add(200);
      myList2.add(250);
      myList2.add(300);
      myList2.add(350);
      myList2.add(400);
      System.out.println("Elements in the AbstractList2 = " + myList2);
      System.out.println("Are both the lists same? = " + myList.equals(myList2));
   }
}

Output

Elements in the AbstractList = [75, 100, 150, 200, 250, 300, 350, 400]
Elements in the AbstractList2 = [75, 100, 150, 200, 250, 300, 350, 400]
Are both the lists same? = true
raja
Published on 28-Mar-2019 08:16:16
Advertisements