JavaTuple getValueX() method

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

Value at 0 = GeeksforGeeks

Program 2: Using getValueX() with Quartet class:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

Value at 3 = 20.18

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



My Personal Notes arrow_drop_up

Technical Content Engineer at GeeksForGeeks

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.