io.polaris.core.map.SetMultiMap Maven / Gradle / Ivy
package io.polaris.core.map;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;
/**
* @author Qt
* @since 1.8
*/
public class SetMultiMap extends BaseMultiMap> {
public SetMultiMap(Map> raw, Supplier> supplier) {
super(raw, supplier);
}
public SetMultiMap(Map> raw) {
super(raw, HashSet::new);
}
public SetMultiMap(Supplier> supplier) {
super(new HashMap<>(), supplier);
}
public SetMultiMap() {
super(new HashMap<>(), HashSet::new);
}
@Override
public V getOne(Object key) {
Set c = get(key);
if (c == null || c.isEmpty()) {
return null;
}
return c.iterator().next();
}
@Override
public Set putOne(K key, V value) {
Set c = get(key);
if (c == null) {
c = supplier.get();
put(key, c);
}
c.add(value);
return c;
}
}