It\'s a simple question, I have a simple HashMap of which i want to reverse the keys and values.
HashMap myHashMap = new HashMap<
private Map invertMap(Map map) {
Map reverseMap = new HashMap<>();
for (Map.Entry entry : map.entrySet()) {
reverseMap.put(entry.getValue(), entry.getKey());
}
return reverseMap;
}
It's important to remember that put replaces the value when called with the same key. So if you map has two keys with the same value only one of them will exist in the inverted map.