Create NavigableMap from TreeMap in Java

Java 8Object Oriented ProgrammingProgramming

To create NavigableMap from TreeMap. The following is the declaration −

NavigableMap<String, Integer> n = new TreeMap<String, Integer>();

Now, add some elements to the NavigableMap created above −

n.put("A", 888);
n.put("B", 999);
n.put("C", 444);
n.put("D", 555);
n.put("E", 666);
n.put("F", 888);
n.put("G", 999);
n.put("H", 444);
n.put("I", 555);
n.put("J", 666);

The following is an example to create NavigableMap from TreeMap and display it −

Example

 Live Demo

import java.util.*;
public class Demo {
   public static void main(String[] args) {
      NavigableMap<String, Integer> n = new TreeMap<String, Integer>();
      n.put("A", 888);
      n.put("B", 999);
      n.put("C", 444);
      n.put("D", 555);
      n.put("E", 666);
      n.put("F", 888);
      n.put("G", 999);
      n.put("H", 444);
      n.put("I", 555);
      n.put("J", 666);
      System.out.println("NavigableMap elements...\n"+n);
   }
}

Output

NavigableMap elements...
{A=888, B=999, C=444, D=555, E=666, F=888, G=999, H=444, I=555, J=666}
raja
Published on 11-Jan-2019 05:33:43
Advertisements