Core Java

Pairs In Java


A Pair is an abstract data type which stores two items together which may or may not be related. They come handy when we want to track two objects together. We often feel the need to work with Pairs, specifically in competitive programming problems. In this tutorial, we’ll cover ways to define or use Pairs in Java.

Using an Array:

A most common workaround is to have an array storing exactly two values. Such an array can act as a PairĀ with the first element at index zero and the second at index one:

private Person[] getStudentParentPair() {
    // ...
    return new Person[] {student, parent};

The above code is returning a Pair: <Student, Parent> where both student and parent are instances of Person class.


We can also choose to return an immutable Map using singletonMap() having exactly those two entries in it:

private Map<Person, Person> getStudentParentPair() { 
    Person parent = new Person(34, "James");
    Student student = new Student(1, "Sam");
    return Collections.singletonMap(student, parent);

Which in turn can be treated as a pair. Note that we have Student is a Person relationship in the above example.


SimpleEntry is a nested class in Java AbstractMap class which can be used to represent a Pair:

AbstractMap.SimpleEntry<Person, Person> entry 
  = new AbstractMap.SimpleEntry<>(new Student(1, "Sam")
      ,new Person(32, "James"));
Person student = entry.getKey();
Person parent = entry.getValue();

In the above example, Student is a subtype of our Person class. Note that the pair we have created here is mutable. If we wish to have an immutable pair implementation, we can use:

AbstractMap.SimpleImmutableEntry<Person, Person> entry
  = new AbstractMap.SimpleImmutableEntry<>(new Student(1, "Sam")
      , new Person(32, "James"));

Using a SimpleImmutableEntry class ensures that the contents of this pair can’t be changed. Any attempt to modify an immutable pair will lead to UnsupportedOperationException.


Java 8 core library has a minimal implementation of Pair functionality in javafx.util package. However, note that at this point in time this package doesn’t seem to be available by default in OpenJDK versions.

The usage is pretty easy:

Pair<Person, Person> pairOfStudentParent =
   new Pair<>(new Student("1", "Sam"), new Person("34", "James"));
Person key = pair.getKey();
Person value = pair.getValue();

The first element in the pair is retrieved using getKey() method and the other using getValue().

Custom Pair Implementation:

We can choose to write our own Pair implementation:

public class Pair {
    private Person student;
    private Person parent;

    //constructor, getters & setters

The idea is to create a wrapper class storing two objects of our desired type. Here, we have created a Pair of <Student, Parent> where the parent is the Student‘s parent object. The main advantage of this approach is the flexibility to name our attributes based on our problem domain.

Apache Commons:

The Apache Commons library provides an abstract Pair class in theĀ org.apache.commons.lang3.tuple package. The ImmutablePair and MutablePair classes subclass this abstract class to help us create a Pair:

Pair<Person, Person> immutablePair = 
  new ImmutablePair<>(new Student("1", "Sam"), new Person("34", "James"));

Person student = immutablePair.getKey();
Person parent = immutablePair.getValue();

Similarly, we can create a MutablePair:

Pair<Person, Person> mutablePair = 
  new MutablePair<>(new Student("1", "Sam"), new Person("34", "James")); 

Person student = mutablePair.getKey(); 
Person parent = mutablePair.getValue();


In this tutorial, we first looked at several alternatives we can use to work with Pairs in Java. Later, we also explored the Pair class available in javafx.util Java package followed by the one available in Apache Commons library.


Be the First to comment.

Leave a Comment

Your email address will not be published. Required fields are marked *