Java Hashmap entrySet() Method

In this tutorial , We will learn about java HashMap entrySet() method . entrySet() method in map returns the set view of the entries in this map. First we will discuss about Syntax of  java map.entryset() method.

Java HashMap entrySet() Method :

Java Hashmap entrySet() method returns a Set view of the mappings contained in this map

Syntax :

Set<Entry<K, V>> entrySet()

Parameters : It does not take any parameters.

Return Value : It returns the set of entries

Now we will see java map entryset() method examples .

Examples 1:  Java HashMap entrySet() method – Mapping Integer to String keys

In this example , We will initialize hashmap hm with mapping String and Integer . We will add element in HashMap using put() method . To get Set view of this map , we will call entrySet() method on this HashMap.

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

public class HashmapEntryset {
	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);
		
		System.out.println("HashMap  :" + hm);
		
		//Using entrySet() to get the set view
		Set<Entry<String,Integer>> set = hm.entrySet();
		
        //print Set view
		System.out.println("Key-Value entries in map: :" + set);
        
	}

}

Output:

HashMap  :{Java=1, Vogue=2, Map=5, You=4, Welcomes=3}
Key-Value entries in map: :[Java=1, Vogue=2, Map=5, You=4, Welcomes=3]

Examples 2:  Java HashMap entrySet() method – Mapping String to Integer keys

In this example , We will initialize hashmap hm with mapping Integer and String. We will see how to entrySet in map return set of entries here . To get Set of entries of this map , we will call entrySet() method on this HashMap.  Let,s see java hashmap entryset() method example for String to Integer Keys mapping .

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

public class HashmapEntryset1 {
	
	public static void main(String[] args) {
		  
        Map<Integer,String> hm = new HashMap<Integer,String>();
		
		hm.put(1,"Java");
		hm.put(2,"Vogue");
		hm.put(3,"Welcomes");
		hm.put(4,"You");
		hm.put(5,"Map");
		
		
		System.out.println("HashMap  :" + hm);
		  
		//Using entrySet() to get the set view
		Set<Entry<Integer,String>> set = hm.entrySet();
		
        //print Set view
		System.out.println("Key-Value entries in map: :" + set);
		
	}

}

Output:

HashMap  :{1=Java, 2=Vogue, 3=Welcomes, 4=You, 5=Map}
Key-Value entries in map: :[1=Java, 2=Vogue, 3=Welcomes, 4=You, 5=Map]

Examples 3:  entrySet() method with Empty HashMap

In this example we will create empty Hashmap , and call java map entryset() method to get set of entries . It returns empty set here . Let,s see java program for map.entryset() method with empty Hashmap .

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

public class HashmapEntryset2 {
	
	public static void main(String[] args) {
		
        Map<Integer,String> hm = new HashMap<Integer,String>();
		
        System.out.println("HashMap  :" + hm);
        
      //Using entrySet() to get the set view
      Set<Entry<Integer,String>> set = hm.entrySet();
      		
      //print Set view
      System.out.println("Key-Value entries in map: :" + set);
		
		
	}

}

Output :

HashMap  :{}
Key-Value entries in map: :[]

In this tutorial we have learned How to java hashmap entryset() method return set of entries . We have seen different for entryset in map . You can learn more HashMap program for practice .

Leave a Reply

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

− 1 = 4