The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Collections
Lesson: Interfaces

The SortedMap Interface

A SortedMap(in the API reference documentation)is a Map(in the API reference documentation)that maintains its entries in ascending order, sorted according to the keys' natural order, or according to a Comparator provided at SortedMap creation time. (Natural order and Comparators are discussed in the section on Object Ordering.) In addition to the normal Map operations, the Map interface provides operations for:
public interface SortedMap extends Map {
    Comparator comparator();

    SortedMap subMap(Object fromKey, Object toKey);
    SortedMap headMap(Object toKey);
    SortedMap tailMap(Object fromKey);

    Object firstKey();
    Object lastKey();
}
This interface is the Map analogue of SortedSet(in the API reference documentation).

Map Operations

The operations that SortedMap inherits from Map behave identically on sorted maps and normal maps with two exceptions: Although it isn't guaranteed by the interface, the toString method of the Collection-views in all the JDK's SortedMap implementations 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]

Standard Constructors

By convention, all Map implementations provide a standard constructor that takes a Map, and SortedMap implementations are no exception. This constructor creates a SortedMap object that orders its entries according to their keys' natural order. Additionally, by convention, SortedMap implementations provide two other standard constructors:

The first of these standard constructors is the normal way to create an empty SortedMap with an explicit Comparator. The second is similar in spirit to the standard Map constructor: It creates a copy of a SortedMap with 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 return null while m3.comparator() will return FUNNY_COMPARATOR. In other words, m2 is sorted according to its keys' natural ordering, while m3 is sorted according to the ordering induced by FUNNY_COMPARATOR.

Comparison to SortedSet

Because this interface is a precise Map analogue of SortedSet, all of the idioms and code examples in the SortedSet section apply to SortedMap, with only trivial modifications.

Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2001 Sun Microsystems, Inc. All rights reserved.