public class TreeMultiMap<K,V> extends Object implements SortedMultiMap<K,V>, Serializable
SortedMultiMap implementation. Map elements are
sorted according to the natural ordering of the keys, or
by a Comparator provided to the constructor.
This implementation provides guaranteed log(n) time cost for the containsKey, get, put and remove operations. The
remaining operations run in constant time. The only exception is the range method, which runs in constant time except for the first time it is
invoked on any sub-map returned by headMap, tailMap, or
subMap, when it runs in linear time.
This map is not thread-safe.
Map,
SortedMap,
HashMultiMap,
MultiMap,
Serialized Form| Constructor and Description |
|---|
TreeMultiMap()
Constructs this map using the natural ordering of the keys.
|
TreeMultiMap(Comparator<? super K> c)
Constructs this map where keys will be sorted according to the provided
comparator.
|
TreeMultiMap(Map<? extends K,? extends V> m)
Constructs this map using the natural ordering of the keys, adding all of
the provided mappings to this map.
|
| Modifier and Type | Method and Description |
|---|---|
Map<K,Set<V>> |
asMap()
Returns a
Map-based view of this MultiMap |
void |
clear()
Removes all of the mappings from this multi-map.
|
Comparator<? super K> |
comparator()
Returns the comparator used to order the keys in this map, or null if
this map uses the
natural ordering of its keys. |
boolean |
containsKey(Object key)
Returns
true if this multi-map contains a mapping for the
specified key. |
boolean |
containsMapping(Object key,
Object value)
Returns
true if this multi-map contains a mapping from specified
key to the specified value. |
boolean |
containsValue(Object value)
Returns
true if this multi-map contains from any key to the
specified value. |
Set<Map.Entry<K,V>> |
entrySet()
Returns a
Set view of all the key-value mappings contained in
this multi-map. |
K |
firstKey()
Returns a
Set view of the mappings contained in this map. |
Set<V> |
get(Object key)
Returns the set of values mapped to this key or
null of the key
is not mapped to any values |
SortedMultiMap<K,V> |
headMap(K toKey)
Returns a view of the portion of this map whose keys are less than
toKey. |
boolean |
isEmpty()
Returns
true if this multi-map contains no mappings. |
Set<K> |
keySet()
Returns a
Set view of the mappings contained in this multi-map. |
K |
lastKey()
Returns the last (highest) key currently in this map.
|
boolean |
put(K key,
V value)
Adds the specified value to the set of values associated with the
specified key in this map.
|
void |
putAll(Map<? extends K,? extends V> m)
Copies all of the mappings from the specified map to this mutli-map
|
void |
putAll(MultiMap<? extends K,? extends V> m)
Copies all of the mappings from the specified mulit-map to this mutli-map
|
boolean |
putMany(K key,
Collection<V> values)
Adds all of the specified values to the set of values associated with the
specified key in this map.
|
int |
range()
Returns the number of values maped to keys.
|
Set<V> |
remove(Object key)
Removes the mapping for a key from this multi-map if it is present,
returning any mapped values to that key
|
boolean |
remove(Object key,
Object value)
Removes the specified mapping for a key if it is present.
|
int |
size()
Returns the number of keys that are mapped to one or more values in this
multi-map.
|
SortedMultiMap<K,V> |
subMap(K fromKey,
K toKey)
Returns a view of the portion of this map whose keys range from
fromKey, inclusive, to toKey, exclusive. |
SortedMultiMap<K,V> |
tailMap(K fromKey)
Returns a view of the portion of this map whose keys are greater than or
equal to
fromKey. |
String |
toString()
Returns the string form of this multi-map
|
Collection<V> |
values()
Returns a
Collection view of the values contained in this map. |
Collection<Set<V>> |
valueSets()
Returns a
Collection view of the sets of values mapped to each
key in this map. |
public TreeMultiMap()
public TreeMultiMap(Comparator<? super K> c)
public void clear()
public Comparator<? super K> comparator()
natural ordering of its keys.comparator in interface SortedMultiMap<K,V>public boolean containsKey(Object key)
true if this multi-map contains a mapping for the
specified key.containsKey in interface MultiMap<K,V>public boolean containsMapping(Object key, Object value)
true if this multi-map contains a mapping from specified
key to the specified value.containsMapping in interface MultiMap<K,V>public boolean containsValue(Object value)
true if this multi-map contains from any key to the
specified value.containsValue in interface MultiMap<K,V>public Set<Map.Entry<K,V>> entrySet()
Set view of all the key-value mappings contained in
this multi-map.public K firstKey()
Set view of the mappings contained in this map.firstKey in interface SortedMultiMap<K,V>public Set<V> get(Object key)
null of the key
is not mapped to any valuespublic SortedMultiMap<K,V> headMap(K toKey)
toKey.headMap in interface SortedMultiMap<K,V>public boolean isEmpty()
true if this multi-map contains no mappings.public K lastKey()
lastKey in interface SortedMultiMap<K,V>public boolean put(K key, V value)
public void putAll(Map<? extends K,? extends V> m)
public void putAll(MultiMap<? extends K,? extends V> m)
public boolean putMany(K key, Collection<V> values)
putMany in interface MultiMap<K,V>key - key with which the specified value is to be associatedvalues - a collection of values to be associated with the specified
keytrue if at least one of the provided value was not
already in the set of value mapped to the specified keypublic int range()
MultiMap.size(). However, in the case where a key is mapped to more
than one value, this method will always return a value strictly
larger than size(). This method runs in constant time, except for the first
time called on a sub-map, when it runs in linear time to the number of
values.public Set<V> remove(Object key)
public boolean remove(Object key, Object value)
public int size()
size in interface MultiMap<K,V>MultiMap.range()public SortedMultiMap<K,V> subMap(K fromKey, K toKey)
fromKey, inclusive, to toKey, exclusive.subMap in interface SortedMultiMap<K,V>public SortedMultiMap<K,V> tailMap(K fromKey)
fromKey.tailMap in interface SortedMultiMap<K,V>public String toString()
public Collection<V> values()
Collection view of the values contained in this map. The collection and its Iterator are backed by the
map, so changes to the map are reflected in the collection, and
vice-versa.public Collection<Set<V>> valueSets()
Collection view of the sets of values mapped to each
key in this map.Copyright © 2012. All Rights Reserved.