Probably the easiest way for any list would be:

listA.containsAll(listB) && listB.containsAll(listA)
Answer from user381105 on Stack Overflow
๐ŸŒ
How to do in Java
howtodoinjava.com โ€บ home โ€บ collections framework โ€บ java arraylist โ€บ compare two lists in java: equality, differences, common items
Compare Two Lists in Java: Equality, Differences, Common Items
1 week ago - List<String> warehouse = List.of("apple", "banana", "cherry"); List<String> shop = List.of("cherry", "banana", "date"); List<String> reordered = List.of("banana", "cherry", "apple"); boolean equal = warehouse.equals(shop); // false boolean sameIgnoringOrder = warehouse.stream().sorted().toList() .equals(reordered.stream().sorted().toList()); // true List<String> onlyInWarehouse = new ArrayList<>(warehouse); onlyInWarehouse.removeAll(shop); // [apple] List<String> missingInWarehouse = new ArrayList<>(shop); missingInWarehouse.removeAll(warehouse); // [date] List<String> common = new ArrayList<>
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ comparing-two-arraylist-in-java
Comparing two ArrayList In Java - GeeksforGeeks
January 23, 2026 - Two ArrayList objects list1 and list2 are created. list1.equals(list2) checks if both lists are equal (same size and elements in the same order).
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ comparing-two-arraylist-in-java
Check two ArrayList for equality in Java
June 18, 2024 - 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.
๐ŸŒ
DevQA
devqa.io โ€บ java-compare-two-arraylists
Java Compare Two Lists
March 17, 2020 - import java.util.Arrays; import java.util.Collections; import java.util.List; public class CompareTwoLists { public static void main(String[] args) { List<String> listOne = Arrays.asList("b", "c", "a"); List<String> listTwo = Arrays.asList("a", "c", "b"); List<String> listThree = Arrays.asList("c", "a", "b"); Collections.sort(listOne); Collections.sort(listTwo); Collections.sort(listThree); boolean isEqual = listOne.equals(listTwo); System.out.println(isEqual); isEqual = listOne.equals(listThree); System.out.println(isEqual); } } Output: true true ยท The List interface also provides methods to
๐ŸŒ
Javaprogramto
javaprogramto.com โ€บ 2020 โ€บ 04 โ€บ how-to-compare-two-arraylist-for-equality-in-java.html
How to compare two ArrayList for equality in Java 8? ArrayList equals() or containsAll() methods works?
June 13, 2021 - // list 2 List<String> listB = ... listB.add("linkedlist"); // comparing two lists boolean isEqualAllValues = listA.containsAll(listB); System.out.println(isEqualAllValues); Output: ... Now, it is time to write the same logic ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-compare-two-arraylist-for-equality-in-java
How to compare two ArrayList for equality in Java?
July 30, 2019 - Java Object Oriented Programming Programming ยท You can compare two array lists using the equals() method of the ArrayList class, this method accepts a list object as a parameter, compares it with the current object, in case of the match it ...
๐ŸŒ
w3resource
w3resource.com โ€บ java-exercises โ€บ collection โ€บ java-collection-exercise-13.php
Java - Compare two array lists
import java.util.*; public class Exercise13 { public static void main(String[] args) { ArrayList<String> c1= new ArrayList<String>(); c1.add("Red"); c1.add("Green"); c1.add("Black"); c1.add("White"); c1.add("Pink"); ArrayList<String> c2= new ArrayList<String>(); c2.add("Red"); c2.add("Green"); c2.add("Black"); c2.add("Pink"); //Storing the comparison output in ArrayList<String> ArrayList<String> c3 = new ArrayList<String>(); for (String e : c1) c3.add(c2.contains(e) ?
๐ŸŒ
javaspring
javaspring.net โ€บ blog โ€บ java-compare-unordered-arraylists
How to Compare Unordered ArrayLists in Java: Efficiently Check for Same Elements Regardless of Order โ€” javaspring.net
By default, the `equals()` method of `ArrayList` checks for **strict equality**: two lists are considered equal only if they have the same elements in the **same order**, with the same frequencies.
Find elsewhere
๐ŸŒ
Apps Developer Blog
appsdeveloperblog.com โ€บ home โ€บ java โ€บ java examples โ€บ compare two arraylist in java
Compare two ArrayList in Java - Apps Developer Blog
August 16, 2022 - class Test { public static void main(String[] args) { List<Integer> list1 = new ArrayList<>(Arrays.asList(1, 2, 3, 4)); // list2 is equal to list 2 List<Integer> list2 = new ArrayList<>(Arrays.asList(1, 2, 3, 4)); // list3 has the same elements but in diff order List<Integer> list3 = new ArrayList<>(Arrays.asList(3, 1, 4, 2)); // list4 is not the same List<Integer> list4 = new ArrayList<>(Arrays.asList(3, 7, 9, 2)); System.out.println("list1 == list2: " + CollectionUtils.isEqualCollection(list1, list2)); System.out.println("list1 == list3: " + CollectionUtils.isEqualCollection(list1, list3)); System.out.println("list1 == list4: " + CollectionUtils.isEqualCollection(list1, list4)); } }
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ a question about the comparison of elements in two arraylists in java.
r/learnprogramming on Reddit: A question about the comparison of elements in two ArrayLists in java.
April 21, 2023 -

I'm trying to do my homework

I created two ArraLists of Integer, and the content in the list is as follows:

List1:

5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 145 150

List2:

150 300 450 600 750

when i use

list1. get(list1. size() - 1) == list2. get(0);

I get false;

I have to use

list1.get(list1.size() - 1).equals(list2.get(0));

to get true;

Then I try the following code:

ArrayList<Integer> arrList1 = new ArrayList<Integer>();

ArrayList<Integer> arrList2 = new ArrayList<Integer>();

arrList1. add(10);

arrList2.add(10);

System.out.println(arrList1.get(0) == arrList2.get(0));

I get true;

I know that == can only be used to compare values, or Primitive types, while .equals() is used to compare references.

I'm sure what I'm storing in the ArrayList is integer, int, so why can't I use == for comparison? (And I'm also wondering why comparing 10 with == gets true and 150 gets false)

I wonder why this is happening?

Thanks in advance to everyone who helped me.

๐ŸŒ
Medium
aryan-kapoor4564.medium.com โ€บ how-to-compare-two-arraylist-in-java-83336458880
How to compare two ArrayList in Java? | by Aryan Kapoor | Medium
March 16, 2020 - With the help of the equals() method, we can compare two ArrayList in java and check whether they are the same or not. But, here is a condition, the order of the elements of both the ArrayList must be the same.
๐ŸŒ
Medium
medium.com โ€บ @QA-initi โ€บ how-to-compare-two-array-list-in-java-using-comparator-cc05617175b5
How to compare two array list in Java using Comparator | by QA-init | Medium
November 14, 2023 - ArrayList<Integer> list1 = new ... comparator); Collections.sort(list2, comparator); After this, we can use the equals() method to compare the two ArrayList....
๐ŸŒ
javaspring
javaspring.net โ€บ blog โ€บ how-to-compare-arraylists-in-java
Comparing ArrayLists in Java: A Comprehensive Guide โ€” javaspring.net
Custom Comparison: Comparing elements based on specific business rules, such as comparing objects by a particular property. The equals method in ArrayList checks if two lists are equal.
๐ŸŒ
Java Guides
javaguides.net โ€บ 2024 โ€บ 06 โ€บ java-arraylist-equals-method.html
Java ArrayList equals() Method
June 11, 2024 - โ–ถ๏ธ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube ... The ArrayList.equals() method in Java is used to compare two ArrayList instances for equality.
๐ŸŒ
DEV Community
dev.to โ€บ aryan19694946 โ€บ how-to-compare-two-arraylist-in-java-499b
How to compare two ArrayList in Java? - DEV Community
March 19, 2020 - With the help of the equals() method, we can compare two ArrayList in java and check whether they are the same or not. But, here is a condition, the order of the elements of both the ArrayList must be the same.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ list-equals-method-in-java-with-examples
List equals() Method in Java with Examples - GeeksforGeeks
January 3, 2025 - // Java code to show the implementation ... l2 = new ArrayList<>(); l2.add(1); l2.add(2); if (l.equals(l2)) System.out.println("Equal"); else System.out.println("Not equal"); } } ... Parameters: This function has a single parameter ...
๐ŸŒ
javathinking
javathinking.com โ€บ blog โ€บ how-to-compare-equality-of-lists-of-arrays-with-modern-java
How to Compare Equality of Lists of Arrays in Modern Java 8 (and Java 9 Enhancements) โ€” javathinking.com
For each index, compare the arrays element-wise (not by reference). Java 8 introduced the Stream API, which provides a clean and efficient way to iterate over collections and perform element-wise checks. Below are three Java 8 approaches to compare lists of arrays. Before comparing elements, always check if the lists have the same size. If not, they canโ€™t be equal.
๐ŸŒ
YouTube
youtube.com โ€บ naveen automationlabs
#7 - How to Compare Two ArrayLists in Java - YouTube
In this video, I have explained how to compare two ArrayLists in Java.Learn:1. Compare two ArrayList using Sort and Equals methods.2. Find out the additional...
Published: October 19, 2020
Views: 52K