pl.edu.agh.cast.common.collections
Class MultiMap<K,V>

java.lang.Object
  extended by pl.edu.agh.cast.common.collections.MultiMap<K,V>
Type Parameters:
K - key type
V - value type
All Implemented Interfaces:
Serializable

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

This class implements a multi-map functionality. Multi-map is a map which allows for multiple values to be kept under one key. This implementation (by default) uses HashMap with LinkedLists, however different Map implementation may be provided during construction. WARNING: This collection does not implement Collection interface and is not in the Java Collections hierarchy.

Author:
AGH CAST Team
See Also:
Serialized Form

Constructor Summary
MultiMap()
          Creates new instance using HashMap as underlying map.
MultiMap(boolean allowDuplicates)
          Creates new instance using HashMap as underlying map.
MultiMap(Map<K,List<V>> map)
          Creates new instance using map as underlying map.
MultiMap(Map<K,List<V>> map, boolean allowDuplicates)
          Creates new instance using map as underlying map.
 
Method Summary
 boolean equals(Object obj)
          
 List<V> get(K key)
          Returns a (modifiable) list of values stored under given key.
 Map<K,List<V>> getMap()
          Returns an unmodifiable view of the underlying map.
 int hashCode()
          
 boolean isAllowDuplicates()
          Checks if duplicate values are allowed for one key.
 List<V> put(K key, V... values)
          Adds values to the key entry.
 List<V> putAll(K key, Collection<V> values)
          Adds a collection of values to the key entry.
 List<V> remove(K key, V value)
          Removes a value for given key.
 String toString()
          
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

MultiMap

public MultiMap()
Creates new instance using HashMap as underlying map. No duplicate values for single key are allowed.


MultiMap

public MultiMap(boolean allowDuplicates)
Creates new instance using HashMap as underlying map. Duplicate values for single key are allowed if allowDuplicates is true.

Parameters:
allowDuplicates - if true than duplicate values are allowed for one key. Defaults to false

MultiMap

public MultiMap(Map<K,List<V>> map)
Creates new instance using map as underlying map. No duplicate values for single key are allowed.

Parameters:
map - the underlying map

MultiMap

public MultiMap(Map<K,List<V>> map,
                boolean allowDuplicates)
Creates new instance using map as underlying map. Duplicate values for single key are allowed if allowDuplicates is true.

Parameters:
map - the underlying map
allowDuplicates - if true than duplicate values are allowed for one key. Defaults to false
Method Detail

getMap

public Map<K,List<V>> getMap()
Returns an unmodifiable view of the underlying map. However, the lists that are the values of the map are modifiable.

Returns:
unmodifiable view of the underlying map

put

public List<V> put(K key,
                   V... values)
Adds values to the key entry. Behavior may differ depending on the isAllowDuplicates() value.

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

putAll

public List<V> putAll(K key,
                      Collection<V> values)
Adds a collection of values to the key entry. Behavior may differ depending on the isAllowDuplicates() value.

Parameters:
key - the key
values - the collection of values to add
Returns:
list

get

public List<V> get(K key)
Returns a (modifiable) list of values stored under given key.

Parameters:
key - the key
Returns:
list of values

remove

public List<V> remove(K key,
                      V value)
Removes a value for given key. If the last element is removed, the key entry is also removed.

Parameters:
key - the key
value - the value to remove
Returns:
the reduced list of values or null if there are no more values for the key

isAllowDuplicates

public boolean isAllowDuplicates()
Checks if duplicate values are allowed for one key.

Returns:
flag indicating if duplicate values are allowed

equals

public boolean equals(Object obj)

Overrides:
equals in class Object
See Also:
Object.equals(java.lang.Object)

hashCode

public int hashCode()

Overrides:
hashCode in class Object
See Also:
Object.hashCode()

toString

public String toString()

Overrides:
toString in class Object
See Also:
Object.toString()


Copyright © 2007-2009 IISG AGH-UST Krakow, Poland. All Rights Reserved.