Introduction
The retainAll()
method in Java's ArrayList
is a handy tool for filtering elements. It keeps only the elements present in an ArrayList that are also found within another Collection. Think of it like this: you have two lists, and you want to end up with a list containing just the items that appear on *both* original lists.
Syntax
public boolean retainAll(Collection> c)
Parameters
Parameter | Description |
---|---|
c |
The Collection whose elements are to be retained. Only the elements that are also present in this collection will remain in the ArrayList after calling retainAll() . It can be any type of Collection, such as another ArrayList, a HashSet, or a LinkedList. The data types of elements must be compatible. |
Return Value
The method returns true
if the list was modified; otherwise, it returns false
.
Examples
Example 1: Basic Usage
This example demonstrates how to use retainAll()
to keep only the elements present in both an ArrayList and a HashSet.
import java.util.*;
public class RetainAllExample {
public static void main(String[] args) {
List list1 = new ArrayList<>(Arrays.asList("apple", "banana", "cherry", "date"));
Set set1 = new HashSet<>(Arrays.asList("banana", "date", "fig"));
System.out.println("Original list: " + list1);
list1.retainAll(set1);
System.out.println("List after retainAll(): " + list1); // Output will only contain 'banana' and 'date'
}
}
Original list: [apple, banana, cherry, date]
List after retainAll(): [banana, date]
Explanation:
We started with an ArrayList list1
and a HashSet set1
. The retainAll()
method was called on list1
, passing in set1
as the collection to retain. As a result, list1
now only contains the elements that were present in *both* list1
and set1
(which are “banana” and “date”).
Example 2: Using Another ArrayList
This example shows how to use retainAll()
with another ArrayList.
import java.util.*;
public class RetainAllArrayListExample {
public static void main(String[] args) {
List list1 = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
List list2 = new ArrayList<>(Arrays.asList(3, 5, 6, 7));
System.out.println("Original list1: " + list1);
System.out.println("Original list2: " + list2);
list1.retainAll(list2);
System.out.println("List1 after retainAll(): " + list1); // Output will only contain 3 and 5
}
}
Original list1: [1, 2, 3, 4, 5]
Original list2: [3, 5, 6, 7]
List1 after retainAll(): [3, 5]
Explanation:
We have two ArrayLists, list1
and list2
. The call to retainAll(list2)
on list1
keeps only the elements present in both lists. Only 3 and 5 exist in both, so they remain in list1
.
Example 3: No Common Elements
This example illustrates what happens when there are no common elements between the ArrayList and the Collection used with retainAll()
.
import java.util.*;
public class RetainAllNoCommonExample {
public static void main(String[] args) {
List list1 = new ArrayList<>(Arrays.asList("apple", "banana", "cherry"));
Set set1 = new HashSet<>(Arrays.asList("date", "fig", "grape"));
System.out.println("Original list: " + list1);
list1.retainAll(set1);
System.out.println("List after retainAll(): " + list1); // Output will be an empty list
}
}
Original list: [apple, banana, cherry]
List after retainAll(): []
Explanation:
The ArrayList list1
and the HashSet set1
have no elements in common. After calling retainAll()
, the ArrayList list1
becomes empty because there are no shared elements.
Example 4: Using retainAll with a LinkedList
This example shows that you can use any type of Collection with retainAll()
.
import java.util.*;
public class RetainAllLinkedListExample {
public static void main(String[] args) {
List list1 = new ArrayList<>(Arrays.asList("apple", "banana", "cherry"));
LinkedList linkedList1 = new LinkedList<>(Arrays.asList("banana", "date"));
System.out.println("Original list: " + list1);
list1.retainAll(linkedList1);
System.out.println("List after retainAll(): " + list1); // Output will contain 'banana'
}
}
Original list: [apple, banana, cherry]
List after retainAll(): [banana]
Explanation:
The retainAll()
method accepts any type of Collection. In this case, we used a LinkedList as the collection to check against.