Calculate difference between two Lists in Java
This post will discuss how to calculate differences between two lists x and y in Java. The solution should return all elements present in x that are not present in y.
1. Using Collection.removeAll() method
The removeAll() method is used to remove all list elements that are contained in the specified collection. We can use it to calculate differences between two lists, as follows. To avoid modifications to the original list, create a copy of the first list before calling the removeAll() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.ArrayList; import java.util.List; class Main { private static <T> List<T> findDifference(List<T> first, List<T> second) { List<T> diff = new ArrayList<>(first); diff.removeAll(second); return diff; } public static void main(String[] args) { List<Integer> first = List.of(1, 3, 2, 3, 4, 1); List<Integer> second = List.of(1, 3); List<Integer> duplicates = findDifference(first, second); System.out.println(duplicates); } } |
Output:
[2, 4]
The following solution transforms the List into a Set and calls the removeAll() method on it.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.HashSet; import java.util.List; import java.util.Set; class Main { private static <T> Set<T> findDifference(List<T> first, List<T> second) { Set<T> diff = new HashSet<>(first); diff.removeAll(second); return diff; } public static void main(String[] args) { List<Integer> first = List.of(1, 3, 2, 3, 4, 1); List<Integer> second = List.of(1, 3); Set<Integer> duplicates = findDifference(first, second); System.out.println(duplicates); } } |
Output:
[2, 4]
2. Using List.contains() method
In Java 8 and above, you can create a stream from the elements of the first list, and then filter the elements that are missing in the other list using the contains() method. This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.List; import java.util.Set; import java.util.stream.Collectors; class Main { private static <T> Set<T> findDifference(List<T> first, List<T> second) { return first.stream() .filter(i -> !second.contains(i)) .collect(Collectors.toSet()); } public static void main(String[] args) { List<Integer> first = List.of(1, 3, 2, 3, 4, 1); List<Integer> second = List.of(1, 3); Set<Integer> duplicates = findDifference(first, second); System.out.println(duplicates); } } |
Output:
[2, 4]
3. Using Apache Commons Collections
If you use the Apache Commons Collections library in your project, you may use the CollectionUtils’s subtract() method for this task.
Be vigilant with this utility method, as it might not remove all occurrences of an element from the list and depends on the cardinality of each element to find the difference. This behavior is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import org.apache.commons.collections4.CollectionUtils; import java.util.List; class Main { public static void main(String[] args) { List<Integer> first = List.of(1, 3, 2, 3, 4, 1); List<Integer> second = List.of(1, 3); System.out.println(CollectionUtils.subtract(first, second)); } } |
Output:
[2, 3, 4, 1]
That’s all about calculating differences between two lists in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)