wangge
wangge

Reputation: 145

What's the difference between Map.entry and Pair in Java?

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.

Upvotes: 2

Views: 5069

Answers (2)

flyingfishcattle
flyingfishcattle

Reputation: 2153

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

Upvotes: 2

Basil Bourque
Basil Bourque

Reputation: 338684

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+).

Upvotes: 1

Related Questions