The SortedMap Interface Flashcards
The SortedMap Interface (5 cards)
The SortedMap Interface
A SortedMap is a Map that maintains its entries in ascending order, sorted according to the keys’ natural ordering, or according to a Comparator provided at the time of the SortedMap creation. Natural ordering and Comparators are discussed in the Object Ordering section.
The SortedMap interface provides which operations for normal Map operations?
Range view — performs arbitrary range operations on the sorted map
Endpoints — returns the first or the last key in the sorted map
Comparator access — returns the Comparator, if any, used to sort the map
SortedMap Interface
The following interface is the Map analog of SortedSet.
public interface SortedMap extends Map{ Comparator super K> comparator(); SortedMap subMap(K fromKey, K toKey); SortedMap headMap(K toKey); SortedMap tailMap(K fromKey); K firstKey(); K lastKey(); }
SortedMap Operations
The operations SortedMap inherits from Map behave identically on sorted maps and normal maps with two exceptions:
The Iterator returned by the iterator operation on any of the sorted map’s Collection views traverse the collections in order.
The arrays returned by the Collection views’ toArray operations contain the keys, values, or entries in order.
Although it isn’t guaranteed by the interface, the toString method of the Collection views in all the Java platform’s SortedMap implementations returns a string containing all the elements of the view, in order.
SortedMap Comparison to SortedSet
Because this interface is a precise Map analog of SortedSet, all the idioms and code examples in The SortedSet Interface section apply to SortedMap with only trivial modifications.