ezvcard.util
Class ListMultimap<K,V>

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

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 an empty multimap.
ListMultimap(int initialCapacity)
          Creates an empty multimap.
ListMultimap(ListMultimap<K,V> orig)
          Creates a copy of an existing multimap.
 
Method Summary
 void clear()
          Clears all entries from the multimap.
 boolean containsKey(K key)
          Determines whether the given key exists.
 boolean equals(Object obj)
           
 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.
 int hashCode()
           
 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()
           
 List<V> values()
          Returns all the values.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ListMultimap

public ListMultimap()
Creates an empty multimap.


ListMultimap

public ListMultimap(int initialCapacity)
Creates an empty multimap.

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

ListMultimap

public ListMultimap(ListMultimap<K,V> orig)
Creates a copy of an existing multimap.

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 to 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 List<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

hashCode

public int hashCode()
Overrides:
hashCode in class Object

equals

public boolean equals(Object obj)
Overrides:
equals in class Object


Copyright © 2012-2014 Michael Angstadt. All Rights Reserved.