|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
ASortedMapis a
Mapthat maintains its entries in ascending order, sorted according to the keys' natural order, or according to a
Comparatorprovided atSortedMapcreation time. (Natural order andComparators are discussed in the section on Object Ordering.) In addition to the normalMapoperations, theMapinterface provides operations for:
- Range-view: Performs arbitrary range operations on the sorted map.
- Endpoints: Returns the first or last key in the sorted map.
- Comparator access: Returns the
Comparatorused to sort the map (if any).This interface is thepublic interface SortedMap extends Map { Comparator comparator(); SortedMap subMap(Object fromKey, Object toKey); SortedMap headMap(Object toKey); SortedMap tailMap(Object fromKey); Object firstKey(); Object lastKey(); }Mapanalogue ofSortedSet.
The operations thatSortedMapinherits fromMapbehave identically on sorted maps and normal maps with two exceptions:Although it isn't guaranteed by the interface, the
- The
Iteratorreturned by theiteratoroperation on any of the sorted map'sCollection-views traverse the collections in key-order.- The arrays returned by the
Collection-views'toArrayoperations contain the keys, values, or entries in key-order.toStringmethod of theCollection-views in all the JDK'sSortedMapimplementations returns a string containing all the elements of the view, in key-order.For example, consider the following snippet:
SortedMap m = new TreeMap(); m.put("Sneezy", "common cold"); m.put("Sleepy", "narcolepsy"); m.put("Grumpy", "seasonal affective disorder"); System.out.println(m.keySet()); System.out.println(m.values()); System.out.println(m.entrySet());Running this snippet produces this output:
[Grumpy, Sleepy, Sneezy] [seasonal affective disorder, narcolepsy, common cold] [Grumpy=seasonal affective disorder, Sleepy=narcolepsy, Sneezy=common cold]
By convention, allMapimplementations provide a standard constructor that takes aMap, andSortedMapimplementations are no exception. This constructor creates aSortedMapobject that orders its entries according to their keys' natural order. Additionally, by convention,SortedMapimplementations provide two other standard constructors:
- One that takes a
Comparatorand returns a new (empty)SortedMapsorted according to the specifiedComparator.- One that takes a
SortedMapand returns a newSortedMapcontaining the same mappings as the givenSortedMap, and sorted according to the sameComparator(or using the elements' natural ordering, if the specifiedSortedMapdid too). Note that it is the compile-time type of the argument that determines whether this constructor is invoked in preference to the ordinaryMapconstructor, and not its runtime type!The first of these standard constructors is the normal way to create an empty
SortedMapwith an explicitComparator. The second is similar in spirit to the standardMapconstructor: It creates a copy of aSortedMapwith the same ordering, but with a programmer specified implementation type.The following snippet illustrates how this works:
final Comparator FUNNY_COMPARATOR = ... ; Map m = new TreeMap(FUNNY_COMPARATOR); // ... code to populate m Map m2 = new TreeMap(m); // invokes TreeMap(Map) Map m3 = new TreeMap((SortedMap)m) // invokes TreeMap(SortedMap)Note that
m2.comparator()will returnnullwhilem3.comparator()will returnFUNNY_COMPARATOR. In other words,m2is sorted according to its keys' natural ordering, whilem3is sorted according to the ordering induced byFUNNY_COMPARATOR.
Because this interface is a preciseMapanalogue ofSortedSet, all of the idioms and code examples in theSortedSetsection apply toSortedMap, with only trivial modifications.
|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.