Name

SortedMap<K,V>

Synopsis

This interface represents a Map object that keeps its set of key objects in sorted order. As with Map, it is conventional that all implementations of this interface define a no-argument constructor to create an empty map and a copy constructor that accepts a Map object that specifies the initial contents of the SortedMap. Furthermore, when creating a SortedMap, there should be a way to specify a Comparator object to sort the key objects of the map. If no Comparator is specified, all key objects must implement the java.lang.Comparable interface so they can be sorted in their natural order. See also Map, TreeMap, and SortedSet.

The inherited keySet( ), values( ), and entrySet( ) methods return collections that can be iterated in the sorted order. firstKey( ) and lastKey( ) return the lowest and highest key values in the SortedMap. subMap( ) returns a SortedMap that contains only mappings for keys from (and including) the first specified key up to (but not including) the second specified key. headMap( ) returns a SortedMap that contains mappings whose keys are less than (but not equal to) the specified key. tailMap( ) returns a SortedMap that contains mappings whose keys are greater than or equal to the specified key. subMap( ), headMap( ), and tailMap( ) return SortedMap objects that are simply views of the original SortedMap; any changes in the original map are reflected in the returned map and vice versa.

Figure 16-56. java.util.SortedMap<K,V>

Get Java in a Nutshell, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.