VOOZH about

URL: https://www.geeksforgeeks.org/java/pair-class-in-java-tuples/

⇱ Pair Class in JavaTuples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Pair Class in JavaTuples

Last Updated : 11 Jul, 2025

A Pair is a Tuple from JavaTuples library that deals with 2 elements. Since this Pair is a generic class, it can hold any type of value in it.
Since Pair is a Tuple, hence it also has all the characteristics of JavaTuples: 

  • They are Typesafe
  • They are Immutable
  • They are Iterable
  • They are Serializable
  • They are Comparable (implements Comparable<Tuple>)
  • They implement equals() and hashCode()
  • They also implement toString()

Class Declaration 

public final class Pair<A, B> extends Tuple 
 implements IValue0<A>, IValue1<B> 

Class hierarchy 

Object
 ↳ org.javatuples.Tuple
 ↳ org.javatuples.Pair<A, B> 

Creating Pair Tuple 

  • From Constructor:
    Syntax
Pair<A, B> pair = new Pair<A, B>(value1, value2);

Example

Output: 

[1, GeeksforGeeks]
  • Using with() method: The with() method is a function provided by the JavaTuples library, to instantiate the object with such values.
    Syntax
Pair<type1, type2> pair = Pair.with(value1, value2);

Example

Output: 

[1, GeeksforGeeks]
  • From other collections: The fromCollection() method is used to create a Tuple from a collection, and fromArray() method is used to create from an array. The collection/array must have the same type as of the Tuple and the number of values in the collection/array must match the Tuple class.
    Syntax
Pair<type1, type2> pair = Pair.fromCollection(collectionWith_2_value);
Pair<type1, type2> pair = Pair.fromArray(arrayWith_2_value);

Example

Output: 

[GeeksforGeeks, A computer portal]
[GeeksforGeeks, A computer portal] 

Getting Value

The getValueX() method can be used to fetch the value in a Tuple at index X. The indexing in Tuples start with 0. Hence the value at index X represents the value at position X+1.

Syntax:  

Pair<type1, type2> pair = 
 new Pair<type1, type2>(value1, value2);

type1 val1 = pair.getValue0();


Example:  

Output: 

1 

Setting Pair Value

Since the Tuples are immutable, it means that modifying a value at an index is not possible. Hence, JavaTuples offer setAtX(value) which creates a copy of the Tuple with a new value at index X, and returns that Tuple.

Syntax:  

Pair<type1, type2> pair = 
 new Pair<type1, type2>(value1, value2);

type1 val1 = pair.getValue0();

Example:  

Output: 

[1, A computer portal] 

Adding a Value

Adding a value can be done with the help of addAtX() method, where X represents the index at which the value is to be added. This method returns a Tuple of element one more than the called Tuple.

Syntax:  

Pair<type1, type2> pair = 
 new Pair<type1, type2>(value1, value2);

Triplet<type 1, type 2, type 3> pair = 
 pair.addAt2(value 2);

Example:  

Output: 

[1, GeeksforGeeks, A computer portal] 

Searching in Pair

An element can be searched in a tuple with the pre-defined method contains(). It returns a boolean value whether the value is present or not.

Syntax:  

Pair<type1, type2> pair = 
 new Pair<type1, type2>(value1, value2);

boolean res = pair.contains(value2);

Example:  

Output: 

true
false 

Iterating through Pair

Since Pair implement the Iterable<Object> interface. It means that they can be iterated in the same way as collections or arrays.

Syntax:  

Pair<type1, type2> pair = 
 new Pair<type1, type2>(value1, value2);

for (Object item : pair) {
 ...
}


Example

Output: 

1
GeeksforGeeks


 

Comment
Article Tags:
Article Tags: