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

🌐
Oracle
docs.oracle.com › javase › 8 › javafx › api › javafx › util › Pair.html
Pair (JavaFX 8)
July 21, 2026 - JavaFX on JDK 8 has a shorter support timeline than JDK 8. Update releases of JDK 8 after March 2028 will not include JavaFX. Visit www.oracle.com/javase/javafx for details. ... A convenience class to represent name-value pairs.
🌐
GeeksforGeeks
geeksforgeeks.org › java › pair-class-in-java
Pair Class in Java - GeeksforGeeks
July 23, 2025 - 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.
🌐
Reddit
reddit.com › r/learnjava › is there a pair class in java?
r/learnjava on Reddit: Is there a pair class in Java?
April 9, 2021 -

To avoid an XY problem here's what I'm doing:

I'm working on an assignment where I'm supposed to maintain a graph and an adjacency list and do Dijkstra's algorithm and stuff, and the way I'm going about it is to make a Vertex class that has fields such as the name of the vertex (since names/labels are relevant in the assignment) and also a field that is a list of adjacent vertices. However, I'm not sure how to deal with weighted edges. It's easy enough if say vertex A has a list that looks like [B, C, E] which indicates that A has an edge that connects to B, C, and E each, but there's no indication of what the weights of those edges are, which is essential for Dijsktra's.

I'd like the list of adjacent vertices to look more like [B:4, C:2, E:6] meaning that the weight from A to B is 4, the weight from A to C is 2, and the weight from A to E is 6. But I'm not actually sure how to do this?

Find elsewhere
🌐
CodeGym
codegym.cc › java blog › java objects › pairs in java
Pairs in Java
December 18, 2024 - The Pair in Java is a part of the javafx.util package and is used to represent a pair of values, where one value is associated with a key (key to value relationship). Although it is not a built-in class in the standard Java library, it provides ...
🌐
Apache Commons
commons.apache.org › proper › commons-math › commons-math4-legacy-core › jacoco › org.apache.commons.math4.legacy.core › Pair.java.html
Pair.java
* * @param <K> Key type. * @param <V> Value type. * * @since 3.0 */ public class Pair<K, V> { /** Key. */ private final K key; /** Value. */ private final V value; /** * Create an entry representing a mapping from the specified key to the * specified value. * * @param k Key (first element of the pair).
🌐
Scaler
scaler.com › home › topics › pair in java
Pair in Java | Pair Class in Java - Scaler Topics
July 14, 2022 - Java supports the Pair data structure using the Pair class available in javafx.util package post Java 8.
🌐
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.
🌐
Apache Commons
commons.apache.org › proper › commons-lang › apidocs › org › apache › commons › lang3 › tuple › Pair.html
Pair (Apache Commons Lang 3.21.0-SNAPSHOT API)
java.lang.Object · org.apache.commons.lang3.tuple.Pair<L, R> Type Parameters: L - the left element type. R - the right element type. All Implemented Interfaces: Serializable, Comparable<Pair<L, R>>, Map.Entry<L, R> Direct Known Subclasses: Diff, ImmutablePair, MutablePair · public abstract class Pair<L, R> extends Object implements Map.Entry<L, R>, Comparable<Pair<L, R>>, Serializable ·
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Map
Map - JavaScript | MDN
August 13, 2026 - Iteration happens in insertion order, which corresponds to the order in which each key-value pair was first inserted into the map by the set() method (that is, there wasn't a key with the same value already in the map when set() was called).
🌐
Oracle
docs.oracle.com › middleware › 11119 › jdev › api-reference-esdk › oracle › javatools › util › Pair.html
Pair (Oracle Fusion Middleware Java API Reference for Oracle Extension SDK Reference)
java.lang.Comparable<Pair<A,?>> Direct Known Subclasses: Pair · public class Pair<A,B> extends java.lang.Object · implements java.lang.Comparable<Pair<A,?>> Encapsulates a Pair of objects. Note: The toString() method returns the toString() of the first object.
🌐
CliffsNotes
cliffsnotes.com › home › computer science
Pair (java) - CliffsNotes
February 15, 2025 - Computer-science document from Purdue University, 1 page, public class Pair { / The first value of the pair. public int a; / The second value of the pair. public int b; /* * Constructor of the class. */ public Pair(int a, int b) { this.a = a; this.b = b; } public boolean equals(Pair o) { return a = o.a & b = o.b
🌐
GitHub
github.com › kijungs › mzoom › blob › master › src › main › java › mzoom › Pair.java
mzoom/src/main/java/mzoom/Pair.java at master · kijungs/mzoom
import java.io.Serializable; · /** * <p>A convenience class to represent name-value pairs.</p> * @since JavaFX 2.0 · */ public class Pair<K,V> implements Serializable { · /** * Key of this <code>util.Pair</code>. */ private K key; · /** * Gets the key for this pair.
Author: kijungs
🌐
Tutorial Horizon
tutorialhorizon.com › algorithms › java-pair-class
Java Pair Class
Class Pair<Key, Value> - A convenience class to represent name-value pairs. pair stores a key-pair value. Two Pair objects are considered equal if key and value of one pair is matching with second key. This class also generates hash code using key and value.
🌐
Java Lessons
javalessons.com › home › blog › five java pair class replacements you should know
Java Pair: A Comprehensive Overview
September 28, 2023 - This class excels at encapsulating a duo of objects within a single entity, granting developers the capacity to consolidate related data without having to explicitly define their precise interconnection. Java caters to the needs of developers with two distinct flavors of Pair implementations: Mutable and Immutable;
🌐
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 - We can now play around with this class and even create an array of pairs, similar to the vector<pair<int,int>> in C++. ... // Java program to create a pair class // and initialize an array of that // 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 array of pairs easily for (pair i : arr) { System.out.println(i); } // to extract particular values int value = arr[3].second; System.out.println("Required Value = " + value); } } // user defined pair class