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.
Syntax:
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:
Java
// Below is a Java program to demonstrate
// use of add() method with
// single 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 add() to create Pair
Pair<String, String> pair = unit.add("forGeeks");
System.out.println(pair);
}
}
Output:
[Geeks, forGeeks]
Program 2: When the add() method is used with any class from Unit to Ennead, with a multiple value as parameter:
Java
// Below is a Java program to demonstrate
// use of add() method with
// multiple value
import java.util.*;
import org.javatuples.Ennead;
import org.javatuples.Decade;
class GfG {
public static void main(String[] args)
{
// Using with() method to instantiate ennead object
Ennead<String, String, String, String, String,
String, String, String, String>
ennead = Ennead.with("Geeks",
"for",
"Geeks",
"A",
"Computer",
"Science",
"Portal",
"for",
"Geeks");
// Using add() to create Decade
Decade<String, String, String, String, String,
String, String, String, String, String>
decade = ennead.add("RishabhPrabhu");
System.out.println(decade);
}
}
Output:
[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:
Java
// Below is a Java program to demonstrate
// use of add() method
import java.util.*;
import org.javatuples.Ennead;
import org.javatuples.Decade;
class GfG {
public static void main(String[] args)
{
// Using with() method to instantiate ennead object
Ennead<String, String, String, String, String,
String, String, String, String>
ennead = Ennead.with("Geeks",
"for",
"Geeks",
"A",
"Computer",
"Science",
"Portal",
"for",
"Geeks");
// Using add() to create Decade
Decade<String, String, String, String, String,
String, String, String, String, String>
decade = ennead.add("Rishabh", "Prabhu");
System.out.println(decade);
}
}
Output:
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.
Similar Reads
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
3 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
3 min read
JavaTuples setAtX() method
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 w
2 min read
JavaTuples setKey() method
The setKey() method in org.javatuples is used to set the key of the KeyValue Class object. This method can be used with only KeyValue class object of javatuples library. It returns another KeyValueClassObject with the Key as the element passed as the parameter, and the value from the previous KeyVal
2 min read
JavaTuple toList() method
The toList() method in org.javatuples is used to convert the values in TupleClass into a List. This List is of Object type, so that it can take all values. It is inherited from the JavaTuple class. This method can be used to any tuple class object of javatuples library. It returns List formed with t
2 min read
LinkedList add() Method in Java
In Java, the add() method of the LinkedList class is used to add an element to the list. By default, it adds the element to the end of the list, if the index is not specified.Example: Here, we use the add() method to add a single element to the list.Java// Java program to add elements in LinkedList
2 min read
JavaTuples toString() method
The toString() method in org.javatuples is used to convert the values in TupleClass into a String. This method is inherited from the JavaTuple class. This method can be used to any tuple class object of javatuples library. It returns a String value formed with the values in the TupleClassObject. Met
2 min read
JavaTuple toArray() method
The toArray() method in org.javatuples is used to convert the values in TupleClass into an array of Object type. This method is inherited from the JavaTuple class. This method can be used to any tuple class object of javatuples library. It returns an array of Object type formed with the values in th
2 min read
LinkedList addAll() Method in Java
In Java, the addAll() method of the LinkedList class is used to add all the elements of one collection to another. This method takes a Collection as an argument and adds all its elements to the end of the list. Example: Here, we use the addAll() method to add all the elements of one collection to an
3 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
2 min read