How To Take Away A Key, Value, Or Entry From An Hashmap Inwards Coffee Eight - Illustration Tutorial

Advertisement

Masukkan script iklan 970x90px

How To Take Away A Key, Value, Or Entry From An Hashmap Inwards Coffee Eight - Illustration Tutorial

Rabu, 28 Maret 2001

In the terminal article, I direct maintain shown how to take away key/value pairs from a map inwards Java based upon some weather condition similar take away all entries where values are greater than some values. I direct maintain told y'all that y'all must usage the Iterator's remove() method for removing mapping acre iteration to avoid ConcurrentModificationException, but that's non required anymore inwards Java 8. From JDK 8 onwards, y'all 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 right away incorporate concrete methods inwards cast of static as well as default methods.

One of such methods is removeIf() method of Collection cast which allows y'all to take away entries based upon some status or predicate.

It takes a lambda expression, which tin survive used to render the predicate y'all want. For example, y'all tin rewrite the inwards a higher house code inwards Java 8 every bit shown inwards the next example:

Since hither nosotros request to compare values, I direct maintain called the values() method which returns a collection of values.

Once y'all got that, y'all tin exactly call removeIf() method which takes a Predicate. Just overstep the status y'all desire to depository fiscal establishment stand upward for for removing values from the map. Btw, if y'all are non familiar amongst novel functional programming API added on Java 8 so I advise y'all to start become through a comprehensive as well as up-to-date Java class like The Complete Java MasterClass on Udemy.



It covers all of import topics for Java developer as well as every bit good regularly updated to the latest Java versions. It's every bit good real inexpensive as well as y'all tin acquire it inwards exactly $10 on several Udemy flash sales which happens all approximately the year.

Btw, If y'all desire to attempt keys from the Map for removing the mapping, y'all tin exactly telephone telephone the keySet() or entrySet() methods as well as apply the removeIf() telephone telephone every bit shown below:

 inwards Java based upon some weather condition similar take away all entries where values are greater than  How to Remove a Key, Value, or Entry from an HashMap inwards Java 8 - Example Tutorial


Also hither is to a greater extent than code to re-create glue as well as endeavor yourself:
System.out.println("map earlier removing values: " + priceMap); priceMap.entrySet().removeIf( e -> e.getValue().compareTo(Double.valueOf(39.00)) > 0); System.out.println("map subsequently 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 subsequently removing values: {Java SE 8 for Really Impatient=31.99,  Java Concurrency In Practice=30.67, Head First Java=29.02}

You tin run across how slow it is to take away key-value pairs based upon some status inwards Java. You tin usage the newly added removeIf() method from Collection to arrive at that.

So, all nosotros did on our whole program, y'all tin exactly produce it inwards exactly i describe of piece of employment inwards Java 8. That's the beauty of Java 8. I strongly advise y'all read a skillful mass on Java 8 similar Java 8 inwards Action to larn to a greater extent than of such useful tricks.

 inwards Java based upon some weather condition similar take away all entries where values are greater than  How to Remove a Key, Value, or Entry from an HashMap inwards Java 8 - Example Tutorial



That's all nearly how to take away a key-value twosome from a HashMap inwards Java 8. You tin run across that right away y'all tin safely take away entries based upon some weather condition without iterating over them, thence y'all don't request to usage the Iterator's remove() method to avoid ConcurrentModificationException inwards Java acre removing entries from HashMap.

Further Learning
The Complete Java MasterClass
books)
  • What is the 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 cast inwards Java 8 (tutorial)
  • How to convert List to Map inwards Java 8 (solution)
  • Difference betwixt abstract cast as well as interface inwards Java 8? (answer)
  • 20 Examples of Date as well as 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 y'all similar this article so delight part amongst your friends as well as colleagues. If y'all direct maintain whatever questions or suggestions so delight drib a comment.