I have a hashmap with some keys pointing to same values. I want to find all the values that are equal and print the corresponding keys.
This is the current code that I
I think other answers already good to solve the question, i support another method to do just for extended thinking.This method need use Guava's MutliMap
interface:
// init the input map
Map map = new HashMap<>();
map.put("hello", "0123");
map.put("hola", "0123");
map.put("kosta", "0123");
map.put("da", "03");
map.put("notda", "013");
map.put("twins2", "01");
map.put("twins22", "01");
// swap key and value of the input map,since different key has same value
// so we need Multimap
Multimap container = ArrayListMultimap.create();
map.entrySet().forEach(entry -> container.put(entry.getValue(), entry.getKey()));
container.keySet().stream()
.filter(s -> container.get(s).size() > 1).
forEach(System.out::println);
output:
01
0123