Java Collection Tutorial - Java HashMap.keySet()


HashMap.keySet() has the following syntax.

public Set <K> keySet()


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

/*from   ww w .jav  a 2  s .  c o  m*/
import java.util.HashMap;
import java.util.Set;

public class Main {
   public static void main(String args[]) {
      HashMap<Integer, String> newmap = new HashMap<Integer, String>();      
      // populate hash map
      newmap.put(1, "tutorials");
      newmap.put(2, "from");
      newmap.put(3, "");
      // get keyset value from map
      Set keyset=newmap.keySet();
      // check key set values
      System.out.println("Key set values are: " + keyset);

The code above generates the following result.