close
The Wayback Machine - https://web.archive.org/web/20240529104637/https://www.geeksforgeeks.org/hashmap-keyset-method-in-java/
Open In App

HashMap keySet() Method in Java

Last Updated : 26 Nov, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

The java.util.HashMap.keySet() method in Java is used to create a set out of the key elements contained in the hash map. It basically returns a set view of the keys or we can create a new set and store the key elements in them.

Syntax:

hash_map.keySet()

Parameters: The method does not take any parameter.

Return Value: The method returns a set having the keys of the hash map.

Below programs are used to illustrate the working of java.util.HashMap.keySet() Method:
Program 1: Mapping String Values to Integer Keys.




// Java code to illustrate the keySet() method
import java.util.*;
  
public class Hash_Map_Demo {
    public static void main(String[] args)
    {
  
        // Creating an empty HashMap
        HashMap<Integer, String> hash_map = new HashMap<Integer, String>();
  
        // Mapping string values to int keys
        hash_map.put(10, "Geeks");
        hash_map.put(15, "4");
        hash_map.put(20, "Geeks");
        hash_map.put(25, "Welcomes");
        hash_map.put(30, "You");
  
        // Displaying the HashMap
        System.out.println("Initial Mappings are: " + hash_map);
  
        // Using keySet() to get the set view of keys
        System.out.println("The set is: " + hash_map.keySet());
    }
}


Output:

Initial Mappings are: {20=Geeks, 25=Welcomes, 10=Geeks, 30=You, 15=4}
The set is: [20, 25, 10, 30, 15]

Program 2: Mapping Integer Values to String Keys.




// Java code to illustrate the keySet() method
import java.util.*;
  
public class Hash_Map_Demo {
    public static void main(String[] args)
    {
  
        // Creating an empty HashMap
        HashMap<String, Integer> hash_map = new HashMap<String, Integer>();
  
        // Mapping int values to string keys
        hash_map.put("Geeks", 10);
        hash_map.put("4", 15);
        hash_map.put("Geeks", 20);
        hash_map.put("Welcomes", 25);
        hash_map.put("You", 30);
  
        // Displaying the HashMap
        System.out.println("Initial Mappings are: " + hash_map);
  
        // Using keySet() to get the set view of keys
        System.out.println("The set is: " + hash_map.keySet());
    }
}


Output:

Initial Mappings are: {4=15, Geeks=20, You=30, Welcomes=25}
The set is: [4, Geeks, You, Welcomes]

Note: The same operation can be performed with any type of Mappings with variation and combination of different data types.



Similar Reads

Hashmap methods in Java with Examples | Set 2 (keySet(), values(), containsKey()..)
HashMap Class Methods in Java with Examples | Set 1 (put(), get(), isEmpty() and size()) In this post more methods are discussed. keySet(): java.util.HashMap.keySet() It returns a Set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. Syntax: public Set keySet() Retu
6 min read
How to Copy One HashMap to Another HashMap in Java?
HashMap is similar to the HashTable, but it is unsynchronized. It allows to store the null keys as well, but there should be only one null key object and there can be any number of null values. This class makes no guarantees as to the order of the map. To use this class and its methods, you need to import java.util.HashMap package or its superclass
4 min read
Converting Integer-String HashMap to String-Array-Integer HashMap in Java
Efficient Conversion of a HashMap&lt;Integer, String&gt; into another HashMap&lt;String, Integer[]&gt; involves organizing integer keys based on their corresponding string values. Here's a concise and efficient Java method to accomplish this transformation, focusing on clear and straightforward implementation. Step-by-Step Implementation of Convers
3 min read
WeakHashMap keySet() method in Java
The java.util.WeakHashMap.keySet() method in Java is used to create a set out of the key elements contained in the hash map. It basically returns a set view of the keys or we can create a new set and store the key elements in them. Syntax: Weak_Hash_Map.keySet() Parameters: The method does not take any parameter. Return Value: The method returns a
2 min read
TreeMap keySet() Method in Java with Examples
In Java, keySet() method of TreeMap class is present inside java.util package in Java is used to create a set out of the key elements contained in the treemap. It basically returns a set view of the keys or we can create a new set and store the key elements in them in ascending order. Since the set is backed by the map, any changes made to the map
3 min read
EnumMap keySet() Method in Java
The Java.util.EnumMap.keySet() method in Java is used to return the set view of the keys contained in the map. Syntax: Enum_Map.keySet() Parameters: The method does not accept any argument. Return Value: The method returns the set view of the keys contained in the map. Below programs illustrate the keySet() function: Program 1: // Java program to d
2 min read
IdentityHashMap keySet() Method in Java
The java.util.IdentityHashMap.keySet() method in Java is used to create a set out of the key elements contained in the hash map. It basically returns a set view of the keys or we can create a new set and store the key elements in them. Syntax: Identity_Hash_Map.keySet() Parameters: The method does not take any parameter. Return Value: The method re
2 min read
Provider keySet() method in Java with Examples
The keySet() method of java.security.Provider class is used to return an unmodifiable Set view of the property keys contained in this provider. Syntax: public Set keySet() Return Value: This method returns a set view of the keys contained in this map. Below are the examples to illustrate the keySet() method: Program 1: // Java program to demonstrat
3 min read
AbstractMap keySet() Method in Java with Examples
The AbstractMap.keySet() method in Java is used to create a set out of the key elements contained in the abstract map. It basically returns a set view of the keys or we can create a new set and store the key elements in them. Syntax: AbstractMap.keySet() Parameters: The method does not take any parameter. Return Value: The method returns a set havi
2 min read
ConcurrentHashMap keySet() method in Java with Examples
The keySet() method of ConcurentHashMap class in Java is used to create a set out of the key elements contained in the hash map. It basically returns a set view of the keys or we can create a new set and store the key elements in them. Syntax: ConcurrentHashMap.keySet() Parameters: The method does not take any parameter. Return Value: The method re
2 min read