Get entryset from hashmap in java

We can use entrySet() method to get entryset from hashmap in java.

entrySet(): Returns a Set that contains the entries of this map. The set contains objects of type Map.Entry. It provides a set-view of this map.


public Set entrySet()


package com.w3spoint;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
public class Test {
  public static void main(String args[]){
	//Create HashMap object.
	HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
	//Add objects to the HashSet.
	hashMap.put(4, "Roxy");
	hashMap.put(2, "Sunil");
	hashMap.put(5, "Sandy");
	hashMap.put(1, "Munish");
	hashMap.put(3, "Pardeep");
	//Print the HashMap object.
	System.out.println("HashMap elements:");
	//Get entry set
	Set<Entry<Integer, String>> entires = hashMap.entrySet();
        for(Entry<Integer,String> entry:entires){
            System.out.println(entry.getKey()+" - "+entry.getValue());


HashMap elements:
{1=Munish, 2=Sunil, 3=Pardeep, 4=Roxy, 5=Sandy}
1 - Munish
2 - Sunil
3 - Pardeep
4 - Roxy
5 - Sandy
Please follow and like us:
Content Protection by