Java – How to get all Entries or Key-Value pairs of HashMap ?

In this article, we will discuss how to get all entries or Key-Value pairs of a HashMap or implementation classes of Map interface like LinkedHashMap or TreeMap

Map implementation classes:

  • HashMap –> retrieves entries or Key-Value pairs, in random-order
  • LinkedHashMap –> retrieves entries or Key-Value pairs, as per insertion-order
  • TreeMap –> retrieves entries or Key-Value pairs, as per some sorting-order

1. To get all entries of HashMap :

  • use entrySet() method of Map interface
  • which returns Set of Entries

Syntax:

Set<Entry<String, String>> setOfEntries = hashMap.entrySet();

GetAllEntriesOfHashMap.java

package in.bench.resources.java.map;

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

public class GetAllEntriesOfHashMap {

	public static void main(String[] args) {

		// creating HashMap object of type <String, String>
		HashMap<String, String> hm = new HashMap<String, String>(); 

		// adding key-value pairs to HashMap object
		hm.put("Google", "Sundar Pichai");
		hm.put("Facebook", "Mark Zuckerberg");
		hm.put("LinkedIn", "Reid Hoffman");
		hm.put("Apple", "Steve Jobs");
		hm.put("Microsoft", "Bill Gates");

		System.out.println("1. all Key-Value pairs:\n\n" + hm + "\n\n");

		System.out.println("2. Set of all Entries: \n");

		// get Entry or Key-Value pairs using entrySet()
		Set<Entry<String, String>> setOfEntries = hm.entrySet();

		// get iterator for Set of Entries
		Iterator<Entry<String, String>> itr = setOfEntries.iterator();
		while(itr.hasNext()) {
			System.out.println(itr.next());
		}

		System.out.println("\n\n3. Iterating Entries using for-each\n");
		for(Entry<String, String> entry : setOfEntries){
			System.out.println("Key : "  + entry.getKey() + "\t\t" +
					"Value : "  + entry.getValue());
		}
	}
}

Output:

1. all Key-Value pairs:

{Facebook=Mark Zuckerberg, Microsoft=Bill Gates, Apple=Steve Jobs,
LinkedIn=Reid Hoffman, Google=Sundar Pichai}

2. Set of all Entries: 

Facebook=Mark Zuckerberg
Microsoft=Bill Gates
Apple=Steve Jobs
LinkedIn=Reid Hoffman
Google=Sundar Pichai

3. Iterating Entries using for-each loop

Key : Facebook		Value : Mark Zuckerberg
Key : Microsoft		Value : Bill Gates
Key : Apple		Value : Steve Jobs
Key : LinkedIn		Value : Reid Hoffman
Key : Google		Value : Sundar Pichai

Alternative Way :

2. Get all keys using keySet() method and retrieve corresponding values

GetAllKeysOfHashMap.java

package in.bench.resources.java.map;

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

public class GetAllKeysOfHashMap {

	public static void main(String[] args) {

		// creating HashMap object of type <String, String>
		HashMap<String, String> hm = new HashMap<String, String>(); 

		// adding key-value pairs to HashMap object
		hm.put("Google", "Sundar Pichai");
		hm.put("Facebook", "Mark Zuckerberg");
		hm.put("LinkedIn", "Reid Hoffman");
		hm.put("Apple", "Steve Jobs");
		hm.put("Microsoft", "Bill Gates");

		System.out.println("all Key-Value pairs:\n\n" + hm + "\n\n");

		System.out.println("List of all keys using keySet(): \n");

		// Iterating keys using keySet()
		Set<String> companies = hm.keySet();
		for(String company : companies) {
			System.out.println("Key : "  + company + "\t\t" +
					"Value : "  + hm.get(company));
		}
	}
}

Output:

all Key-Value pairs:

{LinkedIn=Reid Hoffman, Facebook=Mark Zuckerberg, Google=Sundar Pichai,
Apple=Steve Jobs, Microsoft=Bill Gates}

List of all keys using keySet(): 

Key : LinkedIn		Value : Reid Hoffman
Key : Facebook		Value : Mark Zuckerberg
Key : Google		Value : Sundar Pichai
Key : Apple		Value : Steve Jobs
Key : Microsoft		Value : Bill Gates

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java - How to get size or length of HashMap ?
Java - How to get all values of a HashMap ?