The all have getkey, getValue methods. Clearly Map.entry is used in a map, like map.entrySet, but internally, are they just the same thing, implemented the same way.
-
4What **`Pair`**? – Elliott Frisch Mar 21 '22 at 01:37
-
`Pair` is a class and `Map.Entry` is an interface. – kaya3 Mar 21 '22 at 01:44
-
1`Map.Entry` has a specific, clear context in which it is meant to be used, and clearer meanings for its parts. Pair has...basically none of that. – Louis Wasserman Mar 21 '22 at 03:08
-
I see people have said Map.Entry is an interface. That's not the question is really asking. I can ask what's the difference between AbstractMap.SimpleEntry, AbstractMap.SimpleImmutableEntry and pair, that would be too verbose. But please answer what's the difference btw those two entries and pair – wangge Mar 25 '22 at 23:24
2 Answers
They should be very similar but Pair
is defined in the javafx
library whereas Map.Entry
is defined in the java.util
library.
The basic gist of both objects are some like:
public class Pair<K, V> implements Serializable {
private K key;
public K getKey() { return key; }
private V value;
public V getValue() { return value; }
}
Map.Entry
is mainly used to store a collection of key-value pairs that can be iterated over whereas Pair
is mainly used for storing single key-value pairs.
Here is another SO post detailing further differences between Pair
and Map
implementations

- 1,817
- 3
- 14
- 25
-
3There is nothing "internal" in `Map.Entry` whatsoever. It is an inferface; it has no internals. – kaya3 Mar 21 '22 at 02:00
Interface versus class
One difference is that one is a class while the other is an interface.
javafx.util.Pair<K,V>
is a concrete class. To instantiate a Pair
, use new
. To access the class you’ll need to add the OpenJFX libraries to your project, or else use a JDK that comes bundled with the OpenJFX libraries such as ZuluFX or LibericaFX.
In contrast, java.util.Map.Entry<K,V>
is an interface. To instantiate a Map.Entry
, you’ll need to choose a class that implements that interface. Java comes with two such classes: AbstractMap.SimpleEntry
and AbstractMap.SimpleImmutableEntry
.
Alternatively, in Java 9+ you can call the static method Map.entry( key , value )
to get an unmodifiable Map.Entry
object. The concrete class is unspecified.
You asked:
but internally, are they just the same thing
An interface has no implementation, so no “internals” on Map.Entry
. As for the implementation details of the concrete classes, we should not really care. All we care about as calling programmers is the contract they promise to fulfill as specified in the JavaDoc.
If you are curious about the implementation details, both are open-source. So you can browse their implementation. Both are found on the OpenJDK project site. But remember that the implementors are free to change their respective implementations between versions, as long as the contract is respected.
Original intent
Pair
is meant to be used in JavaFX.
Entry
is meant to be used with Map
.
You can certainly use either class outside of those contexts. But such use can be confusing to other programmers reading your code. Consider adding a brief explanation in your code comments.
Record
Instead of using Pair
outside of JavaFX, or using Entry
outside of a Map
, in many cases it makes more sense to use the new records feature to make your own purpose-built class for pairing in one short simple line.
For example:
Record( Person person , Color favoriteColor ) {}
Records can even be defined locally (as can interfaces and enums in Java 16+).

- 303,325
- 100
- 852
- 1,154
-
For what it’s worth, there is now a [Map.entry(key, value)](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Map.html#entry(K,V)) method. – VGR Mar 21 '22 at 02:58
-