equals(Object o) method of List Interface in Java.

equals(Object o) method of List Interface in Java.


Posted in : Core Java Posted on : January 4, 2011 at 7:51 PM Comments : [ 0 ]

In the following example we will show you the method of check the equality of the two List Objects using equals(Object o) method of List Interface in Java.

equals(Object o) method of List Interface in Java.

In the following example we will show you the method of check the equality of the two List Objects using equals(Object o) method of List Interface in Java.

Syntax

public boolean equals(Object o)

This method returns either True or False.

In this method you can check that the two lists are equal or not . It compares the specified list object with the other list object and returns true if both lists are equal or returns false if lists are not equal. When all the corresponding pairs of elements of both lists are equals and of same size then it returns 'true' otherwise 'false'. In other words, two lists are defined to be equal if they contain the same elements in the same order.  i.e. A List is equal to another object if the other object implements the List interface, has the same size( ), and contains the same elements in the same positions.

Parameter Description

Object o : It takes an argument as List Object which contains a list.

Example of equals(Object o) method 

In this example we will show you how does a equals(Object o) method work in List Interface. This example will help you to understand how check all elements of a specific list is equal with a specified list. Through this example we will show you adding elements to the list, count the number of list, and equaling  the two List Objects. In this example the true value is returned because all the corresponding pairs of elements are equal and at same place. 

Example:-


package Devmanuals.com;
import java.util.List;
import java.util.ArrayList;
public class ListEquals {
public static void main (String args []){
List l1= new ArrayList();
l1.add("Ram");
l1.add("is");
l1.add("a");
l1.add("boy");
System.out.println("The Element of list1 is : "+l1);
System.out.println("The size of list1 is : "+l1.size());
List l2=new ArrayList();
l2.add("Ram");
l2.add("is");
l2.add("a");
l2.add("boy");
System.out.println("The Element of list2 is : "+l2);
System.out.println("The size of list2 is : "+l2.size());
l1.equals(l2);
System.out.println("The Elements are equal : "+l1.equals(l2));
}
}

Output : 

The Element of list1 is : [Ram, is, a, boy]

The size of list1 is : 4

The Element of list2 is : [Ram, is, a, boy]

The size of list2 is : 4

The Elements are equal : true

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics