In this tutorial, we will walk through the process of finding common elements between two lists in Java. Working with lists is a fundamental aspect of Java programming, and the ability to compare and manipulate lists is essential for solving a wide variety of problems. Whether you’re preparing for coding interviews or looking to improve your Java skills, understanding how to perform list operations is crucial.
Understanding Lists in Java
In Java, a List
is an ordered collection of elements, and it is part of the java.util
package. Lists allow duplicate elements, and their size can dynamically change. The two most common implementations of the List
interface are ArrayList
and LinkedList
.
Before we dive into finding common elements, let’s create two sample lists that we can work with:
import java.util.ArrayList;
import java.util.List;
public class CommonElementsExample {
public static void main(String[] args) {
// Create two lists with some common elements
List list1 = new ArrayList<>();
List list2 = new ArrayList<>();
// Adding elements to list1
list1.add(1);
list1.add(2);
list1.add(3);
list1.add(4);
list1.add(5);
// Adding elements to list2
list2.add(3);
list2.add(4);
list2.add(5);
list2.add(6);
list2.add(7);
System.out.println("List 1: " + list1);
System.out.println("List 2: " + list2);
}
}
Approach 1: Using retainAll()
Method
Java’s List
interface provides a built-in method called retainAll()
, which can be used to retain only the elements that are present in both lists. This is a straightforward way to find common elements.
The retainAll()
method takes a collection as a parameter and removes all elements from the calling list that are not present in the specified collection.
Here is an example of using retainAll()
to find the common elements:
import java.util.ArrayList;
import java.util.List;
public class CommonElementsExample {
public static void main(String[] args) {
// Create two lists
List list1 = new ArrayList<>();
List list2 = new ArrayList<>();
// Adding elements to list1
list1.add(1);
list1.add(2);
list1.add(3);
list1.add(4);
list1.add(5);
// Adding elements to list2
list2.add(3);
list2.add(4);
list2.add(5);
list2.add(6);
list2.add(7);
// Finding common elements
list1.retainAll(list2);
// Output the common elements
System.out.println("Common elements: " + list1);
}
}
Explanation:
In this example, the retainAll()
method is called on list1
, passing list2
as the parameter. After executing the method, list1
will only contain the elements that are common between both lists. The output of this program will be:
Common elements: [3, 4, 5]
Approach 2: Using for
Loop and contains()
Method
Another way to find common elements is by manually iterating through the elements of one list and checking if they exist in the second list using the contains()
method.
Here’s an example using a loop:
import java.util.ArrayList;
import java.util.List;
public class CommonElementsExample {
public static void main(String[] args) {
// Create two lists
List list1 = new ArrayList<>();
List list2 = new ArrayList<>();
// Adding elements to list1
list1.add(1);
list1.add(2);
list1.add(3);
list1.add(4);
list1.add(5);
// Adding elements to list2
list2.add(3);
list2.add(4);
list2.add(5);
list2.add(6);
list2.add(7);
// Finding common elements manually
List commonElements = new ArrayList<>();
for (Integer element : list1) {
if (list2.contains(element)) {
commonElements.add(element);
}
}
// Output the common elements
System.out.println("Common elements: " + commonElements);
}
}
Explanation:
In this approach, we loop through list1
and check if each element is present in list2
using the contains()
method. If the element is found in both lists, it is added to the commonElements
list. The output will be:
Common elements: [3, 4, 5]
Performance Considerations:
The retainAll()
method is typically more efficient because it performs a direct comparison between the two lists. On the other hand, the contains()
method used in a loop has a time complexity of O(n) for each lookup, which can be inefficient for large lists.
Conclusion
In this article, we covered two different approaches for finding common elements between two lists in Java: the retainAll()
method and a manual for
loop combined with the contains()
method. Both methods are effective, but the retainAll()
method is often the best choice for its simplicity and efficiency. Understanding these basic list operations is an important skill for any Java developer.
retainAll()
method modifies the original list, so if you want to keep the original lists intact, make sure to create a copy of the list before using this method.