JavaTuples add() method
A value can be added to an existing tuple using the org.javatuples add() function. The addition of a value to an existing tuple creates a new tuple with one more value because JavaTuples are immutable. When a value is added to a Unit tuple, for instance, a Pair tuple gets formed. Although the Decade class is the highest class in the JavaTuples library, this method can be applied to any other tuple class object. Based on how many values are in the arguments, it returns the tuple class object of a class that is higher than the called class.
Syntax:
Triplett = ...... Quartet q = t.add(value(s));
Parameters: The parameters for the above process can take n values.
-where
n– indicates the number of values to be created as the return object based on the TupleClass (Unit, Pair, etc.).
type– indicates the type of value(s) that have been given as an argument.
value– the values that were given as an argument.
Return Value: This function returns the values specified as parameters and the combined values of the called tuple class as an object of TupleClass. The values of the called tuple class are appended after the given values.
The programs listed below show a variety of implementations for the add() functions.
Approach: add() method with Single value
When a single value is passed as a parameter to any class, from Unit to Ennead, using the add() method.
Implementation:
FileName: SingleParameter.java
import java.io.*; import java.util.*; import org.javatuples.Unit; import org.javatuples.Pair; public class SingleParameter { public static void main(String[] args) { // Instantiating a unit object using the with() function Unitu = Unit.with("Hello"); // To create a Pair, use add() Pair p = u.add("World"); System.out.println(p); } }
Output:
[Hello, World]
Approach: add() method with Multiple values
When any class, from Unit to Ennead, is used with the add() method and a multiple value as a parameter
Implementation:
FileName: AddMultipleValues.java
import java.io.*; import java.util.*; import org.javatuples.Ennead; import org.javatuples.Decade; public class AddMultipleValues { public static void main(String[] args) { // Instantiating an ennead object using the with() function Enneadenn = Ennead.with("Java", "C", "Python", "Json", "C++", "Ruby", "React", "NodeJS", "JavaScript"); // Creating a Decade with add() Decade String, String, String, String, String> dec = enn.add("Html"); System.out.println(dec); } }
Output:
[Java, C, Python, Json, C++, Ruby, React, NodeJS, JavaScript, Html]