dk.brics.xact.analysis.util
Class MultiMap<K,V>

java.lang.Object
  extended by dk.brics.xact.analysis.util.MultiMap<K,V>
Type Parameters:
K - key type
V - value type

public class MultiMap<K,V>
extends Object

Maps keys to sets of values.

For practical reasons, this does not implement Map<K, Set<V>>, although it is conceptually that type of map.


Constructor Summary
MultiMap()
           
 
Method Summary
 boolean add(K key, V value)
          Adds a key-value pair to the multimap.
 boolean addAll(K key, Collection<? extends V> values)
          Adds a key-value pair for each value in the specified set.
 void clear()
          Removes all key-value pairs.
 boolean contains(K key, V value)
          Determines if the multimap contains the specified key-value pair.
 int count(K key)
           
 K getAnyKey()
          Returns any of the keys in the map, that has at least one associated value.
 V getAnyValue(K key)
          Returns any of the values associated with the specified key, or null if no values are associated with the key.
 Set<V> getValues(K key)
          Returns a copy of the set of items associated with the specified key.
 Set<V> getView(K key)
          Returns a read-only view of the items associated with the specified key.
 Set<K> keySet()
          Returns the set of keys with at least one associated value, backed by the multimap.
 int numberOfKeys()
           
<K2 extends K,V2 extends V>
boolean
putAll(MultiMap<K2,V2> other)
          Adds all key-value pairs from the other map to this map.
 boolean remove(K key, V value)
          Removes a key-value pair from the multimap.
 boolean removeAll(K key)
          Removes all key-value pairs with the specified key.
 int size()
           
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

MultiMap

public MultiMap()
Method Detail

add

public boolean add(K key,
                   V value)
Adds a key-value pair to the multimap.

Returns:
true if the pair was added, and false if it was already there.

addAll

public boolean addAll(K key,
                      Collection<? extends V> values)
Adds a key-value pair for each value in the specified set.

Parameters:
key - the key to associate with each of the specified values.
values - the values to associate with the specified key.
Returns:
true if at least one new mapping was created.

clear

public void clear()
Removes all key-value pairs.


contains

public boolean contains(K key,
                        V value)
Determines if the multimap contains the specified key-value pair.


count

public int count(K key)
Returns:
the number of values associated with the given key.

getAnyKey

public K getAnyKey()
Returns any of the keys in the map, that has at least one associated value.

Returns:
a key from the map, or null if the map is empty.

getAnyValue

public V getAnyValue(K key)
Returns any of the values associated with the specified key, or null if no values are associated with the key.


getValues

public Set<V> getValues(K key)
Returns a copy of the set of items associated with the specified key. Changes to the multimap will not affect the returned set, and vice versa.

Returns:
a set independant of the multimap. Null is never returned.

getView

public Set<V> getView(K key)
Returns a read-only view of the items associated with the specified key. Changes to the multimap may or may not propagate to the view!

In the current implementation, changes always propagate until the specified key has no associated values, at which point all existing views of that key are cleared and disconnected from the multimap.

Returns:
a read-only set partly backed by the multimap. Null is never returned.

keySet

public Set<K> keySet()
Returns the set of keys with at least one associated value, backed by the multimap.

Keys may be removed from the returned set, but attempting to add a key will throw an UnsupportedOperationException. Removing a key will disconnect all existing views of that particular key from the multimap.

Returns:
a view of the keys in the multimap.

numberOfKeys

public int numberOfKeys()
Returns:
the number of keys with at least one associated value in the multimap.

putAll

public <K2 extends K,V2 extends V> boolean putAll(MultiMap<K2,V2> other)
Adds all key-value pairs from the other map to this map. Existing mappings are not removed.

Parameters:
other - another multi map.
Returns:
true if at least one new mapping was created.

remove

public boolean remove(K key,
                      V value)
Removes a key-value pair from the multimap.

Parameters:
key -
value -
Returns:
true if the pair was removed, and false if it was not there in the first place.

removeAll

public boolean removeAll(K key)
Removes all key-value pairs with the specified key.

Parameters:
key - key to remove from the map.
Returns:
true if the map changed.

size

public int size()
Returns:
the number of key-value pars in the map.

toString

public String toString()
Overrides:
toString in class Object


Copyright © 2005-2011 Aarhus University.