Pair Class in JavaTuples - GeeksforGeeks

05 Aug, 2021
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()
public final class Pair<A, B> extends Tuple 
           implements IValue0<A>, IValue1<B> 
Object
  ↳ org.javatuples.Tuple
      ↳ org.javatuples.Pair<A, B> 
From Constructor:Syntax: 
Pair<A, B> pair = new Pair<A, B>(value1, value2);
Example: 
Java
// Below is a Java program to create// a Pair tuple from Constructor import java.util.*;import org.javatuples.Pair; class GfG {    public static void main(String[] args)    {        Pair<Integer, String> pair            = new Pair<Integer, String>(Integer.valueOf(1), "GeeksforGeeks");         System.out.println(pair);    }}
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: 
Java
// Below is a Java program to create// a Pair tuple from with() method import java.util.*;import org.javatuples.Pair; class GfG {    public static void main(String[] args)    {        Pair<Integer, String> pair            = Pair.with(Integer.valueOf(1), "GeeksforGeeks");         System.out.println(pair);    }}
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: 
Java
// Below is a Java program to create// a Pair tuple from Collection import java.util.*;import org.javatuples.Pair; class GfG {    public static void main(String[] args)    {        // Creating Pair from List        List<String> list = new ArrayList<String>();        list.add("GeeksforGeeks");        list.add("A computer portal");        Pair<Strin, String> pair            = Pair.fromCollection(list);         // Creating Pair from Array        String[] arr = { "GeeksforGeeks", "A computer portal" };        Pair<String, String> otherPair            = Pair.fromArray(arr);         System.out.println(pair);        System.out.println(otherPair);    }}
Output: 
[GeeksforGeeks, A computer portal]
[GeeksforGeeks, A computer portal] 
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:  
Java
// Below is a Java program to get// a Pair value import java.util.*;import org.javatuples.Pair; class GfG {    public static void main(String[] args)    {        Pair<Integer, String> pair            = Pair.with(Integer.valueOf(1), "GeeksforGeeks");         System.out.println(pair.getValue0());    }}
Output: 
1 
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:  
Java
// Below is a Java program to set// a Pair value import java.util.*;import org.javatuples.Pair; class GfG {    public static void main(String[] args)    {        Pair<Integer, String> pair            = Pair.with(Integer.valueOf(1), "GeeksforGeeks");         Pair<Integer, String> otherPair            = pair.setAt1("A computer portal");         System.out.println(otherPair);    }}
Output: 
[1, A computer portal] 
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:  
Java
// Below is a Java program to add// a value import java.util.*;import org.javatuples.Pair;import org.javatuples.Triplet; class GfG {    public static void main(String[] args)    {        Pair<Integer, String> pair            = Pair.with(Integer.valueOf(1), "GeeksforGeeks");         Triplet<Integer, String, String> triplet            = pair.addAt2("A computer portal");         System.out.println(triplet);    }}
Output: 
[1, GeeksforGeeks, A computer portal] 
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:  
Java
// Below is a Java program to search// a value import java.util.*;import org.javatuples.Pair; class GfG {    public static void main(String[] args)    {        Pair<Integer, String> pair            = Pair.with(Integer.valueOf(1), "GeeksforGeeks");         boolean exist = pair.contains("GeeksforGeeks");        boolean exist1 = pair.contains(4);         System.out.println(exist);        System.out.println(exist1);    }}
Output: 
true
false 
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: 
Java
// Below is a Java program to iterate// a Pair import java.util.*;import org.javatuples.Pair; class GfG {    public static void main(String[] args)    {        Pair<Integer, String> pair            = Pair.with(Integer.valueOf(1), "GeeksforGeeks");         for (Object item : pair)            System.out.println(item);    }}
Output: 
1
GeeksforGeeks
 
kk9826225
JavaTuples
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Stream In Java
Object Oriented Programming (OOPs) Concept in Java
Reverse a string in Java
HashMap in Java with Examples
Arrays.sort() in Java with examples
Interfaces in Java
How to iterate any Map in Java