|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectinf.util.DoubleKeyTreeMap<E>
E
- the element type for the values to which the keys are mappedpublic class DoubleKeyTreeMap<E>
This is a Red-Black-Tree-based implementation of a SortedMap
,
mapping double
s (keys) to Object
s (values).
If the key is given as a Double
rather than a
double
, it must not be null
. Values are
allowed to be null
. This class guarantees that the
Map
will be in ascending key order and the
Iterator
returned by iterator()
will list the
DoubleKeyEntry
s contained in this Map
in
ascending key order.
This implementation provides guaranteed log(n) time cost for the
containsKey(...)
, get(...)
,
put(...)
and remove(...)
operations. Algorithms
are adaptations of those in Cormen, Leiserson, and Rivest's Introduction
to Algorithms.
This implementation should essentially behave like the one in the java.util
package. The main difference is that keys are double
s here
which are not Object
s. The use of double
s
rather than Double
s improves the efficiency of the
implementation (very roughly by a factor of 2).
Important: The functions that should give a collection view of this Map as described in the documentation for Map do not do so in this implementation. Modifications to the returned Sets or Collection only affect the Map in a limited way, and vice versa.
TreeMap
implementation
by Josh Bloch and Doug LeaNested Class Summary | |
---|---|
(package private) static class |
DoubleKeyTreeMap.DoubleKeyTreeEntry<E>
|
(package private) class |
DoubleKeyTreeMap.DoubleKeyTreeMapIterator<T>
|
Nested classes/interfaces inherited from interface inf.util.DoubleKeyMap |
---|
DoubleKeyMap.DoubleKeyEntry<E> |
Nested classes/interfaces inherited from interface java.util.Map |
---|
java.util.Map.Entry<K,V> |
Field Summary | |
---|---|
protected DoubleKeyTreeMap.DoubleKeyTreeEntry<E> |
root
the root of the tree |
Constructor Summary | |
---|---|
DoubleKeyTreeMap()
This constructor creates a new, empty DoubleKeyTreeMap. |
|
DoubleKeyTreeMap(java.util.Map<? extends java.lang.Double,? extends E> map)
This constructor creates a new DoubleKeyTreeMap that contains all the associations from the given map. |
Method Summary | |
---|---|
void |
clear()
This function removes all mappings from this Map. |
DoubleKeyTreeMap<E> |
clone()
This function returns a shallow copy of this DoubleKeyTreeMap. |
java.util.Comparator<? super java.lang.Double> |
comparator()
This function throws an UnsupportedOperationException. |
boolean |
containsKey(double key)
This function returns true if this DoubleKeyMap contains a mapping for the specified key, which is a double here. |
boolean |
containsKey(java.lang.Object key)
This function returns true if this Map contains a mapping for the specified key. |
boolean |
containsValue(java.lang.Object value)
This function returns true if this Map contains an Entry containing the specified value. |
java.util.Set<java.util.Map.Entry<java.lang.Double,E>> |
entrySet()
This function returns a set of all the DoubleKeyEntrys contained in this Map. |
boolean |
equals(DoubleKeyMap<E> imap)
This function tests whether the this and the given DoubleKeyMap do map the same keys to equal values. |
boolean |
equals(java.lang.Object o)
This function tests whether this and the given Object are equal. |
double |
firstDoubleKey()
This function returns the first (lowest) key currently in this SortedMap. |
java.lang.Double |
firstKey()
This function returns the first (lowest) key currently in this SortedMap. |
E |
get(double key)
This function returns the value to which this DoubleKeyMap maps the specified key, which is a double here. |
E |
get(java.lang.Object key)
This function returns the value to which this Map maps the specified key. |
int |
hashCode()
This function computes a hash value for this DoubleKeyTreeMap. |
java.util.SortedMap<java.lang.Double,E> |
headMap(java.lang.Double toKey)
This function throws an UnsupportedOperationException. |
boolean |
isEmpty()
This function tests whether this Map contains no entries. |
java.util.Iterator<DoubleKeyTreeMap.DoubleKeyTreeEntry<E>> |
iterator()
This function returns an Iterator for the Entries in this Map. |
java.util.Set<java.lang.Double> |
keySet()
This function returns a Set containing all the keys contained in this Map. |
double |
lastDoubleKey()
This function returns the last (highest) key currently in this SortedMap. |
java.lang.Double |
lastKey()
This function returns the last (highest) key currently in this SortedMap. |
E |
put(double key,
E value)
This function associates the specified key, a double, with the specified value in this DoubleKeyMap. |
E |
put(java.lang.Double key,
E value)
This function associates the specified key with the specified value in this Map. |
void |
putAll(java.util.Map<? extends java.lang.Double,? extends E> map)
This function adds all the associations from the given Map to this Map. |
E |
remove(double key)
This function removes the mapping for this key, a double, from this DoubleKeyMap if it was present. |
E |
remove(java.lang.Object key)
This function removes the mapping for this key from this Map if it was present. |
int |
size()
This function returns the number of key-value mappings in this Map. |
java.util.SortedMap<java.lang.Double,E> |
subMap(java.lang.Double fromKey,
java.lang.Double toKey)
This function throws an UnsupportedOperationException. |
java.util.SortedMap<java.lang.Double,E> |
tailMap(java.lang.Double fromKey)
This function throws an UnsupportedOperationException. |
java.lang.String |
toString()
This function returns a printable representation of this DoubleKeyMap. |
java.util.Collection<E> |
values()
This function returns a Collection containing all the values contained in this Map. |
Methods inherited from class java.lang.Object |
---|
finalize, getClass, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
protected DoubleKeyTreeMap.DoubleKeyTreeEntry<E> root
Constructor Detail |
---|
public DoubleKeyTreeMap()
This constructor creates a new, empty DoubleKeyTreeMap.
public DoubleKeyTreeMap(java.util.Map<? extends java.lang.Double,? extends E> map)
This constructor creates a new DoubleKeyTreeMap that contains all the associations from the given map.
map
- the Map containing the associations to be added to this MapMethod Detail |
---|
public DoubleKeyTreeMap<E> clone()
This function returns a shallow copy of this DoubleKeyTreeMap. The values themselves are not cloned. Cloning a DoubleKeyTreeMap is rather inefficient (time complexity in O(n), where n is the size of this Map) and is best avoided whenever possible.
clone
in class java.lang.Object
public E put(java.lang.Double key, E value)
This function associates the specified key with the specified value in this Map. If the Map previously contained a mapping for this key, the old value is replaced. The old value is also used as the return value of this function. A null return may indicate that no previous association existed or that the Map previously associated null with the specified key. Note that the given key Object must not be null. The time complexity of this function is guaranteed to be in O(log(n)), where n is the size of this Map.
put
in interface java.util.Map<java.lang.Double,E>
key
- the key with which the specified value is to be associatedvalue
- the value to be associated with the specified key
public void putAll(java.util.Map<? extends java.lang.Double,? extends E> map)
This function adds all the associations from the given Map to this Map. Any values for existing keys in this Map will be replaced. The time complexity of this function is in O(m log(n+m)), where m is the size of the given Map and n is the size of this Map.
putAll
in interface java.util.Map<java.lang.Double,E>
map
- the Map containing the associations to be added to this Mappublic boolean isEmpty()
This function tests whether this Map contains no entries. If this is true, the size of the Map is 0.
isEmpty
in interface java.util.Map<java.lang.Double,E>
public int size()
This function returns the number of key-value mappings in this Map. The result is a positive integer.
size
in interface java.util.Map<java.lang.Double,E>
public boolean containsKey(java.lang.Object key)
This function returns true if this Map contains a mapping for the specified key. Note that the given key Object must be an Double, which must not be null. The time complexity of this function is guaranteed to be in O(log(n)), where n is the size of this Map.
containsKey
in interface java.util.Map<java.lang.Double,E>
key
- the key whose presence in this Map is to be tested
public boolean containsValue(java.lang.Object value)
This function returns true if this Map contains an Entry containing the specified value. The given value may be null. Note that searching for values is inefficient: the time complexity of this function is in O(n), where n is the size of this Map, i.e. it is linear in the size of the Map.
containsValue
in interface java.util.Map<java.lang.Double,E>
value
- the value whose presence in this Map is to be tested
public E get(java.lang.Object key)
This function returns the value to which this Map maps the specified key.
It returns null if the Map contains no mapping for this key. A return
value of null does not necessarily indicate that the Map contains
no mapping for the key; it's also possible that the Map explicitly maps
the key to null. The containsKey(Object)
operation may be used
to distinguish these two cases. Note that the given key Object must be an
Double, but must not be null. The time complexity of this function is
guaranteed to be in O(log(n)), where n is the size of
this Map.
get
in interface java.util.Map<java.lang.Double,E>
key
- key whose associated value is to be returned
public java.util.Set<java.lang.Double> keySet()
This function returns a Set containing all the keys contained in this Map. The time and space complexity of this function is in O(n), where n is the size of this Map. In this implementation the returned Set is actually a TreeSet and the keys in it are in the same order as in this SortedMap.
Important: This function does not return a collection view of this Map as described in the documentation of the Map interface, and changes to the returned Set will not affect this Map, and vice versa. In other words, this Map and the returned Set are independent Objects.
keySet
in interface java.util.Map<java.lang.Double,E>
keySet
in interface java.util.SortedMap<java.lang.Double,E>
public java.util.Collection<E> values()
This function returns a Collection containing all the values contained in this Map. Note that the Collection may contain duplicates. The time and space complexity of this function is in O(n), where n is the size of this Map. In this implementation the returned Collection is actually a LinkedList and the values in it are in the same order as in this SortedMap.
Important: This function does not return a collection view of this Map as described in the documentation of the Map interface, and changes to the returned Collection will not affect this Map, and vice versa. In other words, this Map and the returned Collection are independent Objects. However the contained value Objects are shared.
values
in interface java.util.Map<java.lang.Double,E>
values
in interface java.util.SortedMap<java.lang.Double,E>
public java.util.Set<java.util.Map.Entry<java.lang.Double,E>> entrySet()
This function returns a set of all the DoubleKeyEntrys contained in this Map. The time and space complexity of this function is in O(n), where n is the size of this Map. In this implementation the returned Set is actually a HashSet.
Important: This function does not quite return a collection view of this Map as described in the documentation of the Map interface, and changes to the returned Collection may not affect this Map, and vice versa. Essentially, the returned Entries are those contained in the Map, so changes to the Entries will be reflected in the Map. However, new Entries that are added to the Set, or Entries that are removed from it will not affect the Map.
entrySet
in interface java.util.Map<java.lang.Double,E>
entrySet
in interface java.util.SortedMap<java.lang.Double,E>
public void clear()
This function removes all mappings from this Map. The resulting Map behaves exactly as if it had just been newly created.
clear
in interface java.util.Map<java.lang.Double,E>
public E remove(java.lang.Object key)
This function removes the mapping for this key from this Map if it was present. The value the key was mapped to is returned. A null return may indicate that the Map previously associated null with the specified key. Note that the given key Object must be an Double, but must not be null. The time complexity of this function is guaranteed to be in O(log(n)), where n is the size of this Map.
remove
in interface java.util.Map<java.lang.Double,E>
key
- the key to the mapping that is to be removed
public E put(double key, E value)
This function associates the specified key, a double, with the specified value in this DoubleKeyMap. If the DoubleKeyMap previously contained a mapping for this key, the old value is replaced. The old value is also used as the return value of this function. A null return may indicate that no previous association existed or that the DoubleKeyMap previously associated null with the specified key. The time complexity of this function is guaranteed to be in O(log(n)), where n is the size of this DoubleKeyMap.
put
in interface DoubleKeyMap<E>
key
- the key with which the specified value is to be associatedvalue
- the value to be associated with the specified key
public boolean containsKey(double key)
This function returns true if this DoubleKeyMap contains a mapping for the specified key, which is a double here. The time complexity of this function is guaranteed to be in O(log(n)), where n is the size of this DoubleKeyMap.
containsKey
in interface DoubleKeyMap<E>
key
- the key whose presence in this DoubleKeyMap is to be tested
public E get(double key)
This function returns the value to which this DoubleKeyMap maps the
specified key, which is a double here. It returns null if the
DoubleKeyMap contains no mapping for this key. A return value of null
does not necessarily indicate that the DoubleKeyMap contains no
mapping for the key; it's also possible that the DoubleKeyMap explicitly
maps the key to null. The containsKey(double)
operation may be
used to distinguish these two cases. The time complexity of this function
is guaranteed to be in O(log(n)), where n is the size of
this DoubleKeyMap.
get
in interface DoubleKeyMap<E>
key
- key whose associated value is to be returned
public E remove(double key)
This function removes the mapping for this key, a double, from this DoubleKeyMap if it was present. The value the key was mapped to is returned. A null return may indicate that the DoubleKeyMap previously associated null with the specified key. The time complexity of this function is guaranteed to be in O(log(n)), where n is the size of this DoubleKeyMap.
remove
in interface DoubleKeyMap<E>
key
- the key to the mapping that is to be removed
public java.lang.Double firstKey() throws java.util.NoSuchElementException
This function returns the first (lowest) key currently in this SortedMap. The time complexity of this function is guaranteed to be in O(log(n)), where n is the size of this SortedMap.
firstKey
in interface java.util.SortedMap<java.lang.Double,E>
java.util.NoSuchElementException
- if this SortedMap is emptypublic java.lang.Double lastKey() throws java.util.NoSuchElementException
This function returns the last (highest) key currently in this SortedMap. The time complexity of this function is guaranteed to be in O(log(n)), where n is the size of this SortedMap.
lastKey
in interface java.util.SortedMap<java.lang.Double,E>
java.util.NoSuchElementException
- if the SortedMap is emptypublic java.util.Comparator<? super java.lang.Double> comparator() throws java.lang.UnsupportedOperationException
This function throws an UnsupportedOperationException. Since keys are doubles (Doubles) here the order is defined by their values and no Comparator is needed to determine their order.
comparator
in interface java.util.SortedMap<java.lang.Double,E>
java.lang.UnsupportedOperationException
- alwayspublic java.util.SortedMap<java.lang.Double,E> headMap(java.lang.Double toKey) throws java.lang.UnsupportedOperationException
This function throws an UnsupportedOperationException.
headMap
in interface java.util.SortedMap<java.lang.Double,E>
toKey
- highest key in the requested sub-Map
java.lang.UnsupportedOperationException
- alwayspublic java.util.SortedMap<java.lang.Double,E> tailMap(java.lang.Double fromKey) throws java.lang.UnsupportedOperationException
This function throws an UnsupportedOperationException.
tailMap
in interface java.util.SortedMap<java.lang.Double,E>
fromKey
- lowest key in the requested sub-Map
java.lang.UnsupportedOperationException
- alwayspublic java.util.SortedMap<java.lang.Double,E> subMap(java.lang.Double fromKey, java.lang.Double toKey) throws java.lang.UnsupportedOperationException
This function throws an UnsupportedOperationException.
subMap
in interface java.util.SortedMap<java.lang.Double,E>
fromKey
- lowest key in the requested sub-MaptoKey
- highest key in the requested sub-Map
java.lang.UnsupportedOperationException
- alwayspublic java.util.Iterator<DoubleKeyTreeMap.DoubleKeyTreeEntry<E>> iterator()
This function returns an Iterator for the Entries in this Map. They will be returned in the (ascending) order of their keys.
iterator
in interface java.lang.Iterable<DoubleKeyTreeMap.DoubleKeyTreeEntry<E>>
Iterator
of the DoubleKeyEntrys in this
Mappublic double firstDoubleKey() throws java.util.NoSuchElementException
This function returns the first (lowest) key currently in this SortedMap. The time complexity of this function is guaranteed to be in O(log(n)), where n is the size of this SortedMap.
java.util.NoSuchElementException
- if this SortedMap is emptypublic double lastDoubleKey() throws java.util.NoSuchElementException
This function returns the last (highest) key currently in this SortedMap. The time complexity of this function is guaranteed to be in O(log(n)), where n is the size of this SortedMap.
java.util.NoSuchElementException
- if the SortedMap is emptypublic boolean equals(java.lang.Object o)
This function tests whether this and the given Object are equal. If the given Object is an DoubleKeyMap the local equality test is used, otherwise entries are compared one by one.
equals
in interface java.util.Map<java.lang.Double,E>
equals
in class java.lang.Object
o
- the Object this DoubleKeyTreeMap is compared to
public boolean equals(DoubleKeyMap<E> imap)
This function tests whether the this and the given DoubleKeyMap do map the same keys to equal values.
imap
- the DoubleKeyMap this is compared to
public int hashCode()
This function computes a hash value for this DoubleKeyTreeMap. Note that this function is relatively expensive to compute (linear in the size of the Map) and thus, hashing may not be very efficient.
hashCode
in interface java.util.Map<java.lang.Double,E>
hashCode
in class java.lang.Object
public java.lang.String toString()
This function returns a printable representation of this DoubleKeyMap. It is printed as a comma-separated set of "key->value" pairs, e.g.:
{1->One, 2->Two}
toString
in class java.lang.Object
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |