Quantcast
Channel: Java Hashmap: How to get key from value? - Stack Overflow
Viewing all articles
Browse latest Browse all 41

Answer by Shashank Bodkhe for Java Hashmap: How to get key from value?

$
0
0

Simplest utility method to fetch a key of a given value from a Map:

public static void fetchValue(Map<String, Integer> map, Integer i){   Stream stream = map.entrySet().stream().filter(val-> val.getValue().equals(i)).map(Map.Entry::getKey);stream.forEach(System.out::println);    }

detailed explaination:

  1. Method fetchValue accepts the map, which has String as key and Integer as value.

  2. Then we use entryset().stream() to convert result into a stream.

  3. Next we use filter (intermediate operation) which gives us a value that is equal to the second argument.

  4. Finally, we use forEach(final operation) to print our end result.


Viewing all articles
Browse latest Browse all 41

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>