Java 16 includes Record Classes:

public record Pair<K, V>(K key, V value) 
{
    // intentionally empty
}

Creates a class with final fields, constructor, methods to retrieve key & value, and overrides of equals, hashCode and toString.

Usage example:

var one = new Pair<Integer, String>(1, "one");
var two = new Pair(2, "two");
System.out.println(one);   // Pair[key=1, value=one]
if (!one.equals(two))
    System.out.printf("not same as Pair for %d%n", two.key());

not sure what is best, if available (java 16) I would use that

JLS 8.10
JEP 395

🌐
Baeldung
baeldung.com › home › java › core java › using pairs in java
Using Pairs in Java | Baeldung
March 13, 2024 - For example, we could create a Pair with a key of 1 and a value of “One”. In this case, the key in the pair object can be retrieved by calling a getKey() method, while the value can be retrieved by calling getValue().
🌐
GeeksforGeeks
geeksforgeeks.org › java › pair-class-in-java
Pair Class in Java - GeeksforGeeks
July 23, 2025 - In C++, we have std::pair in the utility library which is of immense use if we want to keep a pair of values together. We were looking for an equivalent class for pair in Java but Pair class did not come into existence till Java 7. JavaFX 2.2 has the javafx.util.Pair class which can be used to store a pair.
Discussions

tuples - Best way to implement a Pair class in Java - Stack Overflow
I have written the following code for a Pair class in Java using Map.Entry. Does anyone have any recommendations for improving it? The most important criteria for me that the class needed to meet w... More on stackoverflow.com
🌐 stackoverflow.com
Is there a pair class in Java?
You could use a HashMap instead of an indexed list. More on reddit.com
🌐 r/learnjava
11
2
April 9, 2021
pairs in java
Just use HashMap More on reddit.com
🌐 r/learnprogramming
9
0
May 7, 2022
Why doesn't Java have tuples?

I personally prefer to read code where the class (and what it holds) has been written clearly and there is no ambiguation.

Return a user: Tuple<String,String,String,String> user = new Tuple<>(name, phone, address, nickname);

This is extremely, extremely ugly. A User class is much more appropriate and conveys the meaning of each param (what does getFirst mean with the tuple? What if i make the tuple new Tuple<>(phone, name....)? What does get(0) would mean also?).

C++11 has std::tuple now with variadic templates, and it proved useful every now and then, but one has to be very careful with it (well ... one has to be careful with c++ in general).

Given the maturity of the average Java programmer that i meet (IRL or interwebs), I would strongly oppose adding such a feature. It can be written (trivial), but it should not be provided out of the box (imho).

More on reddit.com
🌐 r/java
58
31
July 15, 2014
🌐
DataFlair
data-flair.training › blogs › pair-class-in-java
Explore Pair Class in Java With Examples - DataFlair
May 28, 2026 - If you want to perform some mathematical calculations on a number and then return the number along with the result, you would generally be able to do it with a pair class very easily. ... In Java, declaring Pair class objects are similar to normal object creation.
🌐
TechVidvan
techvidvan.com › tutorials › java-pair-class
Java Pair Class - Learn to Implement them in Java - TechVidvan
April 4, 2020 - We use Pairs or Pair class in Java to return two values from a method. For example, there is a method that returns both the cube root of the number and the number itself.
🌐
Medium
medium.com › @mandeepdhakal11 › using-pair-class-in-java-6f19aab092c3
Using Pair Class in Java. Introduction | by Mandeep Dhakal | Medium
May 19, 2024 - The Pair class, provided by the com.nimbusds.jose.util package, offers a simple way to handle pairs of objects. Different kind of data types can be used in a Pair as long as they are reference types (i.e., classes) and not primitive types. ... Example 1: In this example, a Pair object is created representing a Student’s name and age.
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java
Java Pair Class Example - Java Code Geeks
March 24, 2021 - JavaTuples is another simple and famous Java library that deals with tuples. It provides a set of tuple Java classes ranging from 1 to 10 elements. To serve our purpose you can use Pair<A, B>, class. The following examples show the usage of the Pair<A, B> class under JavaTuples Library.
Find elsewhere
🌐
Scaler
scaler.com › home › topics › pair in java
Pair in Java | Pair Class in Java - Scaler Topics
July 14, 2022 - The Pair class in Java is used for managing a pair of values which are somehow associated with each other, for example, a player and score, a student and marks, etc.
🌐
Techie Delight
techiedelight.com › home › java › implement pair class in java
Implement Pair class in Java | Techie Delight
July 7, 2026 - C++ doesn’t indicate what the data pair class stores. In Java, Map.Entry is an excellent example with a meaningful name representing a key-value pair.
🌐
Javatpoint
javatpoint.com › java-pair
Java Pair - Javatpoint
Java Program to swap two string variables without using third or temp variable.
🌐
GeeksforGeeks
geeksforgeeks.org › java › pair-class-in-java-tuples
Pair Class in JavaTuples - GeeksforGeeks
July 11, 2025 - // Below is a Java program to create ... class GfG { public static void main(String[] args) { Pair<Integer, String> pair = new Pair<Integer, String>(Integer.valueOf(1), "GeeksforGeeks"); System.out.println(pair); } }...
🌐
Tutorial Horizon
tutorialhorizon.com › algorithms › java-pair-class
Java Pair Class - Algorithms
--------toString function-------- ... 1 key: banana and its value: 2 key: pineapple and its value: 4 --------hashCode function-------- Pair: apple=1 and hashCode: 1209379731 Pair: apple=1 and hashCode: 1209379731 Pair: banana=2 and hashCode: -972748765 Pair: pineapple=4 and ...
🌐
TutorialsPoint
tutorialspoint.com › javatuples › javatuples_pair_class.htm
JavaTuples - Pair Class
package com.tutorialspoint; import ... import org.javatuples.Unit; public class TupleTester { public static void main(String args[]){ Pair<Integer, Integer> pair = Pair.with(5,6); System.out.println(pair); boolean isPresent = ...
🌐
Technogeeks CS
technogeekscs.com › home › blog › programming › pair in java
Pair Class in Java: A Powerful Data Structure | Technogeeks
March 5, 2026 - When sorting or manipulating data based on multiple attributes, Pairs can be valuable. For example, when sorting a list of objects based on two properties, such as name and age, a Pair can hold these attributes and facilitate sorting operations.
🌐
CodeGym
codegym.cc › java blog › java objects › pairs in java
Pairs in Java
December 18, 2024 - Pairs can be used to represent various data types such as coordinates, points, colors, and so on. They are also commonly used in C++ container libraries such as std::map and std::set. There is no built-in Pair class in the core Java Development Kit (JDK) library.
🌐
GeeksforGeeks
geeksforgeeks.org › java › creating-a-user-defined-printable-pair-class-in-java
Creating a User-Defined Printable Pair Class in Java - GeeksforGeeks
November 16, 2022 - // Java program to create a pair ... class object public class pair_example { public static void main(String[] args) { pair[] arr = new pair[5]; for (int i = 0; i < 5; i++) { arr[i] = new pair(i + 1, i + 2); } // printing an ...
🌐
Techkluster
techkluster.com › java › pairs-java
Working with Pairs in Java – TechKluster
By creating a custom Pair class, we have a flexible and reusable solution for working with pairs of values in Java. Instead of creating our own Pair class, we can leverage existing libraries that provide implementations for working with pairs. One popular library is Apache Commons Lang, which offers a Pair class in its org.apache.commons.lang3.tuple package. Here’s an example of using the Pair class from Apache Commons Lang:
🌐
How to do in Java
howtodoinjava.com › home › pairs in java
Java Pair class - key value pair data structure in Java - HowToDoInJava
October 1, 2022 - Tuple2<Integer, String> pair = new Tuple2<>(100, "howtodoinjava.com"); Integer key = pair._1(); //100 String value = pair._2(); //howtodoinjava.com pair.equals(new Tuple2<>(100, "howtodoinjava.com")); //true - same name and value pair.equals(new ...