Java HashMap keySet() Method Last Updated : 16 Jan, 2025 Comments Improve Suggest changes Like Article Like Report The keySet() method of the Java HashMap class is used to return a set containing all the keys in the map. This set is backed by the HashMap, so if we make any changes to the HashMap, it will be reflected in the Set as well.Example 1: The below Java program demonstrates creating a HashMap, adding key-value pairs, displaying its contents, and retrieving the set view of keys using the keySet() method. Java // Java program to demonstrate the working of keySet() import java.util.HashMap; public class Geeks { public static void main(String[] args) { // Creating an empty HashMap HashMap<Integer, String> m = new HashMap<>(); // Adding key-value pairs m.put(1, "Geeks"); m.put(2, "For"); m.put(3, "Geeks"); m.put(4, "Welcomes"); m.put(5, "You"); // Displaying the HashMap System.out.println("Initial Mappings: " + m); // Using keySet() to get the set view of keys System.out.println("The keys are: " + m.keySet()); } } OutputInitial Mappings: {1=Geeks, 2=For, 3=Geeks, 4=Welcomes, 5=You} The keys are: [1, 2, 3, 4, 5] Syntax of keySet() Methodpublic Set<K> keySet()Return Type: Return a set containing all the keys in the HashMapExample 2: The below program demonstrates how keySet() behaves when duplicate keys are inserted into a HashMap. Java // Java program to demonstrate // keySet() with duplicate keys import java.util.HashMap; public class Geeks { public static void main(String[] args) { // Creating an empty HashMap HashMap<String, Integer> m = new HashMap<>(); // Adding key-value pairs m.put("Geeks", 10); m.put("for", 20); m.put("Geeks", 30); m.put("Welcomes", 40); m.put("You", 50); // Displaying the HashMap System.out.println("Initial Mappings: " + m); // Using keySet() to get the set view of keys System.out.println("The keys are: " + m.keySet()); } } OutputInitial Mappings: {Geeks=30, for=20, You=50, Welcomes=40} The keys are: [Geeks, for, You, Welcomes] Comment More infoAdvertise with us Next Article Java HashMap keySet() Method C chinmoy lenka Follow Improve Article Tags : Misc Java Java-Collections Java - util package Java-Functions Java-HashMap +2 More Practice Tags : JavaJava-CollectionsMisc Similar Reads 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 n 2 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 2 min read Java HashMap put() Method The put() method of the Java HashMap class is used to add or update the key-value pairs in the map. If the key already exists in the map, the previous value associated with the key is replaced by the new value and If the key does not exist, the new key-value pair is added to the map.Syntax of HashMa 1 min read Java HashMap putAll() Method The putAll() method of the Java HashMap class is used to copy all key-value mappings from another map to the existing map. If a key exists in both maps, its value is updated with the value from the source map. Otherwise, new key-value pairs are added.Example 1: This example demonstrates copying all 2 min read Java HashMap remove() Method The remove() method of the Java HashMap class is used to remove a key-value pair from the map based on the given key. If the key is found, the mapping associated with the key is returned, otherwise, it returns null.Example 1: This example demonstrates how to use remove(Object key) to remove a key-va 3 min read Like