In Java, you can use the Collection interface's removeAll method.

// Create a couple ArrayList objects and populate them
// with some delicious fruits.
Collection firstList = new ArrayList() {{
    add("apple");
    add("orange");
}};

Collection secondList = new ArrayList() {{
    add("apple");
    add("orange");
    add("banana");
    add("strawberry");
}};

// Show the "before" lists
System.out.println("First List: " + firstList);
System.out.println("Second List: " + secondList);

// Remove all elements in firstList from secondList
secondList.removeAll(firstList);

// Show the "after" list
System.out.println("Result: " + secondList);

The above code will produce the following output:

First List: [apple, orange]
Second List: [apple, orange, banana, strawberry]
Result: [banana, strawberry]
Answer from William Brendel on Stack Overflow
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java list โ€บ finding the differences between two lists in java
Finding the Differences Between Two Lists in Java | Baeldung
April 29, 2026 - List<String> differences = new ArrayList<>(CollectionUtils.subtract(listOne, listTwo)); assertEquals(3, differences.size()); assertThat(differences).containsExactly("Tom", "John", "Jack"); In this article, we explored a few ways to find the differences between lists. We covered a basic Java solution, a solution using the Streams API, and solutions using third-party libraries, like Google Guava and Apache Commons Collections.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ comparing-two-arraylist-in-java
Comparing two ArrayList In Java - GeeksforGeeks
January 23, 2026 - To compare two ArrayList objects, Java provides the equals() method. This method checks whether both lists have the same size and contain the same elements in the same order, making it a simple and reliable way to compare lists.
๐ŸŒ
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<>
๐ŸŒ
DevQA
devqa.io โ€บ java-compare-two-arraylists
Java Compare Two Lists
March 17, 2020 - The List interface also provides methods to find differences between two lists. The removeAll() method compares two lists and removes all the common items. Whatโ€™s left is the additional or missing items.
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2013 โ€บ 12 โ€บ how-to-compare-two-arraylist-in-java
How to compare two ArrayList in Java
"Yes" : "No"); System.out.println(al3); //Storing the comparison output in ArrayList<Integer> ArrayList<Integer> al4= new ArrayList<Integer>(); for (String temp2 : al1) al4.add(al2.contains(temp2) ? 1 : 0); System.out.println(al4); } } ... What is the logic in above code? If the first element of ArrayList al1 is present in al2 then ArrayList al3 would be having โ€œYesโ€ and al4 would be having โ€œ1โ€ However if the element is not present โ€œNoโ€ would be stored in al3 and 0 would be in al4. Java ArrayList addAll(int index, Collection c) Method example
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 638779 โ€บ java โ€บ Compare-Contrast-ArrayList-Object
Compare and Contrast Two ArrayList<Object> (Java in General forum at Coderanch)
August 29, 2014 - When you have this magical function that finds added/removed pins how does it not include 'changed' pins. Feel free to reference my mock table below: There is no way to check a modification vs an addition/removal (That I know even if I'm just eyeing the data versus java calculating it). It's impossible to check for changes since there is no unique key that can be compared across the two connectors. The difference function's purpose is not to find the changed objects, it's only for finding the added/removed ones.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ comparing-two-arraylist-in-java
Check two ArrayList for equality in Java
June 18, 2024 - Questions and Answers ยท Online Resume Builder ยท HR Interview Questions ยท Computer Glossary ยท Who is Who ยท Java Object Oriented Programming Programming ยท Two ArrayList can be compared to check if they are equal or not using the method java.util.ArrayList.equals().
Find elsewhere
๐ŸŒ
Javatpoint
javatpoint.com โ€บ how-to-compare-two-arraylist-in-java
How to Compare Two ArrayList in Java - Javatpoint
How to Compare Two ArrayList in Java with oops, string, exceptions, multithreading, collections, jdbc, rmi, fundamentals, programs, swing, javafx, io streams, networking, sockets, classes, objects etc,
๐ŸŒ
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
๐ŸŒ
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 - public class TwoArrayListEqualityExampleInJava8 { public static void main(String[] args) { // list 1 List<String> list1 = new ArrayList<String>(); list1.add("compare"); list1.add("two"); list1.add("lists"); list1.add("in java 8"); // list 2 List<String> list2 = new ArrayList<String>(); list2.add("compare"); list2.add("two"); list2.add("lists"); list2.add("in java 8"); List<String> unavailable = list1.stream().filter(e -> (list2.stream().filter(d -> d.equals(e)).count()) < 1) .collect(Collectors.toList()); if (unavailable.size() == 0) { System.out.println("list1 and list2 all values same."); }
๐ŸŒ
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 - To be sure, we can sort the ArrayList (sorting can be ascending or descending) and then apply the equals() method for comparison. Read Article to learn how to sort ArrayList in Java: https://www.java8net.com/2020/02/how-to-sort-arraylist-in...
๐ŸŒ
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 - To be sure, we can sort the ArrayList (sorting can be ascending or descending) and then apply the equals() method for comparison. Read Article to learn how to sort ArrayList in Java: https://www.java8net.com/2020/02/how-to-sort-arraylist-in...
๐ŸŒ
Quora
quora.com โ€บ Way-to-compare-two-ArrayLists-and-see-if-they-share-mutual-Persons-objects-in-Java
Way to compare two ArrayLists and see if they share mutual Persons objects in Java? - Quora
Answer (1 of 6): Way to compare two ArrayLists and see if they share mutual Persons objects in Java? You have other answers that focus on "How would I do this instead of what you're doing?", so I'll give an answer that focuses on "What is wrong with my code?" One of the important things when yo...
๐ŸŒ
amitph
amitph.com โ€บ home โ€บ java โ€บ comparing two lists in java
Comparing Two Lists In Java - amitph
November 22, 2024 - The method takes two Java Collections and returns a Collection instance containing their common elements. Example of finding common elements of two Lists using Commons Collections. List<Integer> commons = new ArrayList<>( CollectionUtils.intersection(one, two)); System.out.println(commons); //prints: //[4, 6]Code language: Java (java) So far, we have been interested in finding the elements present in both Lists. Now letโ€™s understand how to find the difference between two ArrayLists in Java.
๐ŸŒ
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) ?
๐ŸŒ
Tpoint Tech
tpointtech.com โ€บ how-to-compare-two-arraylist-in-java
How to Compare Two ArrayList in Java - Tpoint Tech
May 1, 2026 - If the elements of firstList match with the elements of the secondList, it return Yes and stores this value into thirdList. Similarly, if the element does not match, it return No. ... Java contentEquals() method compares the String with the StringBuffer and returns a boolean value.
๐ŸŒ
DaniWeb
daniweb.com โ€บ programming โ€บ software-development โ€บ threads โ€บ 298013 โ€บ need-help-compare-two-string-arraylist-in-java-and-find-difference
Need help compare two string arraylist in java and find difference
July 20, 2010 - Check the API of the ArrayList class. There is a remove method that you can use. Or you can use the Vector class that is similar to the ArrayList. Check their APIs to find a method "remove" that is suitable for you. โ€” javaAddict 900 Jump to Post