The removeIf()
method is a method of the java.util.ArrayList
class that allows you to remove all elements from an ArrayList that satisfy a given predicate (a boolean-valued function). It was introduced in Java 8 as a way to remove elements from a list using a declarative approach, rather than the imperative approach used by the remove()
method.
Here is an example of how to use the removeIf()
method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import java.util.*; class Main { public static void main(String arg[]) { List<String> names = new ArrayList<>(); names.add("Alice"); names.add("Bob"); names.add("Charlie"); names.removeIf(name -> name.startsWith("A")); for (String name : names) { System.out.println(name); } } } |
Output:
1 2 3 4 | Bob Charlie |
In this example, the removeIf()
method will remove the element “Alice” from the names
list because it starts with the letter “A”. The resulting list will be [“Bob”, “Charlie”].
The removeIf()
method takes a Predicate
as an argument. A Predicate
is a functional interface that represents a boolean-valued function of one argument. It specifies the condition that elements must meet to be removed from the collection. In the example above, the Predicate
is a lambda expression that checks whether a name starts with the letter “A”.
You can also use the removeIf()
method with a method reference or a traditional anonymous inner class instead of a lambda expression. Here is an example using a method reference.
Example: Removing elements from a list of strings that are empty or contain only whitespace:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import java.util.*; class Main { public static void main(String arg[]) { List<String> names = new ArrayList<>(); names.add("Alice"); names.add(""); names.add("Bob"); names.add(""); names.add("Charlie"); names.removeIf(String::isEmpty); for (String name : names) { System.out.println(name); } } } |
Output:
1 2 3 4 5 | Alice Bob Charlie |
This will remove all empty strings from the names
list.
Example: Removing elements from a set of integers that are even:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import java.util.*; class Main { public static void main(String arg[]) { Set<Integer> numbers = new HashSet<>(); numbers.add(1); numbers.add(2); numbers.add(3); numbers.add(4); numbers.removeIf(n -> n % 2 == 0); for (int number : numbers) { System.out.println(number); } } } |
Output:
1 2 3 4 | 1 3 |
After calling removeIf()
, the numbers
set will contain [1, 3].
Example: Removing elements from a map of strings to integers that have a value greater than 10:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import java.util.*; class Main { public static void main(String arg[]) { Map<String, Integer> values = new HashMap<>(); values.put("Alice", 5); values.put("Bob", 10); values.put("Charlie", 15); values.entrySet().removeIf(entry -> entry.getValue() > 10); for (Map.Entry<String, Integer> value : values.entrySet()) { System.out.println("Key : " + value.getKey() + ", Value : " + value.getValue()); } } } |
Output:
1 2 3 4 | Key : Bob, Value : 10 Key : Alice, Value : 5 |
After calling removeIf()
, the values
map will contain only the key-value pair (“Alice”, 5).
I hope these examples help clarify how to use the removeIf()
method in Java! Let me know if you have any questions.