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;
/*  j a v  a2  s.  c  o m*/
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  j av  a2  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("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;
/* java2s  . 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 » Collections

List interface
List add/insert elements
List clear/remove elements
List search
List element get and set
List and its Iterator
List size, empty
List conversion, to array
List to sublist
List comparison
ArrayList
ArrayList Creation
ArrayList add/insert
ArrayList get/set element
ArrayList clear/remove
ArrayList search
ArrayList copy and shallow copy
ArrayList size, trim to size and capacity
ArrayList to array
LinkedList class
LinkedList creation
LinkedList add/insert elements
LinkedList get elements
LinkedList search
LinkeList replace/set elements
LinkedList remove element
LinkedList copy
LinkedList iterator
LinkedList peek element
LinkedList pop/push element
LinkedList conversion
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
HashSet
HashSet element adding
HashSet element removing and clearing
HashSet clone
HashSet iterator
HashSet properties
TreeSet
TreeSet elements adding
TreeSet subSet
NavigableSet
LinkedHashSet
Iterator
ListIterator
List filling
List reversing
List rotating and shuffling
List sorting
List element swap
List element replacing
List copy
List binary search
Collection unmodifiable
Collection synchronized
Collection singleton
Collection max/min value
Empty Collections
Comparator
Comparable
Enumeration
EnumSet
EnumMap Class
PriorityQueue