Skip to content
Advertisement

clear all values of hashmap except two key/value pair

I have a HashMap with hundred of key/value pairs.

Now I have to delete all key/values except 2 key/value. I have use this way :

JavaScript

But java 8 introduced removeIf() for these kind of condition. How can I solve this problem with removeIf() method ?

Advertisement

Answer

You’ll need to it like this :

JavaScript

It will iterate over the keys and remove the ones for those the key is not one of or two required keys

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement