Tutorialspoint.dev

Pair Class in JavaTuples

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 characterstics 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:

    // 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:

    // 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:

    // 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]
    

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:

// 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

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:

// 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

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:

// 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]

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:

// 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

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:

// 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




This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter