How to get keys from a HashMap in Java?
How to Get Keys from a HashMap
in Java
In Java, HashMap
is a part of the Java Collections Framework and is used to store key-value pairs. Sometimes, you might need to retrieve all the keys from a HashMap
. This can be done in several ways using the keySet()
, entrySet()
, and forEach()
methods provided by the HashMap
class.
Using keySet()
The keySet()
method returns a Set
view of the keys contained in the HashMap
. This is the most straightforward way to get all the keys.
import java.util.HashMap; import java.util.Map; import java.util.Set; public class Main { public static void main(String[] args) { // Create a HashMap Map<String, Integer> map = new HashMap<>(); map.put("apple", 1); map.put("banana", 2); map.put("cherry", 3); // Get the keys using keySet() Set<String> keys = map.keySet(); // Iterate over the keys for (String key : keys) { System.out.println("Key: " + key); } } }
Using entrySet()
The entrySet()
method returns a Set
view of the mappings contained in the HashMap
. You can iterate over this set and extract the keys from each entry.
import java.util.HashMap; import java.util.Map; import java.util.Set; public class Main { public static void main(String[] args) { // Create a HashMap Map<String, Integer> map = new HashMap<>(); map.put("apple", 1); map.put("banana", 2); map.put("cherry", 3); // Get the entries using entrySet() Set<Map.Entry<String, Integer>> entries = map.entrySet(); // Iterate over the entries and get the keys for (Map.Entry<String, Integer> entry : entries) { String key = entry.getKey(); System.out.println("Key: " + key); } } }
Using forEach()
Java 8 introduced the forEach()
method, which allows you to perform an action for each entry in the HashMap
. You can use this method to retrieve the keys.
import java.util.HashMap; import java.util.Map; public class Main { public static void main(String[] args) { // Create a HashMap Map<String, Integer> map = new HashMap<>(); map.put("apple", 1); map.put("banana", 2); map.put("cherry", 3); // Use forEach to iterate over the keys map.forEach((key, value) -> System.out.println("Key: " + key)); } }
Summary
- keySet(): Returns a
Set
view of the keys contained in theHashMap
. It’s the most direct way to get all the keys. - entrySet(): Returns a
Set
view of the mappings contained in theHashMap
. You can iterate over this set and extract the keys. - forEach(): Allows you to perform an action for each entry in the
HashMap
. You can use it to retrieve and process the keys.
Example of All Methods Combined
Here’s a combined example demonstrating all the above methods:
import java.util.HashMap; import java.util.Map; import java.util.Set; public class Main { public static void main(String[] args) { // Create a HashMap Map<String, Integer> map = new HashMap<>(); map.put("apple", 1); map.put("banana", 2); map.put("cherry", 3); // Using keySet() System.out.println("Using keySet():"); Set<String> keys = map.keySet(); for (String key : keys) { System.out.println("Key: " + key); } // Using entrySet() System.out.println("\nUsing entrySet():"); Set<Map.Entry<String, Integer>> entries = map.entrySet(); for (Map.Entry<String, Integer> entry : entries) { String key = entry.getKey(); System.out.println("Key: " + key); } // Using forEach() System.out.println("\nUsing forEach():"); map.forEach((key, value) -> System.out.println("Key: " + key)); } }
By using these methods, you can effectively retrieve and work with the keys in a HashMap
in Java. For more in-depth knowledge and practical examples on Java collections and other programming concepts, consider exploring Grokking the Coding Interview on DesignGurus.io, which provides comprehensive courses on essential coding and interview techniques.
GET YOUR FREE
Coding Questions Catalog