|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectinf.util.IntKeyTreeMap<E>
E
- the element type for the values to which the keys are mappedpublic class IntKeyTreeMap<E>
This is a Red-Black-Tree-based implementation of a SortedMap
,
mapping int
s (keys) to Object
s (values). If
the key is given as an Integer
rather than an int
,
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 IntKeyEntry
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 int
s here which
are not Object
s. The use of int
s rather than
Integer
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 |
IntKeyTreeMap.IntKeyTreeEntry<E>
|
(package private) class |
IntKeyTreeMap.IntKeyTreeMapIterator<T>
|
Nested classes/interfaces inherited from interface inf.util.IntKeyMap |
---|
IntKeyMap.IntKeyEntry<E> |
Nested classes/interfaces inherited from interface java.util.Map |
---|
java.util.Map.Entry<K,V> |
Field Summary | |
---|---|
protected IntKeyTreeMap.IntKeyTreeEntry<E> |
root
the root of the tree |
Constructor Summary | |
---|---|
IntKeyTreeMap()
This constructor creates a new, empty IntKeyTreeMap. |
|
IntKeyTreeMap(java.util.Map<? extends java.lang.Integer,? extends E> map)
This constructor creates a new IntKeyTreeMap that contains all the associations from the given map. |
Method Summary | |
---|---|
void |
clear()
This function removes all mappings from this Map. |
IntKeyTreeMap<E> |
clone()
This function returns a shallow copy of this IntKeyTreeMap. |
java.util.Comparator<? super java.lang.Integer> |
comparator()
This function throws an UnsupportedOperationException. |
boolean |
containsKey(int key)
This function returns true if this IntKeyMap contains a mapping for the specified key, which is an int 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.Integer,E>> |
entrySet()
This function returns a Set of all the IntKeyEntrys contained in this Map. |
boolean |
equals(IntKeyMap<E> imap)
This function tests whether the this and the given IntKeyMap 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. |
int |
firstIntKey()
This function returns the first (lowest) key currently in this SortedMap. |
java.lang.Integer |
firstKey()
This function returns the first (lowest) key currently in this SortedMap. |
E |
get(int key)
This function returns the value to which this IntKeyMap maps the specified key, which is an int 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 IntKeyTreeMap. |
java.util.SortedMap<java.lang.Integer,E> |
headMap(java.lang.Integer toKey)
This function throws an UnsupportedOperationException. |
boolean |
isEmpty()
This function tests whether this Map contains no entries. |
java.util.Iterator<IntKeyTreeMap.IntKeyTreeEntry<E>> |
iterator()
This function returns an Iterator for the Entries in this Map. |
java.util.Set<java.lang.Integer> |
keySet()
This function returns a Set containing all the keys contained in this Map. |
int |
lastIntKey()
This function returns the last (highest) key currently in this SortedMap. |
java.lang.Integer |
lastKey()
This function returns the last (highest) key currently in this SortedMap. |
E |
put(int key,
E value)
This function associates the specified key, an int, with the specified value in this IntKeyMap. |
E |
put(java.lang.Integer key,
E value)
This function associates the specified key with the specified value in this Map. |
void |
putAll(java.util.Map<? extends java.lang.Integer,? extends E> map)
This function adds all the associations from the given Map to this Map. |
E |
remove(int key)
This function removes the mapping for this key, an int, from this IntKeyMap 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.Integer,E> |
subMap(java.lang.Integer fromKey,
java.lang.Integer toKey)
This function throws an UnsupportedOperationException. |
java.util.SortedMap<java.lang.Integer,E> |
tailMap(java.lang.Integer fromKey)
This function throws an UnsupportedOperationException. |
java.lang.String |
toString()
This function returns a printable representation of this IntKeyMap. |
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 IntKeyTreeMap.IntKeyTreeEntry<E> root
Constructor Detail |
---|
public IntKeyTreeMap()
This constructor creates a new, empty IntKeyTreeMap.
public IntKeyTreeMap(java.util.Map<? extends java.lang.Integer,? extends E> map)
This constructor creates a new IntKeyTreeMap that contains all the associations from the given map.
map
- the Map containing the associations to be added to this MapMethod Detail |
---|
public IntKeyTreeMap<E> clone()
This function returns a shallow copy of this IntKeyTreeMap. The values themselves are not cloned. Cloning an IntKeyTreeMap 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.Integer 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.Integer,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.Integer,? 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.Integer,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.Integer,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.Integer,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 Integer, 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.Integer,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.Integer,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
Integer, 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.Integer,E>
key
- key whose associated value is to be returned
public java.util.Set<java.lang.Integer> 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.Integer,E>
keySet
in interface java.util.SortedMap<java.lang.Integer,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.Integer,E>
values
in interface java.util.SortedMap<java.lang.Integer,E>
public java.util.Set<java.util.Map.Entry<java.lang.Integer,E>> entrySet()
This function returns a Set of all the IntKeyEntrys 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.Integer,E>
entrySet
in interface java.util.SortedMap<java.lang.Integer,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.Integer,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 Integer, 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.Integer,E>
key
- the key to the mapping that is to be removed
public E put(int key, E value)
This function associates the specified key, an int, with the specified value in this IntKeyMap. If the IntKeyMap 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 IntKeyMap 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 IntKeyMap.
put
in interface IntKeyMap<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(int key)
This function returns true if this IntKeyMap contains a mapping for the specified key, which is an int here. The time complexity of this function is guaranteed to be in O(log(n)), where n is the size of this IntKeyMap.
containsKey
in interface IntKeyMap<E>
key
- the key whose presence in this IntKeyMap is to be tested
public E get(int key)
This function returns the value to which this IntKeyMap maps the
specified key, which is an int here. It returns null if the IntKeyMap
contains no mapping for this key. A return value of null does not
necessarily indicate that the IntKeyMap contains no mapping for
the key; it's also possible that the IntKeyMap explicitly maps the key to
null. The containsKey(int)
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 IntKeyMap.
get
in interface IntKeyMap<E>
key
- key whose associated value is to be returned
public E remove(int key)
This function removes the mapping for this key, an int, from this IntKeyMap if it was present. The value the key was mapped to is returned. A null return may indicate that the IntKeyMap 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 IntKeyMap.
remove
in interface IntKeyMap<E>
key
- the key to the mapping that is to be removed
public java.lang.Integer 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.Integer,E>
java.util.NoSuchElementException
- if this SortedMap is emptypublic java.lang.Integer 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.Integer,E>
java.util.NoSuchElementException
- if the SortedMap is emptypublic java.util.Comparator<? super java.lang.Integer> comparator() throws java.lang.UnsupportedOperationException
This function throws an UnsupportedOperationException. Since keys are ints (Integers) 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.Integer,E>
java.lang.UnsupportedOperationException
- alwayspublic java.util.SortedMap<java.lang.Integer,E> headMap(java.lang.Integer toKey) throws java.lang.UnsupportedOperationException
This function throws an UnsupportedOperationException.
headMap
in interface java.util.SortedMap<java.lang.Integer,E>
toKey
- highest key in the requested sub-Map
java.lang.UnsupportedOperationException
- alwayspublic java.util.SortedMap<java.lang.Integer,E> tailMap(java.lang.Integer fromKey) throws java.lang.UnsupportedOperationException
This function throws an UnsupportedOperationException.
tailMap
in interface java.util.SortedMap<java.lang.Integer,E>
fromKey
- lowest key in the requested sub-Map
java.lang.UnsupportedOperationException
- alwayspublic java.util.SortedMap<java.lang.Integer,E> subMap(java.lang.Integer fromKey, java.lang.Integer toKey) throws java.lang.UnsupportedOperationException
This function throws an UnsupportedOperationException.
subMap
in interface java.util.SortedMap<java.lang.Integer,E>
fromKey
- lowest key in the requested sub-MaptoKey
- highest key in the requested sub-Map
java.lang.UnsupportedOperationException
- alwayspublic java.util.Iterator<IntKeyTreeMap.IntKeyTreeEntry<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<IntKeyTreeMap.IntKeyTreeEntry<E>>
Iterator
of the IntKeyEntrys in this
Mappublic int firstIntKey() 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 int lastIntKey() 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 IntKeyMap the local equality test is used, otherwise entries are compared one by one.
equals
in interface java.util.Map<java.lang.Integer,E>
equals
in class java.lang.Object
o
- the Object this IntKeyTreeMap is compared to
public boolean equals(IntKeyMap<E> imap)
This function tests whether the this and the given IntKeyMap do map the same keys to equal values.
imap
- the IntKeyMap this is compared to
public int hashCode()
This function computes a hash value for this IntKeyTreeMap. 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.Integer,E>
hashCode
in class java.lang.Object
public java.lang.String toString()
This function returns a printable representation of this IntKeyMap. 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 |