Java gives a strategy to contrasting two Array List. The ArrayList.equals() is the strategy utilized for contrasting two Array List. It analyzes the Array records as, both Array records ought to have a similar size, and all relating sets of components in the two Array records are equivalent.
Example:
Input : ArrayList1 = [1, 2, 3, 4], ArrayList2 = [1, 2, 3, 4] Output: Array List are equal Input : ArrayList1 = [1, 2, 3, 4], ArrayList2 = [4, 2, 3, 1] Output: Array List are not equal
Syntax:
boolean equals(Object o)
Parameters:
Implementation:
// Comparing two ArrayList In Java import java.util.ArrayList; public class GFG { public static void main(String[] args) { // create two Array List ArrayList ArrayList1 = new ArrayList(); ArrayList ArrayList2 = new ArrayList(); // insert items in AyyarList 1 ArrayList1.add("item 1"); ArrayList1.add("item 2"); ArrayList1.add("item 3"); ArrayList1.add("item 4"); // insert items in AyyarList 2 ArrayList2.add("item 1"); ArrayList2.add("item 2"); ArrayList2.add("item 3"); ArrayList2.add("item 4"); // Display both ArrayList System.out.println(" ArrayList1 = " + ArrayList2); System.out.println(" ArrayList1 = " + ArrayList1); // compare ArrayList1 with ArrayList2 if (ArrayList1.equals(ArrayList2) == true) { System.out.println(" Array List are equal"); } else // else block execute when // ArrayList are not equal { System.out.println(" Array List are not equal"); } // insert one more item in ArrayList 1 System.out.println( "\n Lets insert one more item in Array List 1"); ArrayList1.add("item 5"); // display both ArrayList System.out.println(" ArrayList1 = " + ArrayList1); System.out.println(" ArrayList = " + ArrayList2); // again compare ArrayList 1 with ArrayList 2 if (ArrayList1.equals(ArrayList2) == true) { System.out.println(" Array List are equal"); } else { System.out.println(" Array List are not equal"); } } }
Output:
ArrayList1 = [item 1, item 2, item 3, item 4] ArrayList1 = [item 1, item 2, item 3, item 4] Array List are equal Lets insert one more item in Array List 1 ArrayList1 = [item 1, item 2, item 3, item 4, item 5] ArrayList = [item 1, item 2, item 3, item 4] Array List are not equal
Time Complexity: O(N), where N is the length of the Array list.
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.