TreeMap navigableKeySet() method in java code example

Example 1: TreeMap navigableKeySet() method in java

import java.util.NavigableSet;
import java.util.TreeMap;
public class TreeMapNavigableKeySetMethodExample
{
   public static void main(String[] args)
   {
      try
      {
         TreeMap<String, Integer> tm = new TreeMap<String, Integer>();
         tm.put("apple", 11);
         tm.put("banana", 12);
         tm.put("grapes", 13);
         tm.put("orange", 14);
         tm.put("pineapple", 15);
         System.out.println("Given TreeMap: " + tm);
         // get NavigableSet view of keys using navigableKeySet() method
         NavigableSet<String> ns = tm.navigableKeySet();
         System.out.println("Value is: " + ns);
      }
      catch(NullPointerException ex)
      {
         System.out.println("Exception: " + ex);
      }
   }
}

Example 2: TreeMap navigableKeySet() method in java

import java.util.NavigableSet;
import java.util.TreeMap;
public class TreeMapNavigableKeySetMethodExample
{
   public static void main(String[] args)
   {
      try
      {
         TreeMap<Integer, String> tm = new TreeMap<Integer, String>();
         tm.put(11, "apple");
         tm.put(12, "banana");
         tm.put(13, "grapes");
         tm.put(14, "orange");
         tm.put(15, "pineapple");
         System.out.println("Given TreeMap: " + tm);
         // get NavigableSet view of keys using navigableKeySet() method
         NavigableSet<Integer> ns = tm.navigableKeySet();
         System.out.println("Value is: " + ns);
      }
      catch(NullPointerException ex)
      {
         System.out.println("Exception: " + ex);
      }
   }
}

Tags:

Java Example