Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

In this tutorial, we’ll discuss the three methods keySet(), entrySet() and values() of the Map interface in Java. These methods are used to retrieve a set of keys, a set of key-value mappings, and a collection of values, respectively.

2. Map Initialization

While we can use these methods on any class implementing the Map interface like HashMap, TreeMap, and LinkedHashMap, we’ll work with HashMap here.

Let’s create and initialize a HashMap whose key is of type String and value is of type Integer:

Map<String, Integer> map = new HashMap<>();
map.put("one", 1);
map.put("two", 2);

3. The keySet() Method

The keySet() method returns the Set of keys contained in the Map. 

Let’s apply the method keySet() to the Map and store it in a Set variable actualValues:

Set<String> actualValues = map.keySet();

Now, let’s confirm that the size of the returned Set is 2:

assertEquals(2, actualValues.size());

Further, we can see that the returned Set contains the keys of the Map:

assertTrue(actualValues.contains("one"));
assertTrue(actualValues.contains("two"));

4. The entrySet() Method

The entrySet() method returns the set of key-value mappings. The method doesn’t take any parameters and has a return type Set of Map.Entry. 

Let’s apply the method entrySet() to the Map:

Set<Map.Entry<String, Integer>> actualValues = map.entrySet();

As we can see, actualValues is a Set of Map.Entry objects.

Map.Entry is a static interface that holds both the key and the value. Internally, it has two implementations – AbstractMap.SimpleEntry and AbstractMap.SimpleImmutableEntry.

As before, let’s confirm that the size of the returned Set is 2:

assertEquals(2, actualValues.size());

Furthermore, we can see that the returned Set contains the key-value entries of the Map:

assertTrue(actualValues.contains(new SimpleEntry<>("one", 1)));
assertTrue(actualValues.contains(new SimpleEntry<>("two", 2)));

Here, we’ve chosen the AbstractMap.SimpleEntry implementation of the interface Map.Entry for our test.

5. The values() Method

The values() method returns the Collection of values contained in the Map. The method doesn’t take any parameters and has a return type Collection. 

Let’s apply the method values() to the Map and store it in a Collection variable actualValues:

Collection<Integer> actualValues = map.values();

Now, let’s verify the size of the returned Collection:

assertEquals(2, actualValues.size());

Further, we can see that the returned Set contains the values of the Map:

assertTrue(actualValues.contains(1));
assertTrue(actualValues.contains(2));

6. Conclusion

In this article, we’ve discussed the keySet(), entrySet(), and values() methods of the Map interface.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW