JavaTuples setAtX() method

The setAtX() method in org.javatuples is used to change the value in the existing tuple, at the index X. Since JavaTuples are immutable, hence changing a value in the existing tuple results in a new tuple with the modified value at the index X. It returns the tuple class object of the called class with the changed value at index X.

Syntax:

Quartet<String, Integer, Double, String> quartet = ...
    ...
Quartet otherQuartet = quartet.setAtX(value);

Here X represents the index at which the value is to be changed.

Return Value: This method returns the tuple class object of the called class with the changed value at index X.

Note: This method do not exists with KeyValue Class, and LabelValue Class.

Below programs illustrate the various ways to use setAtX() methods:

Program 1: When the setAtX() method is used with any class from Unit to Decade, with a direct values as parameter:




// Below is a Java program to demonstrate
// use of setAtX() method
  
import java.util.*;
import org.javatuples.Pair;
  
class GfG {
    public static void main(String[] args)
    {
        // Creating a Pair with 2 values
        Pair<String, String> pair = Pair.with("w3wiki",
                                              "A computer science portal");
  
        // Using Pair() method to instantiate unit object
        Pair otherPair = pair.setAt1("by Sandeep Jain");
  
        // Printing the returned Pair
        System.out.println(otherPair);
    }
}


Output:

[w3wiki, by Sandeep Jain]

Program 2:




// Below is a Java program to demonstrate
// use of setAtX() method
  
import java.util.*;
import org.javatuples.Decade;
  
class GfG {
    public static void main(String[] args)
    {
        // Using with() method to instantiate Decade object
        Decade<Integer, Integer, Integer,
               Integer, Integer, Integer,
               Integer, Integer, Integer,
               Integer>
            decade
            = Decade.with(Integer.valueOf(1),
                          Integer.valueOf(2),
                          Integer.valueOf(3),
                          Integer.valueOf(4),
                          Integer.valueOf(5),
                          Integer.valueOf(6),
                          Integer.valueOf(7),
                          Integer.valueOf(8),
                          Integer.valueOf(9),
                          Integer.valueOf(10));
  
        // Using setAtX()
        Decade otherDecade = decade.setAt9(100);
  
        // Printing the formed Decade
        System.out.println(otherDecade);
    }
}


Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 100]

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