Open In App

JavaTuples addAtX() method

Last Updated : 23 Aug, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

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:




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


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:




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


Output:

[forGeeks, Geeks]

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



Similar Reads

JavaTuples fromArray() method
The fromArray() method in org.javatuples is used to instantiate a tuple in a semantically elegant way, with the values of the array, given as parameters. This method can be used to any tuple class object of javatuples library. It is a static function in each javatuple class and it returns the tuple class object of the called class, with the values
2 min read
JavaTuples fromCollection() method
The fromCollection() method in org.javatuples is used to instantiate a tuple in a semantically elegant way, with the values of the collection, given as parameters. This method can be used to any tuple class object of javatuples library. It is a static function in each javatuple class and it returns the tuple class object of the called class, with t
2 min read
JavaTuples compareTo() method
The compareTo() method in org.javatuples is used to compare the order of a Tuple object with the object passed as the parameter. This method can be used for any tuple class object of javatuples library. It returns the difference in the ASCII value of the 1st different element present in the passed object, from the called object. Syntax: tupleObject
2 min read
JavaTuples with() method
The with() method in org.javatuples is used to instantiate a tuple in a semantically elegant way, with the values given as parameters. This method can be used for any tuple class object of the javatuples library. This method is a static function in each javatuple class and it returns the tuple class object of the called class, with the values initi
3 min read
JavaTuples add() method
The add() method in org.javatuples is used to add a value to the existing tuple. 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 javatupl
3 min read
JavaTuples equal() method
The equals() method in org.javatuples is used to check whether a TupleClass is equal to the TupleClass given as parameter. This method can be used to any tuple class object of javatuples library. It returns a boolean value that is true or false based on the equivalence of that TupleClass with existing TupleClass. Method Declaration: public final bo
2 min read
JavaTuples fromIterable() method
The fromIterable() method in org.javatuples is used to instance a tuple in a semantically elegant way, with the values of the iterable, given as parameters. This method can be used for any tuple class object of the javatuples library. It is a static function in each javatuple class and it returns the tuple class object of the called class, with the
2 min read
JavaTuples containsAll() method
The containsAll() method in org.javatuples is used to check whether a collection of values is present in the TupleClass, given as parameters. This method can be used for any tuple class object of the javatuples library. It returns a boolean value that is true or false based on the presence of that collection of values in the TupleClass. Method Decl
2 min read
JavaTuples getSize() method
The getSize() method in org.javatuples is used to fetch the size of the TupleClassObject. That is, it is used to fetch the number of values present in the TupleClassObject. This method can be used to any tuple class object of the javatuples library. It returns a integer value equal to the number of the elements present in the TupleClassObject. Meth
2 min read
JavaTuples getValue() method
The getValue() method in org.javatuples is used to fetch the value of the TupleClassObject from the index passed as the parameter. This method can be used to any tuple class object of javatuples library. It returns a Object value which is the element present at the index, passed as parameter, of the TupleClassObject. Since the returned Value is of
2 min read
Article Tags :
Practice Tags :