What Is Map Entry in Java

A map entry (key-value pair). The Map. entrySet method returns a collection-view of the map, whose elements are of this class. The only way to obtain a reference to a map entry is from the iterator of this collection-view.

What are map entries?

entries() The entries() method returns a new Iterator object that contains the [key, value] pairs for each element in the Map object in insertion order. In this particular case, this iterator object is also an iterable, so the for-of loop can be used.

How do you initialize a map entry in Java?

Starting from Java 9, there is a new utility method allowing to create an immutable entry which is Map#entry(Object, Object) . Here is a simple example: Entry<String, String> entry = Map. entry(“foo”, “bar”);

James H. Sterling

James H. Sterling

Environmental Science & Climate Journalist

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.

Share this article
Twitter Facebook Pinterest