package moe.nea.zwirn; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.function.Function; public class Index { Map> map = new HashMap<>(); public void addToIndex(K k, V v) { find(k).add(v); } public Set keys() { return map.keySet(); } public void loadFrom(Iterable it, Function keyExtractor, Function valueExtractor) { for (T t : it) { addToIndex(keyExtractor.apply(t), valueExtractor.apply(t)); } } public Set find(K k) { return map.computeIfAbsent(k, ignored -> new HashSet<>()); } }