JavaTuples getValue() method

The getValue() method in org.javatuples is used to fetch the value of the TupleClassObject from the index passed as the parameter. This method can be used to any tuple class object of javatuples library. It returns a Object value which is the element present at the index, passed as parameter, of the TupleClassObject. Since the returned Value is of Object type, hence using getValue() loses the type-safety.

Method Declaration:

public final Object getValue(int pos)

Syntax:



Object val = TupleClassObject.getValue(int pos)

Here TupleClassObject represents the JavaTuple Class object used like Unit, Quintet, Decade, etc.

Parameters: This method takes pos as parameter where:

  • pos– is the index at which the value of the TupleClassObject is to be known.
  • TupleClassObject– represents the JavaTuple Class object used like Unit, Quintet, Decade, etc.

Return Value: This method returns a Object value which is the element present at the index, passed as parameter, of the TupleClassObject.

Below programs illustrate the various ways to use getValue() method:

Program 1: Using getValue() with Unit class:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Below is a Java program to use getValue() 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 getValue() method
        Object val = unit.getValue(0);
  
        System.out.println("Value at 0 = " + val);
    }
}

chevron_right


Output:

Value at 0 = GeeksforGeeks

Program 2: Using getValue() with Quartet class:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Below is a Java program to use getValue() method
  
import java.util.*;
import org.javatuples.Quartet;
  
class GfG {
    public static void main(String[] args)
    {
        Quartet<String, String, String, String> quartet
            = Quartet.with("GeeksforGeeks",
                           "A computer portal",
                           "for geeks",
                           "by Sandeep Jain");
  
        // Using getValue() method
        Object val = quartet.getValue(2);
  
        System.out.println("Value at 2 = " + val);
    }
}

chevron_right


Output:

Value at 2 = for geeks

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.