biweekly.util
Class ListMultimap<K,V>

java.lang.Object
  extended by biweekly.util.ListMultimap<K,V>
Type Parameters:
K - the key
V - the value
All Implemented Interfaces:
Iterable<Map.Entry<K,List<V>>>
Direct Known Subclasses:
ICalParameters

public class ListMultimap<K,V>
extends Object
implements Iterable<Map.Entry<K,List<V>>>

A multimap that uses List objects to store its values. The internal Map implementation is a LinkedHashMap that uses ArrayList for its values.

Author:
Michael Angstadt

Constructor Summary
ListMultimap()
          Creates a new multi map.
ListMultimap(int initialCapacity)
          Creates a new multi map.
ListMultimap(ListMultimap<K,V> orig)
          Copy constructor.
 
Method Summary
 void clear()
          Clears all entries from the multimap.
 boolean containsKey(K key)
          Determines whether the given key exists.
 V first(K key)
          Gets the first value that's associated with a key.
 List<V> get(K key)
          Gets the values associated with the key.
 Map<K,List<V>> getMap()
          Gets the underlying Map object.
 boolean isEmpty()
          Determines if the multimap is empty or not.
 Iterator<Map.Entry<K,List<V>>> iterator()
           
 Set<K> keySet()
          Returns all the keys.
 void put(K key, V value)
          Adds a value to the multimap.
 void putAll(K key, Collection<V> values)
          Adds multiple values to the multimap.
 boolean remove(K key, V value)
          Removes a particular value.
 List<V> removeAll(K key)
          Removes all the values associated with a key
 List<V> replace(K key, Collection<V> values)
          Replaces all values with the given values.
 List<V> replace(K key, V value)
          Replaces all values with the given value.
protected  K sanitizeKey(K key)
          Modifies a given key before it is used to interact with the internal map.
 int size()
          Returns the number of values in the map.
 String toString()
           
 Collection<V> values()
          Returns all the values.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ListMultimap

public ListMultimap()
Creates a new multi map.


ListMultimap

public ListMultimap(int initialCapacity)
Creates a new multi map.

Parameters:
initialCapacity - the initial capacity of the underlying map.

ListMultimap

public ListMultimap(ListMultimap<K,V> orig)
Copy constructor.

Parameters:
orig - the multimap to copy from
Method Detail

put

public void put(K key,
                V value)
Adds a value to the multimap.

Parameters:
key - the key
value - the value to add

putAll

public void putAll(K key,
                   Collection<V> values)
Adds multiple values to the multimap.

Parameters:
key - the key
values - the values to add

get

public List<V> get(K key)
Gets the values associated with the key.

Parameters:
key - the key
Returns:
the list of values or empty list if the key doesn't exist

first

public V first(K key)
Gets the first value that's associated with a key.

Parameters:
key - the key
Returns:
the first value or null if the key doesn't exist

containsKey

public boolean containsKey(K key)
Determines whether the given key exists.

Parameters:
key - the key
Returns:
true if the key exists, false if not

remove

public boolean remove(K key,
                      V value)
Removes a particular value.

Parameters:
key - the key
value - the value to remove
Returns:
true if the multimap contained the value, false if not

removeAll

public List<V> removeAll(K key)
Removes all the values associated with a key

Parameters:
key - the key to remove
Returns:
the removed values or empty list if the key doesn't exist

replace

public List<V> replace(K key,
                       V value)
Replaces all values with the given value.

Parameters:
key - the key
value - the value with which to replace all existing values, or null will remove all values
Returns:
the values that were replaced

replace

public List<V> replace(K key,
                       Collection<V> values)
Replaces all values with the given values.

Parameters:
key - the key
values - the values with which to replace all existing values
Returns:
the values that were replaced

clear

public void clear()
Clears all entries from the multimap.


keySet

public Set<K> keySet()
Returns all the keys.

Returns:
all the keys

values

public Collection<V> values()
Returns all the values.

Returns:
all the values

isEmpty

public boolean isEmpty()
Determines if the multimap is empty or not.

Returns:
true if it's empty, false if not

size

public int size()
Returns the number of values in the map.

Returns:
the number of values

getMap

public Map<K,List<V>> getMap()
Gets the underlying Map object.

Returns:
the underlying Map object

sanitizeKey

protected K sanitizeKey(K key)
Modifies a given key before it is used to interact with the internal map. This method is meant to be overridden by child classes if necessary.

Parameters:
key - the key
Returns:
the modified key (by default, the key is returned as-is)

iterator

public Iterator<Map.Entry<K,List<V>>> iterator()
Specified by:
iterator in interface Iterable<Map.Entry<K,List<V>>>

toString

public String toString()
Overrides:
toString in class Object


Copyright © 2013 Michael Angstadt. All Rights Reserved.