org.apache.pig.impl.util
Class MultiMap<K,V>

java.lang.Object
  extended by org.apache.pig.impl.util.MultiMap<K,V>
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
LinkedMultiMap, PlanEdge

public class MultiMap<K,V>
extends Object
implements Serializable

An implementation of multi-map. We can't use Apache commons MultiValueMap because it isn't serializable. And we don't want to use MultiHashMap, as it is marked deprecated. This class can't extend Map, because it needs to change the semantics of put, so that you give it one key and one value, and it either creates a new entry with the key and a new collection of value (if the is not yet in the map) or adds the values to the existing collection for the key (if the key is already in the map).

See Also:
Serialized Form

Field Summary
protected  Map<K,ArrayList<V>> mMap
           
 
Constructor Summary
MultiMap()
           
MultiMap(int size)
           
 
Method Summary
 void clear()
           
 boolean containsKey(K key)
           
 boolean containsValue(V val)
           
 List<V> get(K key)
          Get the collection of values associated with a given key.
 boolean isEmpty()
           
 Set<K> keySet()
          Get a set of all the keys in this map.
 void put(K key, Collection<V> values)
          Add a key to the map with a collection of elements.
 void put(K key, V value)
          Add an element to the map.
 V remove(K key, V value)
          Remove one value from an existing key.
 Collection<V> removeKey(K key)
          Remove all the values associated with the given key
 int size()
          Get the number of keys in the map.
 String toString()
           
 Collection<V> values()
          Get a single collection of all the values in the map.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

mMap

protected Map<K,ArrayList<V>> mMap
Constructor Detail

MultiMap

public MultiMap()

MultiMap

public MultiMap(int size)
Parameters:
size - Initial size of the map
Method Detail

put

public void put(K key,
                V value)
Add an element to the map.

Parameters:
key - The key to store the value under. If the key already exists the value will be added to the collection for that key, it will not replace the existing value (as in a standard map).
value - value to store.

put

public void put(K key,
                Collection<V> values)
Add a key to the map with a collection of elements.

Parameters:
key - The key to store the value under. If the key already exists the value will be added to the collection for that key, it will not replace the existing value (as in a standard map).
values - collection of values to store.

get

public List<V> get(K key)
Get the collection of values associated with a given key.

Parameters:
key - Key to fetch values for.
Returns:
list of values, or null if the key is not in the map.

remove

public V remove(K key,
                V value)
Remove one value from an existing key. If that is the last value for the key, then remove the key too.

Parameters:
key - Key to remove the value from.
value - Value to remove.
Returns:
The value being removed, or null if the key or value does not exist.

removeKey

public Collection<V> removeKey(K key)
Remove all the values associated with the given key

Parameters:
key - the key to be removed
Returns:
list of all value being removed

keySet

public Set<K> keySet()
Get a set of all the keys in this map.

Returns:
Set of keys.

values

public Collection<V> values()
Get a single collection of all the values in the map. All of the values in the map will be conglomerated into one collection. There will not be any duplicate removal.

Returns:
collection of values.

toString

public String toString()
Overrides:
toString in class Object

size

public int size()
Get the number of keys in the map.

Returns:
number of keys.

isEmpty

public boolean isEmpty()

clear

public void clear()

containsKey

public boolean containsKey(K key)

containsValue

public boolean containsValue(V val)


Copyright © 2007-2012 The Apache Software Foundation