java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to use the SortedMap interface?

package com.javacoderanch.example.util;

import org.apache.commons.lang.ArrayUtils;

import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

public class SortedMapDemo {
    public static void main(String[] args) {
        //
        // A two dimensional array of countries capital.
        //
        String[][] countries = {{"United States", "New York"},
                {"United Kingdom", "London"},
                {"Netherlands", "Amsterdam"},
                {"Japan", "Tokyo"},
                {"France", "Paris"}};

        //
        // To convert an array to a Map each array elements must
        // be an array with at least two elements where the first
        // element will be the key and the second element will
        // be the value.
        //
        @SuppressWarnings("unchecked")
        Map<String, String> countryCapitals = ArrayUtils.toMap(countries);

        //
        // Copies all of the mappings from 'countryCapitals' to this
        // 'sorted'
        //
        SortedMap<String, String> sorted = new TreeMap<String, String>();
        sorted.putAll(countryCapitals);

        //
        // 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<String, String> sub = sorted.subMap("Japan",
                "United Kingdom");
        Set<String> 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<String, String> head = sorted.headMap("Netherlands");
        Set<String> 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<String, String> tail = sorted.tailMap("Netherlands");
        Set<String> 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