uk.co.codera.lang.collection.MultiMap Maven / Gradle / Ivy
package uk.co.codera.lang.collection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MultiMap {
private final Map> values;
public MultiMap() {
this.values = new HashMap<>();
}
public void put(K key, V value) {
if (!this.values.containsKey(key)) {
this.values.put(key, new ArrayList());
}
this.values.get(key).add(value);
}
public boolean remove(K key, V value) {
return get(key).remove(value);
}
public Collection get(K key) {
if (this.values.containsKey(key)) {
return this.values.get(key);
}
return Collections.emptyList();
}
}