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 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.
Triplet<String, Integer, Double> triplet = ... ... Quartet<String, Integer, Double, type(s)> quartet = triplet.add(value(s));
Parameters: This method can take n values as parameters where:
- 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 after the called tuple class values.
Below programs illustrate the various ways to use add() methods:
Program 1: When the add() method is used with any class from Unit to Ennead, with a single value as parameter:
Program 2: When the add() method is used with any class from Unit to Ennead, with a multiple value as parameter:
[Geeks, for, Geeks, A, Computer, Science, Portal, for, Geeks, RishabhPrabhu]
Program 3: When the add() method is used with any class from Unit to Ennead, with total values contribute to be more than 10, it shows Runtime Exception:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: org.javatuples.Ennead.add
Note: Similarly, it can be used with any other JavaTuple Class.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.