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

java.lang.Object
  extended by java.util.AbstractMap
      extended by edu.emory.mathcs.backport.java.util.AbstractMap
          extended by edu.emory.mathcs.backport.java.util.TreeMap
All Implemented Interfaces:
NavigableMap, java.io.Serializable, java.util.Map, 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/interfaces 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)
           
 NavigableSet descendingKeySet()
          Returns a reverse order NavigableSet view of the keys contained in this map.
 NavigableMap descendingMap()
          Returns a reverse order view of the mappings 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)
          
 NavigableMap headMap(java.lang.Object toKey, boolean toInclusive)
          Returns a view of the portion of this map whose keys are less than (or equal to, if inclusive is true) 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.Set keySet()
          
 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.
 NavigableSet navigableKeySet()
          Returns a NavigableSet view of the keys contained in this map.
 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()
           
 NavigableMap subMap(java.lang.Object fromKey, boolean fromInclusive, java.lang.Object toKey, boolean toInclusive)
          Returns a view of the portion of this map whose keys range from fromKey to toKey.
 java.util.SortedMap subMap(java.lang.Object fromKey, java.lang.Object toKey)
          
 java.util.SortedMap tailMap(java.lang.Object fromKey)
          
 NavigableMap tailMap(java.lang.Object fromKey, boolean fromInclusive)
          Returns a view of the portion of this map whose keys are greater than (or equal to, if inclusive is true) fromKey.
 
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.SortedMap
values
 
Methods inherited from interface java.util.Map
equals, hashCode
 

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
Overrides:
size in class java.util.AbstractMap

clear

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

clone

public java.lang.Object clone()
Overrides:
clone in class java.util.AbstractMap

put

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

get

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

Specified by:
get in interface java.util.Map
Overrides:
get in class java.util.AbstractMap

containsKey

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

entrySet

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

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

descendingMap

public NavigableMap descendingMap()
Description copied from interface: NavigableMap
Returns a reverse order view of the mappings contained in this map. The descending map is backed by this map, so changes to the map are reflected in the descending map, and vice-versa. If either map is modified while an iteration over a collection view of either map is in progress (except through the iterator's own remove operation), the results of the iteration are undefined.

The returned map has an ordering equivalent to Collections.reverseOrder(comparator()). The expression m.descendingMap().descendingMap() returns a view of m essentially equivalent to m.

Specified by:
descendingMap in interface NavigableMap
Returns:
a reverse order view of this map
Since:
1.6

descendingKeySet

public NavigableSet descendingKeySet()
Description copied from interface: NavigableMap
Returns a reverse order NavigableSet 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 reverse order navigable set view of the keys in this map

subMap

public java.util.SortedMap subMap(java.lang.Object fromKey,
                                  java.lang.Object toKey)
Description copied from interface: NavigableMap

Equivalent to subMap(fromKey, true, toKey, false).

Specified by:
subMap in interface NavigableMap
Specified by:
subMap in interface java.util.SortedMap

headMap

public java.util.SortedMap headMap(java.lang.Object toKey)
Description copied from interface: NavigableMap

Equivalent to headMap(toKey, false).

Specified by:
headMap in interface NavigableMap
Specified by:
headMap in interface java.util.SortedMap

tailMap

public java.util.SortedMap tailMap(java.lang.Object fromKey)
Description copied from interface: NavigableMap

Equivalent to tailMap(fromKey, true).

Specified by:
tailMap in interface NavigableMap
Specified by:
tailMap in interface java.util.SortedMap

subMap

public NavigableMap subMap(java.lang.Object fromKey,
                           boolean fromInclusive,
                           java.lang.Object toKey,
                           boolean toInclusive)
Description copied from interface: NavigableMap
Returns a view of the portion of this map whose keys range from fromKey to toKey. If fromKey and toKey are equal, the returned map is empty unless fromExclusive and toExclusive are both true. 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 of its range, or to construct a submap either of whose endpoints lie outside its range.

Specified by:
subMap in interface NavigableMap
Parameters:
fromKey - low endpoint of the keys in the returned map
fromInclusive - true if the low endpoint is to be included in the returned view
toKey - high endpoint of the keys in the returned map
toInclusive - true if the high endpoint is to be included in the returned view
Returns:
a view of the portion of this map whose keys range from fromKey to toKey

headMap

public NavigableMap headMap(java.lang.Object toKey,
                            boolean toInclusive)
Description copied from interface: NavigableMap
Returns a view of the portion of this map whose keys are less than (or equal to, if inclusive is true) 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:
headMap in interface NavigableMap
Parameters:
toKey - high endpoint of the keys in the returned map
toInclusive - true if the high endpoint is to be included in the returned view
Returns:
a view of the portion of this map whose keys are less than (or equal to, if inclusive is true) toKey

tailMap

public NavigableMap tailMap(java.lang.Object fromKey,
                            boolean fromInclusive)
Description copied from interface: NavigableMap
Returns a view of the portion of this map whose keys are greater than (or equal to, if inclusive is true) 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:
tailMap in interface NavigableMap
Parameters:
fromKey - low endpoint of the keys in the returned map
fromInclusive - true if the low endpoint is to be included in the returned view
Returns:
a view of the portion of this map whose keys are greater than (or equal to, if inclusive is true) fromKey

comparator

public java.util.Comparator comparator()
Specified by:
comparator 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
Overrides:
isEmpty in class java.util.AbstractMap

containsValue

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

remove

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

putAll

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

keySet

public java.util.Set keySet()
Description copied from class: AbstractMap

Specified by:
keySet in interface java.util.Map
Specified by:
keySet in interface java.util.SortedMap
Overrides:
keySet in class AbstractMap

navigableKeySet

public NavigableSet navigableKeySet()
Description copied from interface: NavigableMap
Returns a NavigableSet view of the keys contained in this map. The set's iterator returns the keys in ascending 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:
navigableKeySet in interface NavigableMap
Returns:
a navigable set view of the keys in this map