Introduction
Sometimes you need to remove a group of elements from an ArrayList
at once. The removeAll()
method is your friend for that! It allows you to specify another Collection
(like another ArrayList
, a HashSet
, etc.) and removes all elements present in *that* collection from the original ArrayList
.
Syntax
public boolean removeAll(Collection> c)
Parameters
Parameter | Description |
---|---|
c |
The collection containing elements to be removed from this ArrayList . The removeAll() method will remove all occurrences of the elements present in `c` from the current list. |
Return Value
The removeAll()
method returns a boolean value: true
if the ArrayList
was modified (meaning at least one element was removed), and false
otherwise.
Examples
Example 1: Removing Elements from Another ArrayList
This example demonstrates removing elements present in another ArrayList
. We'll start with two lists, remove the common elements from the first list, and then observe the result.
import java.util.ArrayList; // Import ArrayList class
import java.util.Arrays;
public class RemoveAllExample1 {
public static void main(String[] args) {
// Create two ArrayLists
ArrayList list1 = new ArrayList<>(Arrays.asList("apple", "banana", "cherry", "date", "fig"));
ArrayList list2 = new ArrayList<>(Arrays.asList("banana", "date", "grape"));
System.out.println("Original List 1: " + list1);
System.out.println("List 2 (elements to remove): " + list2);
// Remove all elements from list1 that are also in list2
boolean removed = list1.removeAll(list2);
System.out.println("List 1 after removal: " + list1);
System.out.println("Was the list modified? " + removed);
}
}
Original List 1: [apple, banana, cherry, date, fig]
List 2 (elements to remove): [banana, date, grape]
List 1 after removal: [apple, cherry, fig]
Was the list modified? true
Explanation: We created two lists. The `removeAll()` method was called on list1
using list2
as the collection of elements to remove. Elements “banana” and “date”, which are present in both lists, were removed from list1
. Because list1 changed (elements were removed), `removed` is true.
Example 2: Removing Elements Using a HashSet
This example shows how to use a HashSet
as the collection for removal. The primary difference here isn't functionality but demonstrates that any Collection
can be passed to `removeAll()`
import java.util.ArrayList; // Import ArrayList class
import java.util.Arrays;
import java.util.HashSet; // Import HashSet class
public class RemoveAllExample2 {
public static void main(String[] args) {
// Create an ArrayList
ArrayList list = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
// Create a HashSet containing elements to remove
HashSet setToRemove = new HashSet<>(Arrays.asList(2, 4));
System.out.println("Original List: " + list);
System.out.println("Set of elements to remove: " + setToRemove);
// Remove all elements from the ArrayList that are also in the HashSet
boolean removed = list.removeAll(setToRemove);
System.out.println("List after removal: " + list);
System.out.println("Was the list modified? " + removed);
}
}
Original List: [1, 2, 3, 4, 5]
Set of elements to remove: [2, 4]
List after removal: [1, 3, 5]
Was the list modified? true
Explanation: We created an ArrayList
and a HashSet
. The removeAll()
method removed all elements present in the HashSet
from the ArrayList
, resulting in “2” and “4” being removed.
Example 3: No Common Elements
This example illustrates what happens when there are no common elements between the two collections. The list remains unchanged, and `removeAll()` returns false.
import java.util.ArrayList; // Import ArrayList class
import java.util.Arrays;
public class RemoveAllExample3 {
public static void main(String[] args) {
// Create two ArrayLists with no common elements
ArrayList list1 = new ArrayList<>(Arrays.asList("apple", "banana", "cherry"));
ArrayList list2 = new ArrayList<>(Arrays.asList("grape", "kiwi", "mango"));
System.out.println("Original List 1: " + list1);
System.out.println("List 2 (elements to remove): " + list2);
// Remove all elements from list1 that are also in list2
boolean removed = list1.removeAll(list2);
System.out.println("List 1 after removal: " + list1);
System.out.println("Was the list modified? " + removed);
}
}
Original List 1: [apple, banana, cherry]
List 2 (elements to remove): [grape, kiwi, mango]
List 1 after removal: [apple, banana, cherry]
Was the list modified? false
Explanation: Because there are no common elements between list1
and list2
, nothing is removed. The original list remains unchanged, so the returned boolean value is false
.