Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to remove entry from hashmap by Value?

Tags:

I need a method to remove a entryset from hashmap by it's value. Is there a simple method without iteration?

like image 898
cataschok Avatar asked Mar 02 '12 03:03

cataschok


People also ask

How do I remove a specific element from a HashMap?

HashMap remove() Method in Java remove() is an inbuilt method of HashMap class and is used to remove the mapping of any particular key from the map. It basically removes the values for any particular key in the Map. Parameters: The method takes one parameter key whose mapping is to be removed from the Map.

How remove all values from HashMap in Java?

To remove all values from HashMap, use the clear() method.

Can we remove elements from HashMap while iterating?

While iterating, check for the key at that iteration to be equal to the key specified. The entry key of the Map can be obtained with the help of entry. getKey() method. If the key matches, remove the entry of that iteration from the HashMap using remove() method.


1 Answers

There's a simple method, but it'll use iteration internally. (There's no way around that.)

map.values().remove(valueToRemove); 
like image 194
Louis Wasserman Avatar answered Nov 18 '22 20:11

Louis Wasserman