edu.emory.mathcs.backport.java.util
Class TreeMap

java.lang.Object
  extended byjava.util.AbstractMap
      extended byedu.emory.mathcs.backport.java.util.AbstractMap
          extended byedu.emory.mathcs.backport.java.util.TreeMap
All Implemented Interfaces:
java.util.Map, NavigableMap, java.io.Serializable, java.util.SortedMap

public class TreeMap
extends AbstractMap
implements NavigableMap, java.io.Serializable

Sorted map implementation based on a red-black tree and implementing all the methods from the NavigableMap interface.

Author:
Dawid Kurzyniec
See Also:
Serialized Form

Nested Class Summary
static class TreeMap.Entry
           
 
Nested classes inherited from class edu.emory.mathcs.backport.java.util.AbstractMap
AbstractMap.SimpleEntry, AbstractMap.SimpleImmutableEntry
 
Constructor Summary
TreeMap()
           
TreeMap(java.util.Comparator comparator)
           
TreeMap(java.util.Map map)
           
TreeMap(java.util.SortedMap map)
           
 
Method Summary
 java.util.Map.Entry ceilingEntry(java.lang.Object key)
          Returns a key-value mapping associated with the least key greater than or equal to the given key, or null if there is no such key.
 java.lang.Object ceilingKey(java.lang.Object key)
          Returns the least key greater than or equal to the given key, or null if there is no such key.
 void clear()
           
 java.lang.Object clone()
           
 java.util.Comparator comparator()
           
 boolean containsKey(java.lang.Object key)
           
 boolean containsValue(java.lang.Object value)
           
 java.util.Set descendingEntrySet()
          Returns a Set view of the mappings contained in this map.
 java.util.Set descendingKeySet()
          Returns a Set view of the keys contained in this map.
 java.util.Set entrySet()
           
 java.util.Map.Entry firstEntry()
          Returns a key-value mapping associated with the least key in this map, or null if the map is empty.
 java.lang.Object firstKey()
           
 java.util.Map.Entry floorEntry(java.lang.Object key)
          Returns a key-value mapping associated with the greatest key less than or equal to the given key, or null if there is no such key.
 java.lang.Object floorKey(java.lang.Object key)
          Returns the greatest key less than or equal to the given key, or null if there is no such key.
 java.lang.Object get(java.lang.Object key)
          
 java.util.SortedMap headMap(java.lang.Object toKey)
           
 java.util.Map.Entry higherEntry(java.lang.Object key)
          Returns a key-value mapping associated with the least key strictly greater than the given key, or null if there is no such key.
 java.lang.Object higherKey(java.lang.Object key)
          Returns the least key strictly greater than the given key, or null if there is no such key.
 boolean isEmpty()
           
 java.util.Map.Entry lastEntry()
          Returns a key-value mapping associated with the greatest key in this map, or null if the map is empty.
 java.lang.Object lastKey()
           
 java.util.Map.Entry lowerEntry(java.lang.Object key)
          Returns a key-value mapping associated with the greatest key strictly less than the given key, or null if there is no such key.
 java.lang.Object lowerKey(java.lang.Object key)
          Returns the greatest key strictly less than the given key, or null if there is no such key.
 NavigableMap navigableHeadMap(java.lang.Object toKey)
          Returns a view of the portion of this map whose keys are strictly less than toKey.
 NavigableMap navigableSubMap(java.lang.Object fromKey, java.lang.Object toKey)
          Returns a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive.
 NavigableMap navigableTailMap(java.lang.Object fromKey)
          Returns a view of the portion of this map whose keys are greater than or equal to fromKey.
 java.util.Map.Entry pollFirstEntry()
          Removes and returns a key-value mapping associated with the least key in this map, or null if the map is empty.
 java.util.Map.Entry pollLastEntry()
          Removes and returns a key-value mapping associated with the greatest key in this map, or null if the map is empty.
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
           
 void putAll(java.util.Map map)
           
 java.lang.Object remove(java.lang.Object key)
           
 int size()
           
 java.util.SortedMap subMap(java.lang.Object fromKey, java.lang.Object toKey)
           
 java.util.SortedMap tailMap(java.lang.Object fromKey)
           
 
Methods inherited from class edu.emory.mathcs.backport.java.util.AbstractMap
keySet
 
Methods inherited from class java.util.AbstractMap
equals, hashCode, toString, values
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode, keySet, values
 

Constructor Detail

TreeMap

public TreeMap()

TreeMap

public TreeMap(java.util.Comparator comparator)

TreeMap

public TreeMap(java.util.SortedMap map)

TreeMap

public TreeMap(java.util.Map map)
Method Detail

size

public int size()
Specified by:
size in interface java.util.Map

clear

public void clear()
Specified by:
clear in interface java.util.Map

clone

public java.lang.Object clone()

put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
Specified by:
put in interface java.util.Map

get

public java.lang.Object get(java.lang.Object key)

Specified by:
get in interface java.util.Map

containsKey

public boolean containsKey(java.lang.Object key)
Specified by:
containsKey in interface java.util.Map

entrySet

public java.util.Set entrySet()
Specified by:
entrySet in interface java.util.Map

lowerEntry

public java.util.Map.Entry lowerEntry(java.lang.Object key)
Description copied from interface: NavigableMap
Returns a key-value mapping associated with the greatest key strictly less than the given key, or null if there is no such key.

Specified by:
lowerEntry in interface NavigableMap
Parameters:
key - the key
Returns:
an entry with the greatest key less than key, or null if there is no such key
Since:
1.6

lowerKey

public java.lang.Object lowerKey(java.lang.Object key)
Description copied from interface: NavigableMap
Returns the greatest key strictly less than the given key, or null if there is no such key.

Specified by:
lowerKey in interface NavigableMap
Parameters:
key - the key
Returns:
the greatest key less than key, or null if there is no such key
Since:
1.6

floorEntry

public java.util.Map.Entry floorEntry(java.lang.Object key)
Description copied from interface: NavigableMap
Returns a key-value mapping associated with the greatest key less than or equal to the given key, or null if there is no such key.

Specified by:
floorEntry in interface NavigableMap
Parameters:
key - the key
Returns:
an entry with the greatest key less than or equal to key, or null if there is no such key
Since:
1.6

floorKey

public java.lang.Object floorKey(java.lang.Object key)
Description copied from interface: NavigableMap
Returns the greatest key less than or equal to the given key, or null if there is no such key.

Specified by:
floorKey in interface NavigableMap
Parameters:
key - the key
Returns:
the greatest key less than or equal to key, or null if there is no such key
Since:
1.6

ceilingEntry

public java.util.Map.Entry ceilingEntry(java.lang.Object key)
Description copied from interface: NavigableMap
Returns a key-value mapping associated with the least key greater than or equal to the given key, or null if there is no such key.

Specified by:
ceilingEntry in interface NavigableMap
Parameters:
key - the key
Returns:
an entry with the least key greater than or equal to key, or null if there is no such key
Since:
1.6

ceilingKey

public java.lang.Object ceilingKey(java.lang.Object key)
Description copied from interface: NavigableMap
Returns the least key greater than or equal to the given key, or null if there is no such key.

Specified by:
ceilingKey in interface NavigableMap
Parameters:
key - the key
Returns:
the least key greater than or equal to key, or null if there is no such key
Since:
1.6

higherEntry

public java.util.Map.Entry higherEntry(java.lang.Object key)
Description copied from interface: NavigableMap
Returns a key-value mapping associated with the least key strictly greater than the given key, or null if there is no such key.

Specified by:
higherEntry in interface NavigableMap
Parameters:
key - the key
Returns:
an entry with the least key greater than key, or null if there is no such key
Since:
1.6

higherKey

public java.lang.Object higherKey(java.lang.Object key)
Description copied from interface: NavigableMap
Returns the least key strictly greater than the given key, or null if there is no such key.

Specified by:
higherKey in interface NavigableMap
Parameters:
key - the key
Returns:
the least key greater than key, or null if there is no such key
Since:
1.6

firstEntry

public java.util.Map.Entry firstEntry()
Description copied from interface: NavigableMap
Returns a key-value mapping associated with the least key in this map, or null if the map is empty.

Specified by:
firstEntry in interface NavigableMap
Returns:
an entry with the least key, or null if this map is empty
Since:
1.6

lastEntry

public java.util.Map.Entry lastEntry()
Description copied from interface: NavigableMap
Returns a key-value mapping associated with the greatest key in this map, or null if the map is empty.

Specified by:
lastEntry in interface NavigableMap
Returns:
an entry with the greatest key, or null if this map is empty
Since:
1.6

pollFirstEntry

public java.util.Map.Entry pollFirstEntry()
Description copied from interface: NavigableMap
Removes and returns a key-value mapping associated with the least key in this map, or null if the map is empty.

Specified by:
pollFirstEntry in interface NavigableMap
Returns:
the removed first entry of this map, or null if this map is empty
Since:
1.6

pollLastEntry

public java.util.Map.Entry pollLastEntry()
Description copied from interface: NavigableMap
Removes and returns a key-value mapping associated with the greatest key in this map, or null if the map is empty.

Specified by:
pollLastEntry in interface NavigableMap
Returns:
the removed last entry of this map, or null if this map is empty
Since:
1.6

descendingKeySet

public java.util.Set descendingKeySet()
Description copied from interface: NavigableMap
Returns a Set view of the keys contained in this map. The set's iterator returns the keys in descending order. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation), the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.

Specified by:
descendingKeySet in interface NavigableMap
Returns:
a set view of the keys contained in this map, sorted in descending order

descendingEntrySet

public java.util.Set descendingEntrySet()
Description copied from interface: NavigableMap
Returns a Set view of the mappings contained in this map. The set's iterator returns the entries in descending key order. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation, or through the setValue operation on a map entry returned by the iterator) the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.

Specified by:
descendingEntrySet in interface NavigableMap
Returns:
a set view of the mappings contained in this map, sorted in descending key order

navigableSubMap

public NavigableMap navigableSubMap(java.lang.Object fromKey,
                                    java.lang.Object toKey)
Description copied from interface: NavigableMap
Returns a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive. (If fromKey and toKey are equal, the returned map is empty.) The returned map is backed by this map, so changes in the returned map are reflected in this map, and vice-versa. The returned map supports all optional map operations that this map supports.

The returned map will throw an IllegalArgumentException on an attempt to insert a key outside its range.

Specified by:
navigableSubMap in interface NavigableMap
Parameters:
fromKey - low endpoint (inclusive) of the keys in the returned map
toKey - high endpoint (exclusive) of the keys in the returned map
Returns:
a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive

navigableHeadMap

public NavigableMap navigableHeadMap(java.lang.Object toKey)
Description copied from interface: NavigableMap
Returns a view of the portion of this map whose keys are strictly less than toKey. The returned map is backed by this map, so changes in the returned map are reflected in this map, and vice-versa. The returned map supports all optional map operations that this map supports.

The returned map will throw an IllegalArgumentException on an attempt to insert a key outside its range.

Specified by:
navigableHeadMap in interface NavigableMap
Parameters:
toKey - high endpoint (exclusive) of the keys in the returned map
Returns:
a view of the portion of this map whose keys are strictly less than toKey

navigableTailMap

public NavigableMap navigableTailMap(java.lang.Object fromKey)
Description copied from interface: NavigableMap
Returns a view of the portion of this map whose keys are greater than or equal to fromKey. The returned map is backed by this map, so changes in the returned map are reflected in this map, and vice-versa. The returned map supports all optional map operations that this map supports.

The returned map will throw an IllegalArgumentException on an attempt to insert a key outside its range.

Specified by:
navigableTailMap in interface NavigableMap
Parameters:
fromKey - low endpoint (inclusive) of the keys in the returned map
Returns:
a view of the portion of this map whose keys are greater than or equal to fromKey

comparator

public java.util.Comparator comparator()
Specified by:
comparator in interface java.util.SortedMap

subMap

public java.util.SortedMap subMap(java.lang.Object fromKey,
                                  java.lang.Object toKey)
Specified by:
subMap in interface java.util.SortedMap

headMap

public java.util.SortedMap headMap(java.lang.Object toKey)
Specified by:
headMap in interface java.util.SortedMap

tailMap

public java.util.SortedMap tailMap(java.lang.Object fromKey)
Specified by:
tailMap in interface java.util.SortedMap

firstKey

public java.lang.Object firstKey()
Specified by:
firstKey in interface java.util.SortedMap

lastKey

public java.lang.Object lastKey()
Specified by:
lastKey in interface java.util.SortedMap

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface java.util.Map

containsValue

public boolean containsValue(java.lang.Object value)
Specified by:
containsValue in interface java.util.Map

remove

public java.lang.Object remove(java.lang.Object key)
Specified by:
remove in interface java.util.Map

putAll

public void putAll(java.util.Map map)
Specified by:
putAll in interface java.util.Map