Java: How to Get Keys and Values from a Map

Java: How to Get Keys and Values from a Map


Key-value stores are essential and often used, especially in operations that require fast and frequent lookups. They allow an object - the key - to be mapped to another object, the value. This way, the values can easily be retrieved, by looking up the key.

In Java, the most popular Map implementation is the HashMap class. Aside from key-value mapping, it's used in code that requires frequest insertions, updates and lookups. The insert and lookup time is a constant O(1).

In this tutorial, we'll go over how to get the Keys and Values of a map in Java.

Get Keys and Values (Entries) from Java Map

Most of the time, you're storing key-value pairs because both pieces of info are important. Thus, in most cases, you'll want to get the key-value pair together.

The entrySet() method returns a set of Map.Entry<K, V> objects that reside in the map. You can easily iterate over this set to get the keys and their associated values from a map.

Let's populate a HashMap with some values:

Map<String, Integer> map =  new HashMap<>();
map.put("David", 24);
map.put("John", 35);
map.put("Jane", 19);
map.put("Billy", 21);

Now, let's iterate over this map, by going over each Map.Entry<K,V> in the entrySet(), and extracing the key and value from each of those entries:

for (Map.Entry<String, Integer> pair : map.entrySet()) {
    System.out.println(String.format("Key (name) is: %s, Value (age) is : %s", pair.getKey(), pair.getValue()));   

This results in:

Key (name) is: Billy, Value (age) is: 21
Key (name) is: David, Value (age) is: 24
Key (name) is: John, Value (age) is: 35
Key (name) is: Jane, Value (age) is: 19

Get Keys and Values (Entries) from Java Map with forEach()

Java 8 introduces us to the forEach() method for collections. It accepts a BiConsumer<? super K, ? super V> action. The forEach() method performs the given BiConsumer action on each entry of the HashMap.

Let's use the same map from before, but this time, add a year to each of the entries' ages:

map.forEach((key, value) -> System.out.println(key + " : " + value));

This prints out:

Billy : 21
David : 24
John : 35
Jane : 19

Or, instead of consuming each key and value from the map, you can use a regular Consumer and just consume entire entries from the entrySet():

    .forEach((entry) -> System.out.println(entry.getKey() + " : " + entry.getValue()));

This also results in:

Billy : 21
David : 24
John : 35
Jane : 19

Get Keys from a Java Map

To retrieve just keys, if you don't really need any information from the values, instead of the entry set, you can get the key set:

for(String key: map.keySet()){



Get Values from a Java Map

Similarly, one may desire to retrieve and iterate through the values only, without the keys. For this, we can use values():

for(String value: map.values()){



Check if Map Contains a Key

The HashMap class has a containsKey() method, which checks if the passed key exists in the HashMap, and returns a boolean value signifying the presence of the element or lack thereof.

Let's check if a key, 5 exists:

boolean result = map.containsKey(5);

This prints out:

Free eBook: Git Essentials

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!


And for an existing key:

boolean result = map.containsKey("John");

This prints out:



In this article, we've gone over a few ways to get keys and values (entries) of a Map in Java. We've covered using an iterator and going through each Map.Entry<K, V>, as well as using a forEach() method both on the map itself, as well as its entry set.

Finally, we've gone over how to get the key set and values separately, and check if a map contains a given key.

Last Updated: November 24th, 2020
Was this article helpful?

Improve your dev skills!

Get tutorials, guides, and dev jobs in your inbox.

No spam ever. Unsubscribe at any time. Read our Privacy Policy.

Want a remote job?

    Prepping for an interview?

    • Improve your skills by solving one coding problem every day
    • Get the solutions the next morning via email
    • Practice on actual problems asked by top companies, like:

    Make Clarity from Data - Quickly Learn Data Visualization with Python

    Learn the landscape of Data Visualization tools in Python - work with Seaborn, Plotly, and Bokeh, and excel in Matplotlib!

    From simple plot types to ridge plots, surface plots and spectrograms - understand your data and learn to draw conclusions from it.

    © 2013-2022 Stack Abuse. All rights reserved.