How do I use the SortedMap interface?

package org.kodejava.example.util;

import java.util.*;

public class SortedMapDemo {
    public static void main(String[] args) {
        //
        // Copies all of the mappings from 'countryCapitals' to this
        // 'sorted'
        //
        SortedMap sorted = new TreeMap();
        sorted.put("United States", "New York");
        sorted.put("United Kingdom", "London");
        sorted.put("Netherlands", "Amsterdam");
        sorted.put("Japan", "Tokyo");
        sorted.put("France", "Paris");

        //
        // Gets the first(lowest) key currently in this map
        //
        String firstKey = sorted.firstKey();

        //
        // Gets the last (highest) key currently in this map
        //
        String lastKey = sorted.lastKey();

        System.out.println("First key: " + firstKey);
        System.out.println("Last key : " + lastKey);

        //
        // Gets a view of the portion of this map whose keys range
        // from fromKey(Japan), inclusive, to toKey(United Kingdom),
        // exclusive. (If fromKey and toKey are equal, the returned
        // map is empty.)
        //
        SortedMap sub = sorted.subMap("Japan",
                "United Kingdom");
        Set subKeys = sub.keySet();
        System.out.println("nSub Map: ");
        System.out.println("============");
        for (String key : subKeys) {
            System.out.println(key);
        }

        //
        // Gets a view of the portion of this map whose keys are
        // strictly less than toKey(in this example is Netherlands)
        //
        SortedMap head = sorted.headMap("Netherlands");
        Set headKeys = head.keySet();
        System.out.println("nHead Map:");
        System.out.println("============");
        for (String key : headKeys) {
            System.out.println(key);
        }

        //
        // Gets a view of the portion of this map whose keys are
        // strictly greater than or equal fromKey(in this example
        // is Netherlands)
        //
        SortedMap tail = sorted.tailMap("Netherlands");
        Set tailKeys = tail.keySet();
        System.out.println("nTail Map:");
        System.out.println("============");
        for (String key : tailKeys) {
            System.out.println(key);
        }
    }
}

Here is the output of the program:

First key: France
Last key : United States

Sub Map: 
============
Japan
Netherlands

Head Map:
============
France
Japan

Tail Map:
============
Netherlands
United Kingdom
United States
Wayan Saryada

Wayan Saryada

A programmer, runner, recreational diver, currently living in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, Hibernate / JPA.
Wayan Saryada

Leave a Reply