SortedMap

In this chapter you will learn:

  1. How to use SortedMap
  2. How to get first key from SortedMap
  3. How to get last key and last key value pair in SortedMap

Use SortedMap

SortedMap is a map that maintains its entries in ascending order. Sorted Map sorts the entries according to the keys' natural ordering or according to a comparator that is supplied when the sorted map is created.

Sorted maps are described by the SortedMap interface. TreeMap is an example of a sorted map.

SortedMap, whose generic type is SortedMap<K,V>, extends Map.

import java.util.Comparator;
import java.util.SortedMap;
import java.util.TreeMap;
/*from  ja v  a 2 s  .com*/
public class Main {
  public static void main(String[] args) {
    SortedMap<String, Integer> sortedMap = new TreeMap<String, Integer>();
    String[] officeSupplies = { "a", "b", "c", "d", "e" };
    
    int[] quantities = { 20, 30, 5, 10, 20 };
    for (int i = 0; i < officeSupplies.length; i++){
      sortedMap.put(officeSupplies[i], quantities[i]);
    }
      
    System.out.println(sortedMap);
    System.out.println(sortedMap.headMap("c"));
    System.out.println(sortedMap.headMap("c"));
    SortedMap<String, Integer> smsiCopy;
    Comparator<String> cmp;
    cmp = new Comparator<String>() {
      public int compare(String key1, String key2) {
        return key2.compareTo(key1); // descending order
      }
    };
    smsiCopy = new TreeMap<String, Integer>(cmp);
    smsiCopy.putAll(sortedMap);
    System.out.println(smsiCopy);
  }
}

The output:

Get first key from SortedMap

import java.util.Calendar;
import java.util.Locale;
import java.util.Map;
import java.util.NavigableMap;
import java.util.TreeMap;
//from jav a 2 s  . com
public class Main {
  public static void main(String args[]) {
    Calendar now = Calendar.getInstance();
    Locale locale = Locale.getDefault();

    Map<String, Integer> names = now.getDisplayNames(Calendar.DAY_OF_WEEK, Calendar.LONG, locale);
    NavigableMap<String, Integer> nav = new TreeMap<String, Integer>(names);
    System.out.printf("Whole list:%n%s%n", nav);
    System.out.printf("First key: %s\tFirst entry: %s%n", nav.firstKey(), nav.firstEntry());
  }
}

The code above generates the following result.

Last key and last key value pair in SortedMap

import java.util.Calendar;
import java.util.Locale;
import java.util.Map;
import java.util.NavigableMap;
import java.util.TreeMap;
// jav a 2  s  . c  o  m
public class Main {
  public static void main(String args[]) {
    Calendar now = Calendar.getInstance();
    Locale locale = Locale.getDefault();

    Map<String, Integer> names = now.getDisplayNames(Calendar.DAY_OF_WEEK, Calendar.LONG, locale);
    NavigableMap<String, Integer> nav = new TreeMap<String, Integer>(names);
    System.out.printf("Whole list:%n%s%n", nav);
    System.out.printf("Last key: %s\tLast entry: %s%n", nav.lastKey(), nav.lastEntry());
  }
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. Get to know HashSet class
Home » Java Tutorial » Map
Map interface
Map element adding
Map.Entry class
Map key
Map value
Map key/value search
Map delete/remove
Map comparison
HashMap Class
HashMap search
HashMap clone
TreeMap
TreeMap key
TreeMap head sub map
TreeMap tail sub map
TreeMap sub map
NavigableMap
NavigableMap key
NavigableMap key-value pair
LinkedHashMap Class
IdentityHashMap
SortedMap