1 package edu.berkeley.sbp.util;
4 /** a mapping from keys of type <tt>K</tt> to <i>sets</i> of values of type <tt>T</tt> */
5 public final class HashMapBag<K,V> implements MapBag<K,V> {
7 private final HashMap<K,HashSet<V>> hm = new HashMap<K,HashSet<V>>();
9 public void add(K k, V v) {
10 HashSet<V> hs = hm.get(k);
11 if (hs==null) hm.put(k, hs = new HashSet<V>());
15 public void addAll(K k, Iterable<V> iv) {
16 for(V v : iv) add(k, v);
19 public HashSet<V> getAll(K k) {
20 HashSet<V> ret = hm.get(k);
21 if (ret==null) return new HashSet<V>();
25 public void remove(K k, V v) {
26 if (hm.get(k)==null) return;
30 public void clear() { hm.clear(); }
32 public boolean contains(K k, V v) {
33 return hm.get(k)!=null && hm.get(k).contains(v);
36 public Iterator<K> iterator() { return hm.keySet().iterator(); }