Table of contents
1.
Introduction
2.
Methods provided by javafx.util.Pair Class
2.1.
1. Constructor: Pair<K, V>
2.2.
2. getKey() & getValue()
2.3.
3. equals(Object obj)
2.4.
4. hashCode()
2.5.
5. toString()
3.
Accessing Values from a Pair
3.1.
1. getKey() – Gets the First Value
3.2.
2. getValue() – Gets the Second Value
3.3.
When would we use this?
4.
Alternative Implementations of Pair in Java
4.1.
1. Using AbstractMap.SimpleEntry (from java.util)
4.2.
2. Using a Custom Class
4.3.
Which one to use?
5.
Why Do We Need the Pair Class?
5.1.
Example Use Case
6.
Frequently Asked Questions
6.1.
Does Pair have more capability to store than two values?
6.2.
Is Pair thread safe?
6.3.
What are the times I should not use Pair?
7.
Conclusion
Last Updated: Jun 22, 2025
Easy

Pair Class in Java

Author Pallavi singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In Java, there are instances when we have to have two things in one. An instance is: the name of a student and their roll number or the name of a product and the price of it. So, instead of defining a separate class each time, Java offers a simple solution a so-called Pair class. We can use it so that we do not need to write any additional code to group two associated objects.The javafx.util.Pair class is a convenient possibility to work with such pairs. 

Pair Class in Java

Within this article we will speak about the ways given by this class, the accessing of the values, other methods of an implementation of pairs, and why we may use Pair class as a useful tool in programming.

Methods provided by javafx.util.Pair Class

JAVA The Java programming language uses the Pair class (in the javafx.util) as a basic method of storing two values in one. It also brings beneficial ways of using these pairs. We will look at them one by one.

1. Constructor: Pair<K, V>

To make a Pair, all you have to do is to feed it with two arguments, the first one is known as the key, and the second one is the value.


Example:

import javafx.util.Pair;  

public class Main {  
    public static void main(String[] args) {  
        // Creating a Pair of String & Integer  
        Pair<String, Integer> student = new Pair<>("Rahul", 25);  
        System.out.println("Pair: " + student);  
    }  
}  
You can also try this code with Online Java Compiler
Run Code


Output:

Pair: Rahul=25


Here, "Rahul" is the key, and 25 is the value.

2. getKey() & getValue()

These methods retrieve the first & second elements of the pair.

Example:

Pair<String, Double> product = new Pair<>("Laptop", 599.99);  
String name = product.getKey();  
double price = product.getValue();  
System.out.println("Product: " + name);  
System.out.println("Price: $" + price);  
You can also try this code with Online Java Compiler
Run Code


Output:

Product: Laptop  
Price: $599.99  

3. equals(Object obj)

Checks if two pairs are equal (both key & value must match).

Example:

Pair<Integer, String> pair1 = new Pair<>(1, "One");  
Pair<Integer, String> pair2 = new Pair<>(1, "One");  
Pair<Integer, String> pair3 = new Pair<>(2, "Two");  


System.out.println(pair1.equals(pair2)); // true  
System.out.println(pair1.equals(pair3)); // false  
You can also try this code with Online Java Compiler
Run Code

4. hashCode()

Returns a unique hash code for the pair (useful in HashMaps & HashSets).

Example:

System.out.println(pair1.hashCode());  
System.out.println(pair2.hashCode()); // Same as pair1  
System.out.println(pair3.hashCode()); // Different  
You can also try this code with Online Java Compiler
Run Code

5. toString()

Converts the pair into a readable string format (key=value).

Example:

System.out.println(pair1.toString()); // Output: 1=One 
You can also try this code with Online Java Compiler
Run Code

Accessing Values from a Pair

Once we create a Pair, we often need to retrieve the stored values. The Pair class provides two simple methods for this:

1. getKey() – Gets the First Value

This method returns the first element (key) of the pair.

2. getValue() – Gets the Second Value

This method returns the second element (value) of the pair.

Example

import javafx.util.Pair;  


public class Main {  
    public static void main(String[] args) {  
        // Creating a Pair of String (Name) & Integer (Age)  
        Pair<String, Integer> person = new Pair<>("Priya", 22);  


        // Accessing values  
        String name = person.getKey();  
        int age = person.getValue();  


        System.out.println("Name: " + name);  
        System.out.println("Age: " + age);  
    }  
}  
You can also try this code with Online Java Compiler
Run Code


Output:

Name: Priya  
Age: 22  

When would we use this?

  • If we need to process the key & value separately (e.g., printing, calculations).
     
  • When passing a pair to a method & extracting its values inside.

Alternative Implementations of Pair in Java

The javafx.util.Pair class is not the only way to store two values together. Let’s look at other options:

1. Using AbstractMap.SimpleEntry (from java.util)

This is another built-in way to store key-value pairs.

Example:

import java.util.AbstractMap;  
public class Main {  
    public static void main(String[] args) {  
        AbstractMap.SimpleEntry<String, Integer> student =  
            new AbstractMap.SimpleEntry<>("Rohit", 30);  


        System.out.println("Student: " + student.getKey() + ", Marks: " + student.getValue());  
    }  
}  
You can also try this code with Online Java Compiler
Run Code


Output:

Student: Rohit, Marks: 30  

2. Using a Custom Class

If we need more flexibility, we can create our own Pair class.

Example:

class CustomPair<K, V> {  
    private K key;  
    private V value;  


    public CustomPair(K key, V value) {  
        this.key = key;  
        this.value = value;  
    }  

    public K getKey() { return key; }  
    public V getValue() { return value; }  
}  


public class Main {  
    public static void main(String[] args) {  
        CustomPair<String, Double> product = new CustomPair<>("Mouse", 12.99);  
        System.out.println(product.getKey() + " costs $" + product.getValue());  
    }  
}  
You can also try this code with Online Java Compiler
Run Code


Output:

Mouse costs $12.99  

Which one to use?

  • javafx.util.Pair → Simple & quick for basic needs.
     
  • SimpleEntry → Useful if working with Java Collections.
     
  • Custom Class → Best when we need extra methods or modifications.

Why Do We Need the Pair Class?

There are some cases when we need to store temporarily two values, not belonging to another type but somehow connected. Where does Pair come in handy?

  • Avoids Extra Classes  - To put two values into a class, rather than creating a Student or Product class, we use Pair.
     
  • Eases Code- Can be used where a method requires that it returns two values (Java does not permit multiple returns).
     
  • Objects that handle Collections - Handy in List<Pair>, Map entries, etc.

Example Use Case

import javafx.util.Pair;  
import java.util.ArrayList;  
import java.util.List;  


public class Main {  
    public static void main(String[] args) {  
        List<Pair<String, Integer>> students = new ArrayList<>();  
        students.add(new Pair<>("Amit", 85));  
        students.add(new Pair<>("Neha", 92));  


        for (Pair<String, Integer> student : students) {  
            System.out.println(student.getKey() + " scored " + student.getValue());  
        }  
    }  
}  
You can also try this code with Online Java Compiler
Run Code


Output

Amit scored 85  
Neha scored 92 

Frequently Asked Questions

Does Pair have more capability to store than two values?

No, there are only two in Pair. To work with more values, make use of a custom class or Triple (provided by such libraries as Apache Commons).

Is Pair thread safe?

No. Use synchronization where you want several threads accessing it.

What are the times I should not use Pair?

Use when values have no association with each-other or when additional technique is required (use custom class instead).

Conclusion

Here, we have come to know that Pair class in Java offers an easy mechanism of storing two values in a single construct. We provided details of its operations such as getKey() and getValue() and juxtaposed it with an alternative such as SimpleEntry and discussed applicability. It is more useful when dealing with short term storage of similar information, but in a non trivial case, one should use custom classes instead of the collection class. When you require the rapid collection of two values with nil added cost, the Pair class assists you maintain your code clean.

Live masterclass