Package com.zybooks.dsaj.map
Class SortedTableMap<K,V>
java.lang.Object
com.zybooks.dsaj.map.AbstractMap<K,V>
com.zybooks.dsaj.map.AbstractSortedMap<K,V>
com.zybooks.dsaj.map.SortedTableMap<K,V>
- Type Parameters:
K
- The key type (keys must be unique and comparable)V
- The value type
An implementation of a map using a sorted table. All accessors run
in O(log n) worst-case time, other than subMap, which runs in O(s + log n)
where s is the size of the resulting submap, and the complete iterations
that run in O(n) time.
-
Nested Class Summary
Nested classes/interfaces inherited from class com.zybooks.dsaj.map.AbstractMap
AbstractMap.MapEntry<K,
V> -
Constructor Summary
ConstructorDescriptionConstructs an empty map using the natural ordering of keys.SortedTableMap
(Comparator<K> comp) Constructs an empty map using the given comparator to order keys. -
Method Summary
Modifier and TypeMethodDescriptionceilingEntry
(K key) Returns the entry with least key greater than or equal to given key (or null if no such key exists).entrySet()
Returns an iterable collection of all key-value entries of the map.Returns the entry having the least key (or null if map is empty).floorEntry
(K key) Returns the entry with greatest key less than or equal to given key (or null if no such key exists).Returns the value associated with the specified key, or null if no such entry exists.higherEntry
(K key) Returns the entry with least key strictly greater than given key (or null if no such key exists).Returns the entry having the greatest key (or null if map is empty).lowerEntry
(K key) Returns the entry with greatest key strictly less than given key (or null if no such key exists).Associates the given value with the given key.Removes the entry with the specified key, if present, and returns its associated value.int
size()
Returns the number of entries in the map.Returns an iterable containing all keys in the range fromfromKey
inclusive totoKey
exclusive.Methods inherited from class com.zybooks.dsaj.map.AbstractSortedMap
compare, compare, compare, compare
Methods inherited from class com.zybooks.dsaj.map.AbstractMap
isEmpty, keySet, values
-
Constructor Details
-
SortedTableMap
public SortedTableMap()Constructs an empty map using the natural ordering of keys. -
SortedTableMap
Constructs an empty map using the given comparator to order keys.- Parameters:
comp
- comparator defining the order of keys in the map
-
-
Method Details
-
size
public int size()Returns the number of entries in the map.- Returns:
- number of entries in the map
-
get
Returns the value associated with the specified key, or null if no such entry exists.- Parameters:
key
- the key whose associated value is to be returned- Returns:
- the associated value, or null if no such entry exists
-
put
Associates the given value with the given key. If an entry with the key was already in the map, this replaced the previous value with the new one and returns the old value. Otherwise, a new entry is added and null is returned.- Parameters:
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified key- Returns:
- the previous value associated with the key (or null, if no such entry)
-
remove
Removes the entry with the specified key, if present, and returns its associated value. Otherwise does nothing and returns null.- Parameters:
key
- the key whose entry is to be removed from the map- Returns:
- the previous value associated with the removed key, or null if no such entry exists
-
firstEntry
Returns the entry having the least key (or null if map is empty).- Returns:
- entry with least key (or null if map is empty)
-
lastEntry
Returns the entry having the greatest key (or null if map is empty).- Returns:
- entry with greatest key (or null if map is empty)
-
ceilingEntry
Returns the entry with least key greater than or equal to given key (or null if no such key exists).- Parameters:
key
- the target key- Returns:
- entry with least key greater than or equal to given (or null if no such entry)
-
floorEntry
Returns the entry with greatest key less than or equal to given key (or null if no such key exists).- Parameters:
key
- the target key- Returns:
- entry with greatest key less than or equal to given (or null if no such entry)
-
lowerEntry
Returns the entry with greatest key strictly less than given key (or null if no such key exists).- Parameters:
key
- the target key- Returns:
- entry with greatest key strictly less than given (or null if no such entry)
-
higherEntry
Returns the entry with least key strictly greater than given key (or null if no such key exists).- Parameters:
key
- the target key- Returns:
- entry with least key strictly greater than given (or null if no such entry)
-
entrySet
Returns an iterable collection of all key-value entries of the map.- Returns:
- iterable collection of the map's entries
-
subMap
Returns an iterable containing all keys in the range fromfromKey
inclusive totoKey
exclusive.- Parameters:
fromKey
- the low end of the submaptoKey
- the high end of the submap- Returns:
- iterable with keys in desired range
-