Find common elements in two lists in Java
This post will discuss how to find the common elements in two lists in Java.
1. Using Collection.retainAll() method
The recommended approach to remove elements from a collection that are missing in the other collection is using the retainAll() method, which retains only the elements in the collection that are contained in the specified collection.
You can use the retainAll() method as follows to find all common elements. The following solution transforms the first list into a Set, and call retainAll on it. This is done to avoid modifying the original list.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; class Main { private static <T> Set<T> findCommonElements(List<T> first, List<T> second) { Set<T> common = new HashSet<>(first); common.retainAll(second); return common; } public static void main(String[] args) { List<Integer> first = Arrays.asList(1, 3, 1, 6, 5, 7); List<Integer> second = Arrays.asList(2, 3, 4, 5); Set<Integer> common = findCommonElements(first, second); System.out.println(common); } } |
Output:
[3, 5]
2. Using Stream API
Stream API made it very convenient to filter a collection. The idea is to get a stream of the elements in the first collection and filter elements that are contained in the other collection. This can be easily done using the List.contains() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.Arrays; import java.util.List; import java.util.Set; import java.util.stream.Collectors; class Main { private static <T> Set<T> findCommonElements(List<T> first, List<T> second) { return first.stream().filter(second::contains).collect(Collectors.toSet()); } public static void main(String[] args) { List<Integer> first = Arrays.asList(1, 3, 1, 6, 5, 7, 3); List<Integer> second = Arrays.asList(2, 3, 4, 5, 3); Set<Integer> common = findCommonElements(first, second); System.out.println(common); } } |
Output:
[3, 5]
You can improve efficiency by converting the second collection to a HashSet and calling its contains() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.stream.Collectors; class Main { private static <T> Set<T> findCommonElements(List<T> first, List<T> second) { Set<T> collection = new HashSet<>(second); return first.stream().filter(collection::contains).collect(Collectors.toSet()); } public static void main(String[] args) { List<Integer> first = Arrays.asList(1, 3, 1, 6, 5, 7, 3); List<Integer> second = Arrays.asList(2, 3, 4, 5, 3); Set<Integer> common = findCommonElements(first, second); System.out.println(common); } } |
Output:
[3, 5]
3. Using Apache Commons Collections
You can also use CollectionUtils.intersection() provided by Apache Commons Collections to get the intersection between the given iterables. Once you have the Collection containing the intersection of the two collections, you can optionally transform it into a Set or a List.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import org.apache.commons.collections4.CollectionUtils; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; class Main { private static <T> Set<T> findCommonElements(List<T> first, List<T> second) { return new HashSet<>(CollectionUtils.intersection(first, second)); } public static void main(String[] args) { List<Integer> first = Arrays.asList(1, 3, 1, 6, 5, 7, 3); List<Integer> second = Arrays.asList(2, 3, 4, 5, 3); Set<Integer> common = findCommonElements(first, second); System.out.println(common); } } |
Output:
[3, 5]
4. Using Guava
Similar to the Apache Commons Collections library, Guava offers the Sets.intersection() method, which returns an unmodifiable view of the intersection of two sets. It can be used as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import com.google.common.collect.Sets; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; class Main { private static <T> Set<T> findCommonElements(List<T> first, List<T> second) { return Sets.intersection(new HashSet<>(first), new HashSet<>(second)); } public static void main(String[] args) { List<Integer> first = Arrays.asList(1, 3, 1, 6, 5, 7, 3); List<Integer> second = Arrays.asList(2, 3, 4, 5, 3); Set<Integer> common = findCommonElements(first, second); System.out.println(common); } } |
Output:
[3, 5]
That’s all about finding the common elements in 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 :)