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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

[GeeksforGeeks, by Sandeep Jain]

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

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

Note: Similarly, it can be used with 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.