JavaTuples setAtX() method
Last Updated :
30 Aug, 2018
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:
import java.util.*;
import org.javatuples.Pair;
class GfG {
public static void main(String[] args)
{
Pair<String, String> pair = Pair.with( "GeeksforGeeks" ,
"A computer science portal" );
Pair otherPair = pair.setAt1( "by Sandeep Jain" );
System.out.println(otherPair);
}
}
|
Output:
[GeeksforGeeks, by Sandeep Jain]
Program 2:
import java.util.*;
import org.javatuples.Decade;
class GfG {
public static void main(String[] args)
{
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 ));
Decade otherDecade = decade.setAt9( 100 );
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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...