HashMap: How to get keys from value in Java?

June 19, 2017

To get a set of keys from a value in Java, we can use the below method:

The method getKeysByValue accepts two arguments. The first one is a map in which we want to look for the keys. The second is a searched value. At the beginning of it, we define the set named keys. Nextly, we iterate through the map using its entries. We use the equals() method (from Objects class) to check if values are the same. Then, if we got the searched one we add it to the keys set.

Click here to add a comment

Leave a comment: