Java TreeMap.keySet()

Syntax

TreeMap.keySet() has the following syntax.

public Set <K> keySet()

Example

In the following code shows how to use TreeMap.keySet() method.


import java.util.Set;
import java.util.TreeMap;
/*  w  w w. j a  va 2  s.c  om*/
public class Main {
   public static void main(String[] args) {
      
      TreeMap<Integer, String>  treemap = new TreeMap<Integer, String> ();
      
      // populating tree map
      treemap.put(2, "two");
      treemap.put(1, "one");
      treemap.put(3, "three");
      treemap.put(6, "six");
      treemap.put(5, "from java2s.com");
      
      // creating set
      Set set=treemap.keySet();
      
      // getting set view         
      System.out.println("Checking values of the set");
      System.out.println("Value is: "+set);
   }    
}

The code above generates the following result.