In this post we'll see how to use removeIf()
method to remove elements from the Collection that satisfy the
given condition. The removeIf() method is included in java.util.Collection
interface in Java 8. Since it is
part of Collection interface so 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.
Java removeIf() method syntax
boolean removeIf(Predicate<? super E> filter)
Parameter passed to this method is of type Predicate
functional interface, which can be implemented as a boolean-valued function with one argument.
Method returns true if any elements were removed.
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 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