Java - Collection Framework Navigable Maps

Introduction

A navigable map is represented by NavigableMap interface.

It extends the SortedMap interface.

TreeMap class implements class for the NavigableMap interface.

Demo

import java.util.Map.Entry;
import java.util.NavigableMap;
import java.util.TreeMap;

public class Main {
  public static void main(String[] args) {
    // Create a sorted map sorted on string keys alphabetically
    NavigableMap<String, String> nMap = new TreeMap<>();
    nMap.put("XML", "(342)113-1234");
    nMap.put("Javascript", "(245)890-2345");
    nMap.put("Json", "(205)678-3456");
    nMap.put("Java", "(205)678-3456");

    System.out.println("Navigable Map:" + nMap);

    // Get the closest lower and higher matches for Java
    Entry<String, String> lowerJava = nMap.lowerEntry("Java");
    Entry<String, String> floorJava = nMap.floorEntry("Java");
    Entry<String, String> higherJava = nMap.higherEntry("Java");
    Entry<String, String> ceilingJava = nMap.ceilingEntry("Java");

    System.out.println("Lower Java: " + lowerJava);
    System.out.println("Floor Java: " + floorJava);
    System.out.println("Higher Java: " + higherJava);
    System.out.println("Ceiling Java: " + ceilingJava);

    // Get the reverse order view of the map
    NavigableMap<String, String> reverseMap = nMap.descendingMap();
    System.out.println("Navigable Map(Reverse Order):" + reverseMap);
  }//w w w  .  j ava  2 s .c  om
}

Result