What is the equivalent of the C++ Pair<L,R> in Java?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
C++ developers are used to std::pair as a lightweight container for two related values. Java does not provide one exact standard-library equivalent with the same everyday role. The best answer in Java depends on what the two values mean and whether you care more about readability, mutability, or convenience.
There Is No One Perfect Built-In Pair Type
Java's standard library does not include a general-purpose Pair class that mirrors C++ directly. Instead, Java code usually chooses among these options:
- '
Map.Entrywhen the two values are naturally a key and a value' - a custom class or record when the pair represents real domain data
- a third-party
Pairtype if the project already uses one
That design choice is very Java-like. The language tends to encourage named data rather than anonymous tuples.
Use Map.Entry for Key-Value Semantics
If the two values truly are a key and a value, Map.Entry is a reasonable standard-library substitute.
For a mutable version, use AbstractMap.SimpleEntry:
This works especially well in stream transformations or temporary plumbing code.
Records Are Usually Better for Domain Data
If the pair represents something meaningful, a named type is usually better than a generic pair. In modern Java, a record is often the cleanest answer.
Usage is straightforward:
This is often clearer than a generic pair because x and y say more than left and right or first and second.
If you are on an older Java version, the same idea can be expressed with a small class:
Third-Party Pair Types Exist
Some libraries, such as Apache Commons Lang, provide a Pair type. That can be fine when:
- the dependency already exists in the project
- the pair is only a temporary helper value
- a dedicated domain type would be unnecessary noise
But a library Pair should not become the default answer for everything. If the two values have important meaning, name them.
Compare the Trade-Offs
The real question is not "How do I mimic C++ exactly?" It is "What kind of two-value object makes this Java code clearest?"
A useful rule of thumb is:
- use
Map.Entryfor mapping semantics - use a record or class for business semantics
- use a library pair for short-lived infrastructure code
Here is a stream example where Map.Entry feels natural:
That is a strong use case because the pair is temporary and really does mean key plus value.
Common Pitfalls
- Using
Map.Entrywhen the data is not actually key-value data. - Creating a generic
Pairclass and then using it everywhere instead of naming important concepts. - Assuming Java cannot hold two values without importing a library.
- Ignoring mutability when choosing between
Map.entry,SimpleEntry, records, and custom classes. - Translating C++ style directly without considering what reads best in Java.
Summary
- Java has no single built-in equivalent to C++
std::pair. - Use
Map.Entrywhen the two values naturally mean key and value. - Prefer a record or small class when the pair represents a real concept.
- Library
Pairtypes are acceptable for short-lived helper values if the dependency already exists. - In modern Java, a named record is usually clearer than a generic pair class.
Related reading
- What is the equivalent of the C# 'var' keyword in Java?
- What is the equivalent to a JavaScript setInterval/setTimeout in Android/Java?
- What is the fastest way in Java to get the amount of factors a number has
- What is the 'instanceof' operator used for in Java?
- What is the fastest Dijkstra implementation you know in C?
- What is the fastest way to transpose a matrix in C?
- What is the Java ? operator called and what does it do?
- What is the Java equivalent for LINQ?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.