java.util.HashMap.keySet() Method


Advertisements


Description

The keySet() method is used to get a Set view of the keys contained in this map.

Declaration

Following is the declaration for java.util.HashMap.keySet() method.

public Set<K> keySet()

Parameters

NA

Return Value

The method call returns a set view of the keys contained in this map.

Exception

NA

Example

The following example shows the usage of java.util.HashMap.keySet()

Live Demo
package com.tutorialspoint;

import java.util.*;

public class HashMapDemo {
   public static void main(String args[]) {
      
      // create hash map
      HashMap newmap = new HashMap();

      // populate hash map
      newmap.put(1, "tutorials");
      newmap.put(2, "point");
      newmap.put(3, "is best");

      // get keyset value from map
      Set keyset = newmap.keySet();

      // check key set values
      System.out.println("Key set values are: " + keyset);
   }    
}

Let us compile and run the above program, this will produce the following result.

Key set values are: [1, 2, 3]

java_util_hashmap.htm

Advertisements