Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions
Privacy Policy




Thinking in Java
Prev Contents / Index Next


If you have a SortedMap (of which TreeMap is the only one available), the keys are guaranteed to be in sorted order, which allows additional functionality to be provided with these methods in the SortedMap interface:

Comparator comparator( ): Produces the comparator used for this Map, or null for natural ordering.

Object firstKey( ): Produces the lowest key.

Object lastKey( ): Produces the highest key.

SortedMap subMap(fromKey, toKey): Produces a view of this Map with keys from fromKey, inclusive, to toKey, exclusive.

SortedMap headMap(toKey): Produces a view of this Map with keys less than toKey.

SortedMap tailMap(fromKey): Produces a view of this Map with keys greater than or equal to fromKey.

Here’s an example that’s similar to and shows this additional behavior of TreeMaps:

import com.bruceeckel.util.*;
//import java.util.*;

public class SimplePairGenerator implements MapGenerator {
  public Pair[] items = {
    new Pair("one", "A"), new Pair("two", "B"),
    new Pair("three", "C"), new Pair("four", "D"),
    new Pair("five", "E"), new Pair("six", "F"),
    new Pair("seven", "G"), new Pair("eight", "H"),
    new Pair("nine", "I"), new Pair("ten", "J")
  private int index = -1;
  public Pair next() {
    index = (index + 1) % items.length;
    return items[index];
  public static SimplePairGenerator gen =
    new SimplePairGenerator();
} ///:~

// What you can do with a TreeMap.
import com.bruceeckel.simpletest.*;
import com.bruceeckel.util.*;
import java.util.*;

public class SortedMapDemo {
  private static Test monitor = new Test();
  public static void main(String[] args) {
    TreeMap sortedMap = new TreeMap();
      sortedMap, SimplePairGenerator.gen, 10);
      low = sortedMap.firstKey(),
      high = sortedMap.lastKey();
    Iterator it = sortedMap.keySet().iterator();
    for(int i = 0; i <= 6; i++) {
      if(i == 3) low =;
      if(i == 6) high =;
    System.out.println(sortedMap.subMap(low, high));
    monitor.expect(new String[] {
      "{eight=H, five=E, four=D, nine=I, one=A, seven=G," +
      " six=F, ten=J, three=C, two=B}",
      "{nine=I, one=A, seven=G, six=F}",
      "{eight=H, five=E, four=D, nine=I, " +
      "one=A, seven=G, six=F}",
      "{nine=I, one=A, seven=G, six=F, " +
      "ten=J, three=C, two=B}"
} ///:~

Here, the pairs are stored by key-sorted order. Because there is a sense of order in the TreeMap, the concept of “location” makes sense, so you can have first and last elements and submaps.
Thinking in Java
Prev Contents / Index Next

   Reproduced courtesy of Bruce Eckel, MindView, Inc. Design by Interspire