Open In App

JavaTuples setAtX() method

Improve
Improve
Like Article
Like
Save
Share
Report

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("GeeksforGeeks",
                                              "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:

[GeeksforGeeks, 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.



Last Updated : 30 Aug, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads