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:
import java.util.*;
import org.javatuples.Unit;
class GfG {
public static void main(String[] args)
{
Unit<String> unit = Unit.with( "GeeksforGeeks" );
String val = unit.getValue0();
System.out.println( "Value at 0 = " + val);
}
}
|
Output:
Value at 0 = GeeksforGeeks
Program 2: Using getValueX() with Quartet class:
import java.util.*;
import org.javatuples.Quartet;
class GfG {
public static void main(String[] args)
{
Quartet<Integer, String, String, Double> quartet
= Quartet.with(Integer.valueOf( 1 ),
"GeeksforGeeks" ,
"A computer portal" ,
Double.valueOf( 20.18 ));
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
Share your thoughts in the comments
Please Login to comment...