In this post, we’ll explore how the removeIf() Method in Java can be used to efficiently remove elements from a Collection based on a given condition. Introduced in Java 8 as part of the java.util.Collection interface, the removeIf() method leverages functional programming by accepting a Predicate- a powerful way to define conditions in a clean, declarative style.
Since removeIf() is defined in the Collection interface, you can use it with Collections like ArrayList, HashSet that implements Collection interface. To use it with HashMap you will have to get a collection view of the Map, since Map doesn't implement Collection interface.
Syntax of removeIf() Method in Java
boolean removeIf(Predicate<? super E> filter)
Parameter: A Predicate functional interface that evaluates each element and returns true if the element should be removed.
Return Value: Returns true if any elements were removed, otherwise false.
Removing elements from ArrayList using removeIf() method
In this example we'll have a list of cities and we'll remove elements from this ArrayList using removeIf() method. In passed Predicate if the condition holds true for any of the elements, then that element is removed from the list.
import java.util.ArrayList;
import java.util.List;
public class RemoveIf {
public static void main(String[] args) {
List<String> cityList = new ArrayList<String>();
cityList.add("Delhi");
cityList.add("Mumbai");
cityList.add("Kolkata");
cityList.add("Hyderabad");
cityList.add("Bangalore");
cityList.add("Mumbai");
System.out.println("*** List Initially ***");
System.out.println(cityList);
cityList.removeIf(p -> p.equalsIgnoreCase("Hyderabad") ||
p.equalsIgnoreCase("Bangalore"));
System.out.println("After Removal " + cityList);
}
}
Output
*** List Initially *** [Delhi, Mumbai, Kolkata, Hyderabad, Bangalore, Mumbai] After Removal [Delhi, Mumbai, Kolkata, Mumbai]
Removing elements from HashSet using removeIf() method
You can use removeIf() method with HashSet also to remove elements from the Set based on the passed condition. In the given example condition is to remove cities having name of length more than 6.
import java.util.HashSet;
import java.util.Set;
public class RemoveIf {
public static void main(String[] args) {
// creating a HashSet
Set<String> citySet = new HashSet<String>();
// Adding elements
citySet.add("London");
citySet.add("Tokyo");
citySet.add("New Delhi");
citySet.add("Beijing");
citySet.add("Nairobi");
System.out.println("*** Set Initially ***");
System.out.println(citySet);
// Remove all cities having length more than 6
citySet.removeIf(e -> e.length() > 6);
System.out.println("After Removal " + citySet);
}
}
Output
*** Set Initially *** [Beijing, New Delhi, Nairobi, Tokyo, London] After Removal [Tokyo, London]
Removing elements from HashMap using removeIf() method
To use removeIf() method with a Map you have to get Collection view (like entrySet(), keySet(), or values()) of a Map. After getting the Collection view of a Map removeIf() method can be used.
import java.util.HashMap;
import java.util.Map;
public class RemoveIf {
public static void main(String[] args) {
Map<String, String> cityMap = new HashMap<String, String>();
// Adding elements
cityMap.put("1","New York City" );
cityMap.put("2", "New Delhi");
cityMap.put("3", "Mumbai");
cityMap.put("4", "Beijing");
cityMap.put("5", "Berlin");
System.out.println("*** Map Initially ***");
System.out.println(cityMap);
// Use entrySet to get Set view of all Map entries.
// Remove entry from Map based on the condition for value.
cityMap.entrySet().removeIf(entry -> entry.getValue().equals("Beijing"));
System.out.println("*** Map After removal ***");
System.out.println(cityMap);
}
}
Output
*** Map Initially ***
{1=New York City, 2=New Delhi, 3=Mumbai, 4=Beijing, 5=Berlin}
*** Map After removal ***
{1=New York City, 2=New Delhi, 3=Mumbai, 5=Berlin}
That's all for this topic removeIf() Method in Java Collection With Examples. If you have any doubt or any suggestions to make please drop a comment. Thanks!
Related Topics
You may also like-
No comments:
Post a Comment