Friday, July 5, 2024
HomeLanguagesJavaJavaTuple getValueX() method

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:




// 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("Lazyroar");
  
        // 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 = Lazyroar

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),
                           "Lazyroar",
                           "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.

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments