How To Take Away Telephone Substitution Value Pairs Or Entries From Hashmap Inwards Coffee 8

In the final article, I receive got shown how to withdraw key/value pairs from a map inwards Java based upon unopen to weather e.g. withdraw all entries where values are greater than 10$. I receive got told you lot that you lot must usage the Iterator's remove() method for removing mapping spell iteration to avoid ConcurrentModificationException, but that's non required anymore inwards Java 8. From JDK 8 onwards, you lot tin selectively withdraw 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 comprise concrete methods inwards shape of static together with default methods.

One of such methods is removeIf() method of Collection shape which allows you lot to withdraw entries based upon unopen to status or predicate. It takes a lambda expression, which tin live on used to render the predicate you lot want. For example, you lot tin rewrite the higher upwards code inwards Java 8 every bit shown inwards 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 exactly call removeIf() method which takes a Predicate. Just transcend the status you lot desire to banking concern stand upwards for for removing values from the map.

If you lot desire to essay keys from the Map for removing the mapping, you lot tin exactly telephone telephone the keySet() or entrySet() methods together with 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 afterwards 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 afterwards 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 slow it is to withdraw telephone commutation value pairs based upon unopen to status inwards Java. You tin usage the newly added removeIf() method from Collection to accomplish that.

So, all nosotros did on our whole program, you lot tin exactly create it inwards exactly i job inwards Java 8. That's the beauty of Java 8. I strongly propose you lot read a expert mass on Java 8 e.g. Java 8 inwards Action to larn to a greater extent than of such useful tricks.

That's all well-nigh how to withdraw telephone commutation value twain from a HashMap inwards Java 8. You tin run into that forthwith you lot tin safely withdraw entries based upon unopen to weather without iterating over them, so you lot don't take to usage the Iterator's remove() method to avoid ConcurrentModificationException inwards Java spell removing entries from HashMap.

Further Learning
The Complete Java MasterClass
books)
  • What is default method inwards Java 8? (example)
  • How to bring together String inwards Java 8 (example)
  • How to usage filter() method inwards Java 8 (tutorial)
  • How to format/parse the appointment amongst LocalDateTime inwards Java 8? (tutorial)
  • How to usage Stream shape inwards Java 8 (tutorial)
  • How to convert List to Map inwards Java 8 (solution)
  • Diference betwixt abstract shape together with interface inwards Java 8? (answer)
  • 20 Examples of Date together with Time inwards Java 8 (tutorial)
  • How to usage peek() method inwards Java 8 (example)
  • How to variety the map yesteryear keys inwards Java 8? (example)
  • How to variety the may yesteryear values inwards Java 8? (example)
  • 10 examples of Optionals inwards Java 8? (example)

  • Thanks for reading this article so far. If you lot similar this article so delight portion amongst your friends together with colleagues. If you lot receive got whatever questions or suggestions so delight driblet a comment. 

    Sumber https://javarevisited.blogspot.com/

    0 Response to "How To Take Away Telephone Substitution Value Pairs Or Entries From Hashmap Inwards Coffee 8"

    Post a Comment

    Iklan Atas Artikel

    Iklan Tengah Artikel 1

    Iklan Tengah Artikel 2

    Iklan Bawah Artikel