Open In App

JavaTuple getValueX() method

Improve
Improve
Like Article
Like
Save
Share
Report

The getValueX() method in org.javatuples is used to fetch the value of the TupleClassObject from the index X. This method can be used to any tuple class object of javatuples library. It returns the value at index X of the TupleClassObject. The returned Value is of the type of Xth element of the TupleClassObject. Hence using getValueX() retains the type-safety, unlike getValue(X).

The value of X lies from 0 to the range of the number of elements present in the TupleClassObject, minus 1. It means that for Unit class, the value of X available is 0. Similarly for Decade class, the value available for X are 0, 1, 2, .. to 9.

Method Declaration:

public A getValueX()

Syntax:

A val = TupleClassObject.getValueX()

Here:

  • TupleClassObject represents the JavaTuple Class object used like Unit, Quintet, Decade, etc.
  • A represents the type of the Xth element
  • X represents the index of the value to be fetched

Return Value: This method returns the value at index X of the TupleClassObject. The returned Value is of the type of Xth element of the TupleClassObject.

Below are programs that will illustrate the various ways to use getValueX() method:

Program 1: Using getValueX() with Unit class:




// Below is a Java program to use getValueX() method
  
import java.util.*;
import org.javatuples.Unit;
  
class GfG {
    public static void main(String[] args)
    {
        // Creating an Unit with one value
        Unit<String> unit = Unit.with("GeeksforGeeks");
  
        // Using getValueX() method for X=0
        // The type of val is String
        // which is the type of the 0th element in Unit
        String val = unit.getValue0();
  
        System.out.println("Value at 0 = " + val);
    }
}


Output:

Value at 0 = GeeksforGeeks

Program 2: Using getValueX() with Quartet class:




// Below is a Java program to use getValueX() method
  
import java.util.*;
import org.javatuples.Quartet;
  
class GfG {
    public static void main(String[] args)
    {
        // Creating a quartet
        Quartet<Integer, String, String, Double> quartet
            = Quartet.with(Integer.valueOf(1),
                           "GeeksforGeeks",
                           "A computer portal",
                           Double.valueOf(20.18));
  
        // Using getValueX() method for X=3
        // The type of val is Double which is the
        // type of the 3rd index value of Quartet
        Double val = quartet.getValue3();
  
        System.out.println("Value at 3 = " + val);
    }
}


Output:

Value at 3 = 20.18

Note: Similarly, it can be used with any other JavaTuple Class.



Last Updated : 27 Aug, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads