Added utilities and a hashmap inverter.

This commit is contained in:
2020-04-19 18:17:17 -05:00
parent 08a23c1bf2
commit 23f761c36f
2 changed files with 80 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
package ca.recrown.islandsurvivalcraft;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;
public class Utilities {
public static <K, V> HashMap<V, ArrayList<K>> invertHashMap(HashMap<K, V> hashMap) {
HashMap<V, ArrayList<K>> res = new HashMap<>();
for (Entry<K, V> entry : hashMap.entrySet()) {
if (!res.containsKey(entry.getValue())) {
ArrayList<K> l = new ArrayList<K>();
l.add(entry.getKey());
res.put(entry.getValue(), l);
} else {
res.get(entry.getValue()).add(entry.getKey());
}
}
return res;
}
}