Friday, August 18, 2017

How to Remove key value pairs or Entries from HashMap in Java 8

In the last article, I have shown how to remove key/value pairs from a map in Java based upon some conditions e.g. remove all entries where values are greater than 10$. I have told you that you must use the Iterator's remove() method for removing mapping while iteration to avoid ConcurrentModificationException, but that's not required anymore in Java 8. From JDK 8 onwards, you can selectively remove mapping without iterating over Map. The JDK 8 API has added several new methods to existing interfaces e.g. java.util.Collection, java.util.Map, Comparator etc because the interface can now contain concrete methods in form of static and default methods.

One of such methods is removeIf() method of Collection class which allows you to remove entries based upon some condition or predicate. It takes a lambda expression, which can be used to supply the predicate you want. For example, you can rewrite the above code in Java 8 as shown in the following example:

priceMap.values().removeIf( d -> d.compareTo(Double.valueOf(39.00)) > 0);

Since here we need to compare values, I have called the values() method which returns a collection of values. Once you got that, you can simply call removeIf() method which takes a Predicate. Just pass the condition you want to check for removing values from the map.

If you want to test keys from the Map for removing the mapping, you can just call the keySet() or entrySet() methods and apply the removeIf() call as shown below:

System.out.println("map before removing values: " + priceMap);
priceMap.entrySet().removeIf( e -> e.getValue().compareTo(Double.valueOf(39.00)) > 0);
System.out.println("map after removing values: " + priceMap);

map before 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 after removing values: {Java SE 8 for Really Impatient=31.99,
 Java Concurrency In Practice=30.67, Head First Java=29.02}

You can see how easy it is to remove key value pairs based upon some condition in Java. You can use the newly added removeIf() method from Collection to achieve that.

So, all we did on our whole program, you can simply do it in just one line in Java 8. That's the beauty of Java 8. I strongly suggest you read a good book on Java 8 e.g. Java 8 in Action to learn more of such useful tricks.

That's all about how to remove key value pair from a HashMap in Java 8. You can see that now you can safely remove entries based upon some conditions without iterating over them, hence you don't need to use the Iterator's remove() method to avoid ConcurrentModificationException in Java while removing entries from HashMap.

Further Reading
What's New in Java 8
Java SE 8 for Really Impatient
From Collections to Streams in Java 8 Using Lambda Expressions

Related Java 8 Tutorials
If you are interested in learning more about new features of Java 8, here are my earlier articles covering some of the important concepts of Java 8:

  • 5 Books to Learn Java 8 from Scratch (books)
  • What is default method in Java 8? (example)
  • How to join String in Java 8 (example)
  • How to use filter() method in Java 8 (tutorial)
  • How to format/parse the date with LocalDateTime in Java 8? (tutorial)
  • How to use Stream class in Java 8 (tutorial)
  • How to convert List to Map in Java 8 (solution)
  • Diference between abstract class and interface in Java 8? (answer)
  • 20 Examples of Date and Time in Java 8 (tutorial)
  • How to use peek() method in Java 8 (example)
  • How to sort the map by keys in Java 8? (example)
  • How to sort the may by values in Java 8? (example)
  • 10 examples of Optionals in Java 8? (example)

Thanks for reading this article so far. If you like this article then please share with your friends and colleagues. If you have any questions or suggestions then please drop a comment. 

1 comment :

Anonymous said...

( d -> d.compareTo ) What does d means here?

Post a Comment