How To Take Fundamental Value Pairs Or Entries From Hashmap Inward Coffee 8
In the final article, I receive got shown how to take away key/value pairs from a map inward Java based upon approximately weather condition e.g. take away all entries where values are greater than 10$. I receive got told you lot that you lot must job the Iterator's remove() method for removing mapping piece iteration to avoid ConcurrentModificationException, but that's non required anymore inward Java 8. From JDK 8 onwards, you lot tin selectively take away mapping without iterating over Map. The JDK 8 API has added several novel methods to existing interfaces e.g. java.util.Collection, java.util.Map, Comparator etc because the interface tin forthwith incorporate concrete methods inward shape of static as well as default methods.
One of such methods is removeIf() method of Collection course of teaching which allows you lot to take away entries based upon approximately status or predicate. It takes a lambda expression, which tin hold out used to furnish the predicate you lot want. For example, you lot tin rewrite the inward a higher house code inward Java 8 every bit shown inward the next example:
Since hither nosotros take to compare values, I receive got called the values() method which returns a collection of values. Once you lot got that, you lot tin precisely call removeIf() method which takes a Predicate. Just move past times the status you lot desire to banking concern lucifer for removing values from the map.
If you lot desire to seek keys from the Map for removing the mapping, you lot tin precisely telephone telephone the keySet() or entrySet() methods as well as apply the removeIf() telephone telephone every bit shown below:
You tin run into how slowly it is to take away cardinal value pairs based upon approximately status inward Java. You tin job the newly added removeIf() method from Collection to hit that.
So, all nosotros did on our whole program, you lot tin precisely produce it inward precisely 1 trouble inward Java 8. That's the beauty of Java 8. I strongly propose you lot read a skillful majority on Java 8 e.g. Java 8 inward Action to acquire to a greater extent than of such useful tricks.
That's all close how to take away cardinal value duo from a HashMap inward Java 8. You tin run into that forthwith you lot tin safely take away entries based upon approximately weather condition without iterating over them, so you lot don't take to job the Iterator's remove() method to avoid ConcurrentModificationException inward Java piece removing entries from HashMap.
Further Learning
The Complete Java MasterClass
books)What is default method inward Java 8? (example) How to bring together String inward Java 8 (example) How to job filter() method inward Java 8 (tutorial) How to format/parse the appointment amongst LocalDateTime inward Java 8? (tutorial) How to job Stream course of teaching inward Java 8 (tutorial) How to convert List to Map inward Java 8 (solution) Diference betwixt abstract course of teaching as well as interface inward Java 8? (answer) 20 Examples of Date as well as Time inward Java 8 (tutorial) How to job peek() method inward Java 8 (example) How to kind the map past times keys inward Java 8? (example) How to kind the may past times values inward Java 8? (example) 10 examples of Optionals inward Java 8? (example)
Thanks for reading this article so far. If you lot similar this article as well as so delight percentage amongst your friends as well as colleagues. If you lot receive got whatsoever questions or suggestions as well as so delight driblet a comment.
One of such methods is removeIf() method of Collection course of teaching which allows you lot to take away entries based upon approximately status or predicate. It takes a lambda expression, which tin hold out used to furnish the predicate you lot want. For example, you lot tin rewrite the inward a higher house code inward Java 8 every bit shown inward the next example:
priceMap.values().removeIf( d -> d.compareTo(Double.valueOf(39.00)) > 0);
Since hither nosotros take to compare values, I receive got called the values() method which returns a collection of values. Once you lot got that, you lot tin precisely call removeIf() method which takes a Predicate. Just move past times the status you lot desire to banking concern lucifer for removing values from the map.
If you lot desire to seek keys from the Map for removing the mapping, you lot tin precisely telephone telephone the keySet() or entrySet() methods as well as apply the removeIf() telephone telephone every bit shown below:
System.out.println("map earlier removing values: " + priceMap); priceMap.entrySet().removeIf( e -> e.getValue().compareTo(Double.valueOf(39.00)) > 0); System.out.println("map later removing values: " + priceMap); Output: map earlier removing values: {Java SE 8 for Really Impatient=31.99, Head First Design Pattern=39.05, Java Concurrency In Practice=30.67, Effective Java=41.79, Head First Java=29.02} map later removing values: {Java SE 8 for Really Impatient=31.99, Java Concurrency In Practice=30.67, Head First Java=29.02}
You tin run into how slowly it is to take away cardinal value pairs based upon approximately status inward Java. You tin job the newly added removeIf() method from Collection to hit that.
So, all nosotros did on our whole program, you lot tin precisely produce it inward precisely 1 trouble inward Java 8. That's the beauty of Java 8. I strongly propose you lot read a skillful majority on Java 8 e.g. Java 8 inward Action to acquire to a greater extent than of such useful tricks.
That's all close how to take away cardinal value duo from a HashMap inward Java 8. You tin run into that forthwith you lot tin safely take away entries based upon approximately weather condition without iterating over them, so you lot don't take to job the Iterator's remove() method to avoid ConcurrentModificationException inward Java piece removing entries from HashMap.
Further Learning
The Complete Java MasterClass
books)
Thanks for reading this article so far. If you lot similar this article as well as so delight percentage amongst your friends as well as colleagues. If you lot receive got whatsoever questions or suggestions as well as so delight driblet a comment.
Komentar
Posting Komentar