- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check two ArrayList for equality in Java
Two ArrayList can be compared to check if they are equal or not using the method java.util.ArrayList.equals(). This method has a single parameter i.e. an ArrayList that is compared with the current object. It returns true if the two ArrayList are equal and false otherwise.
A program that demonstrates this is given as follows −
Example
import java.util.ArrayList; import java.util.List; public class Demo { public static void main(String[] args) { List aList1 = new ArrayList(); aList1.add("Sun"); aList1.add("Moon"); aList1.add("Stars"); List aList2 = new ArrayList(); aList2.add("Sun"); aList2.add("Moon"); aList2.add("Stars"); System.out.println("The two ArrayList are equal? " + aList1.equals(aList2)); } }
Output
The two ArrayList are equal? true
Now let us understand the above program.
The ArrayList aList1 and aList2 are created and ArrayList.add() is used to add the elements to the two ArrayList. Then ArrayList.equals() method is used to check if the ArrayList are equal or not and the result is displayed. A code snippet which demonstrates this is as follows −
List aList1 = new ArrayList(); aList1.add("Sun"); aList1.add("Moon"); aList1.add("Stars"); List aList2 = new ArrayList(); aList2.add("Sun"); aList2.add("Moon"); aList2.add("Stars"); System.out.println("The two ArrayList are equal? " + aList1.equals(aList2));
- Related Articles
- How to compare two ArrayList for equality in Java?
- Check two numbers for equality in Java
- Check two HashMap for equality in Java
- Check two float arrays for equality in Java
- Java Program to check for equality between two strings ignoring the case
- How to compare two lists for equality in C#?
- How to compare two arrays for equality in Perl?
- Java Program to compare strings for equality
- Check existence of an element in Java ArrayList
- Check if two ArrayList objects are equal in C#
- MongoDB - How to check for equality in collection and in embedded document?
- How to check for equality of three columns by row in R?
- How to check arraylist is empty for Listview in Android?
- How to check if ArrayList contains an item in Java?
- Equality of two arrays JavaScript

Advertisements