JavaTuples addAtX() method

The addAtX() method in org.javatuples is used to add a value to the existing tuple, at an index X. Since JavaTuples are immutable, hence adding a value to the existing tuple results in a new tuple with one more value. For example, adding a value to Unit tuple results in the formation of a Pair tuple. This method can be used for any tuple class object of the javatuples library, except the Decade class, as Decade is the highest class available in JavaTuples library. It returns the tuple class object of a class higher than the called class, decided by the number of values in the arguments.

Syntax:

Triplet<String, Integer, Double> triplet = ...
    ...
Quartet<String, Integer, Double, type(s)> quartet = triplet.addAtX(value(s));

Parameters: This method can take n values as parameters where:



  • X– represents the index at which the values are to be added.
  • n– represents the number of values based on the TupleClass (Unit, Pair, etc) to be created as return object.
  • type– represents the type for value(s) passed as arguments.
  • value– represents the value(s) passed as arguments.

Return Value: This method returns the object of TupleClass with combined values of the called tuple class and the values passed as the parameters. The values passed are added at index X in the called tuple class values.

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

Program 1: When the addAtX() method is used with any class from Unit to Ennead, 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 addAtX() method with
// direct value
  
import java.util.*;
import org.javatuples.Unit;
import org.javatuples.Pair;
  
class GfG {
    public static void main(String[] args)
    {
        // Using with() method to instantiate unit object
        Unit<String> unit = Unit.with("Geeks");
  
        // Using addAtX() to create Pair
        Pair<String, String> pair = unit.addAt0("forGeeks");
  
        System.out.println(pair);
    }
}

chevron_right


Output:

[forGeeks, Geeks]

Program 2: When the addAtX() method is used with any class from Unit to Ennead, with a another tuple class object as parameter:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Below is a Java program to demonstrate
// use of addAtX() method with
// multiple value
  
import java.util.*;
import org.javatuples.Unit;
import org.javatuples.Pair;
  
class GfG {
    public static void main(String[] args)
    {
        // Using with() method to instantiate unit object
        Unit<String> unit1 = Unit.with("Geeks");
  
        // Using with() method to instantiate unit object
        Unit<String> unit2 = Unit.with("forGeeks");
  
        // Using addAtX() to create Pair
        Pair<String, String> pair = unit1.addAt0(unit2);
  
        System.out.println(pair);
    }
}

chevron_right


Output:

[forGeeks, Geeks]

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