How to get all keys of HashMap

In this tutorial we will see how to java get keys from map . We will see different method for hashmap get keys in java . We can get keys from hashmap in java using keySet() and entrySet() .

Example 1: Java Program for Hashmap Get keys Using keySet()

In this example we will initialize hashmap hm with mapping String and Integer. Then we will get set of keys from hashmap using keySet() method . After that this set of keys will iterate and it will print the all keys from hashmap .

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class HashmapGetKeys {
	
	public static void main(String[] args) {
		
    	Map<String,Integer> hm = new HashMap<String,Integer>();

		hm.put("Java",1);
		hm.put("Vogue",2);
		hm.put("Welcomes",3);
		hm.put("You",4);
		hm.put("Map", 5);
		
		//get set of keys from hashmap
		Set<String> keys = hm.keySet();
		
		//iterate set of keys 
        for(String key: keys){
            System.out.println("key is : "+key);
        }
		
	}

}

Output:

key is : Java
key is : Vogue
key is : Map
key is : You
key is : Welcomes

Example 2 : Java Program for Hashmap Get Arary of keys Using keySet()

Now we will see example of java map get keys using keySet() .In this example we will get array of keys from hashmap using keySey() . Then we will iterate this array of keys and print all keys from HashMap .

import java.util.HashMap;
import java.util.Map;

public class HashmapGetKeys1 {
	
	public static void main(String[] args) {
		
		Map<String,Integer> hm = new HashMap<String,Integer>();

		hm.put("Java",1);
		hm.put("Vogue",2);
		hm.put("Welcomes",3);
		hm.put("You",4);
		hm.put("Map", 5);

		//get array of keys from hashmap
		Object[] keys = hm.keySet().toArray();
		//iterate array of keys 
		for (int i = 0; i < keys.length; i++) {
			System.out.println("key is : "+ keys[i]);
		}
	}
}

Output:

key is : Java
key is : Vogue
key is : Map
key is : You
key is : Welcomes

Example 3 : Java Program for Hashmap Get keys Using entrySet()

In this example we will initialize hashmap hm with mapping String and Integer. Then we will get set of Entry from Hashmap using entrySet() method . Now we will iterate this set of Entry and print key using getKey() method of Entry .

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class HashmapGetKeys3 {
	public static void main(String[] args) {
		
		Map<String,Integer> hm = new HashMap<String,Integer>();

		hm.put("Java",1);
		hm.put("Vogue",2);
		hm.put("Welcomes",3);
		hm.put("You",4);
		hm.put("Map", 5);

		//get entry set from hashmap
		Set<Entry<String, Integer>> entrySet = hm.entrySet();
		
		//iterate entry set 
		for (Entry<String, Integer> entry : entrySet) {
			System.out.println("key is : "+ entry.getKey());
		}
		
	}

}

Output:

key is : Java
key is : Vogue
key is : Map
key is : You
key is : Welcomes

In this tutorial we have learned how to get key of hashmap java . We have see different methods of java get keys from map . You can learn more java HashMap program for practice .

Leave a Reply

Your email address will not be published. Required fields are marked *

5 + 4 =