Explain Codes LogoExplain Codes Logo

What is the equivalent of the C++ Pair in Java?

java
pair-classes
custom-classes
best-practices
Nikita BarsukovbyNikita Barsukov·Nov 11, 2024
TLDR

In Java, an equivalent to C++'s Pair<L,R> can be found in the Map.Entry<K,V> interface, mainly used in the form of AbstractMap.SimpleEntry<K,V>:

Map.Entry<Integer, String> entry = new AbstractMap.SimpleEntry<>(1, "One");

For a standalone pair separate from a map context, there's javafx.util.Pair<L,R>:

Pair<Integer, String> pair = new Pair<>(1, "One");

Remember, javafx.util.Pair is part of JavaFX. Map.Entry is a built-in feature and usually preferred for non-GUI related tasks. Both can hold any two objects of your chosen types.

Java conveniently provides Map.Entry and javafx.util.Pair for bundling values. But experienced coders often prefer dedicated classes with meaningful names that clearly match the data they carry. These distinct classes communicate intent, and make for more organized, understandable code.

The why and how of creating specific classes

Instead of using a generic Pair, you can create a simple, dedicated class with fields that represent your data. This adds proper semantics and readability. Imagine a Coordinate class with fields for x and y, or a DateRange with startDate and endDate, which communicates far more information than a simple pair.

Ensuring long-term project health

Using dedicated classes also increases maintainability. You have the power to enforce methods such as hashCode(), equals(), and Comparable. Don't forget to implement getters and setters for controlled access to preserve data integrity.

Avoiding pair pitfalls

Pairs are tempting but can lead to confusion. They only hold two values together, and when the relationship between these values becomes complex, a dedicated class is more beneficial and promotes cleaner code.

When to pick a Pair

Instances where a generic Pair suffice do exist, such as when you need to return two unrelated values from a function. Here, Pair comes to the rescue and can prevent unnecessary object creation.

Java’s Map.Entry: Pair for the masses

If integrating with vanilla Java collections like HashMap, Map.Entry is a safe bet - it's natively supported and built-in. It also offers a toString() method, great for debugging with explicit string representations.

Dealing with nulls in your Pair class

If you need to use or create a Pair class, be sure to handle null values in methods like hashCode and equals. This ensures consistent behaviour which is essential if pairs are used as keys in a HashMap.

Custom class superiority: Why ditch standard Pairs?

Programming is often about choosing the right tool for the job. Pairs may be generic, but custom classes tell better stories. Let's explore why:

Names matter: Express data purpose

A Point2D or ScreenResolution class is instantly more insightful than a new Pair<>(1080, 1920) in your code.

Flexibility for the future

Custom classes can grow and evolve. Say you need to add a z to your Point2D, it becomes Point3D without breaking your codebase.

Integration and universality

AbstractMap.SimpleEntry and javafx.util.Pair do an excellent job in their respective libraries. But custom classes fit everywhere in Java code with no imports required.