NavigableMap Interface in Java with Example

Java 8Server Side ProgrammingProgramming

NavigableMap is an extension of the SortedMap collection framework. It is used to arrange the elements in a uniform fashion. NavigableMap has different methods to iterate over the elements in the Map.


Following is an example −

 Live Demo

import java.util.NavigableMap;
import java.util.TreeMap;
public class Demo {
   public static void main(String[] args) {
      NavigableMap<String, Integer> my_map = new TreeMap<String, Integer>();
      my_map.put("A", 856);
      my_map.put("M", 349);
      my_map.put("Z", 567);
      System.out.printf("The descending set is : %s%n", my_map.descendingKeySet());
      System.out.printf("The floor entry is : %s%n", my_map.floorEntry("A"));
      System.out.printf("The first key : %s%n", my_map.firstKey());
      System.out.printf("The reversed map : %s%n", my_map.descendingMap());


The descending set is : [Z, M, A]
The floor entry is : A=856
The first key : A
The reversed map : {Z=567, M=349, A=856}

A class named Demo contains the main function. An instance of the NavigableMap is created, and elements are added to the map with the help of the ‘put’ function. The relevant function are used to display the map in descending order, the first element of the map, the first key of the map, and the reversed version of the map.

Published on 14-Sep-2020 12:41:24