Top Description Fields Constructors Methods
org.jvnet.hk2.component

public Class MultiMap<K, V>

extends Object
implements Serializable, Cloneable
Class Inheritance
All Implemented Interfaces
java.lang.Cloneable, java.io.Serializable
Type Parameters
<K>
The key of the multi-map
<V>
The types in the multi-map
Imports
java.io.Serializable, java.security.AccessController, .PrivilegedAction, java.util.Collection, .Collections, .LinkedHashMap, .LinkedList, .List, .Map, .Set

Map from a key to multiple values. Order is significant among values, and null values are allowed, although null keys are not.
Authors
Kohsuke Kawaguchi, Jerome Dochez

Field Summary

Modifier and TypeField and Description
private static final String
private static final long
serialVersionUID

For serialization

private final Map<K, List<V>>

Constructor Summary

AccessConstructor and Description
public
MultiMap()

Creates an empty multi-map with default concurrency controls

public
MultiMap(MultiMap<K, V>
map to copy
base
)

Copy constructor.

Method Summary

Modifier and TypeMethod and Description
private final List<V>
_get(K
the key
k
)

Package private (for getting the raw map for direct manipulation by the habitat)

public final void
add(K
key to store the entry under
k
,
V
value to store in the k's values.
v
)

Adds one more key-value pair.

public MultiMap<K, V>
clone()

Overrides java.lang.Object.clone.

Creates a copy of the map that contains the exact same key and value set.

public boolean

Returns:

true if the map contains at least one element for these keys
contains
(K
key from top collection
k1
,
V
key (value) from inner collection
k2
)

Checks if the map contains the given key(s), also extending the search to including the sub collection.

public boolean

Returns:

true if the map contains at least one element for this key
containsKey
(K
key to test
k
)

Checks if the map contains the given key.

public Set<Map.Entry<K, List<V>>>

Returns:

a java.util.Set of java.util.Map.Entry of entries
entrySet
()

Lists up all entries.

public boolean
equals(Object another)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

public final List<V>

Returns:

Can be empty but never null. Read-only.
get
(K
key for the values
k
)

Returns the elements indexed by the provided key

private V
getFirst(K k)

public V

Returns:

null if the key has no values or it has a value but the value is null.
getOne
(K
key for the values
k
)

Gets the first value if any, or null.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

public Set<K>

Returns:

The set of keys currently available in this MultiMap. Will not return null, but may return a Set of lenght zero
keySet
()

Returns the set of keys associated with this MultiMap

public void
mergeAll(MultiMap<K, V>
The MultiMap from which to add values to this MultiMap. If null this method does nothing
another
)

This method merges all of the keys and values from another MultiMap into this MultiMap.

private List<V>
newList(Collection<? extends V> initialVals)

Creates an optionally populated list to be used as an entry in the map.

public List<V>

Returns:

the value stored under this key or null if there was none
remove
(K
key to be removed
key
)

Removes an key value from the map

public boolean

Returns:

true if there was none that was deleted
remove
(K
key to be removed
key
,
V
the entry to be removed from the key'ed list
entry
)

Removes an key value pair from the map.

public void
set(K
key for the values
k
,
Collection<? extends V>
Can be null or empty.
v
)

Replaces all the existing values associated with the key by the given value.

public void
set(K
key for the values
k
,
V
singleton value for k key

This is short for set(k,Collections.singleton(v))

v
)

Replaces all the existing values associated with the key by the given single value.

public int

Returns:

integer or 0 if the map is empty
size
()

Returns the size of the map.

public String
toCommaSeparatedString()

Returns: the map as "key=value1,key=value2,...."

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
finalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

NEWLINEback to summary
private static final String NEWLINE
serialVersionUIDback to summary
private static final long serialVersionUID

For serialization

storeback to summary
private final Map<K, List<V>> store

Constructor Detail

MultiMapback to summary
public MultiMap()

Creates an empty multi-map with default concurrency controls

MultiMapback to summary
public MultiMap(MultiMap<K, V> base)

Copy constructor.

Parameters
base:MultiMap<K, V>

map to copy

Method Detail

_getback to summary
private final List<V> _get(K k)

Package private (for getting the raw map for direct manipulation by the habitat)

Parameters
k:K

the key

addback to summary
public final void add(K k, V v)

Adds one more key-value pair.

Parameters
k:K

key to store the entry under

v:V

value to store in the k's values.

cloneback to summary
public MultiMap<K, V> clone() throws CloneNotSupportedException

Overrides java.lang.Object.clone.

Creates a copy of the map that contains the exact same key and value set. Keys and values won't cloned.

Annotations
@Override
Exceptions
CloneNotSupportedException:

Doc from java.lang.Object.clone.

if the object's class does not support the Cloneable interface. Subclasses that override the clone method can also throw this exception to indicate that an instance cannot be cloned.

containsback to summary
public boolean contains(K k1, V k2)

Checks if the map contains the given key(s), also extending the search to including the sub collection.

Parameters
k1:K

key from top collection

k2:V

key (value) from inner collection

Returns:boolean

true if the map contains at least one element for these keys

containsKeyback to summary
public boolean containsKey(K k)

Checks if the map contains the given key.

Parameters
k:K

key to test

Returns:boolean

true if the map contains at least one element for this key

entrySetback to summary
public Set<Map.Entry<K, List<V>>> entrySet()

Lists up all entries.

Returns:Set<Map.Entry<K, List<V>>>

a java.util.Set of java.util.Map.Entry of entries

equalsback to summary
public boolean equals(Object another)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Annotations
@SuppressWarnings:unchecked
@Override
getback to summary
public final List<V> get(K k)

Returns the elements indexed by the provided key

Parameters
k:K

key for the values

Returns:List<V>

Can be empty but never null. Read-only.

getFirstback to summary
private V getFirst(K k)
getOneback to summary
public V getOne(K k)

Gets the first value if any, or null.

This is useful when you know the given key only has one value and you'd like to get to that value.

Parameters
k:K

key for the values

Returns:V

null if the key has no values or it has a value but the value is null.

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Annotations
@Override
keySetback to summary
public Set<K> keySet()

Returns the set of keys associated with this MultiMap

Returns:Set<K>

The set of keys currently available in this MultiMap. Will not return null, but may return a Set of lenght zero

mergeAllback to summary
public void mergeAll(MultiMap<K, V> another)

This method merges all of the keys and values from another MultiMap into this MultiMap. If a key/value pair is found in both MultiMaps it is not re-added to this MultiMap, but is instead discarded

Parameters
another:MultiMap<K, V>

The MultiMap from which to add values to this MultiMap. If null this method does nothing

newListback to summary
private List<V> newList(Collection<? extends V> initialVals)

Creates an optionally populated list to be used as an entry in the map.

removeback to summary
public List<V> remove(K key)

Removes an key value from the map

Parameters
key:K

key to be removed

Returns:List<V>

the value stored under this key or null if there was none

removeback to summary
public boolean remove(K key, V entry)

Removes an key value pair from the map. If the list of entries for that key is empty after the remove it will be removed from the set of keys

Parameters
key:K

key to be removed

entry:V

the entry to be removed from the key'ed list

Returns:boolean

true if there was none that was deleted

setback to summary
public void set(K k, Collection<? extends V> v)

Replaces all the existing values associated with the key by the given value. If v is empty the key k will be removed from the MultiMap.

Parameters
k:K

key for the values

v:Collection<? extends V>

Can be null or empty.

setback to summary
public void set(K k, V v)

Replaces all the existing values associated with the key by the given single value.

Parameters
k:K

key for the values

v:V

singleton value for k key

This is short for set(k,Collections.singleton(v))

sizeback to summary
public int size()

Returns the size of the map. This returns the numbers of keys in the map, not the number of values

Returns:int

integer or 0 if the map is empty

toCommaSeparatedStringback to summary
public String toCommaSeparatedString()
Returns:String

the map as "key=value1,key=value2,...."

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Annotations
@Override