
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Add a value in Ennead Tuple in Java
By default, Java does not have built-in tuple support; to use the tuple, we use the third-party library called Javatuples. Using this library, you can create a tuple of different sizes, starting from a single-element tuple, which is the Unit class, up to a ten-element tuple (Decade class).
The following is the list of JavaTuples library classes:
- Unit: 1 element tuple.
- Pair: 2 element tuple.
- Triplet: 3 element tuple
- Quartet: 4 element tuple
- Quintet: 5 element tuple
- Sextet: 6 element tuple
- Septet: 7 element tuple
- Octet: 8 element tuple
- Ennead: 9 element tuple
- Decade: 10 element tuple
Java Ennead Class
The Ennead class is one of the classes from the JavaTuple library. It is a fixed-size tuple that holds 9 values. When we add a value to an Ennead tuple, it becomes a Decade (a tuple that holds 10 values).
Constructor of Ennead tuple class
Below is the declaration of the Ennead tuple class:
Ennead(A value0, B value1, C value2, D value3, E value4, F value5, G value6, H value7, I value8)
Adding a Value to Ennead Class in Java
Here are the following different methods to add a value to the Ennead class:Let us understand the above methods in detail with the help of examples.
Using add() Method
The add() method of the Ennead class adds a single value at the end of the tuple. And this add() method has 2 variants:- add(X0 value)
- add(Unit tuple)
add(X0 value)
The add(X0 value) method of the Ennead class adds a raw value (string, integer, char, double, float, long, short, byte, boolean) at the end of the tuple.
Raw values: We can directly add these values to the tuple(without wrapping them in unit tuple, pair tuple, etc).
Below is an example to add the value in a tuple using the add(X0 value) method:
import org.javatuples.Ennead; import org.javatuples.Decade; public class TupleExample { public static void main(String[] args) { Ennead<String, String, String, String, String, String, String, String, String> ennead = new Ennead<>("One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"); Decade<String, String, String, String, String, String, String, String, String, String> decade = ennead.add("Ten"); System.out.println("Decade: " + decade); } }
The output of the above code is:
Decade: [One, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten]
Here we added "Ten" which is a string, and the string is a raw value we don't need to wrap, in a unit or pair tuple; that's why we are using the add(X0 value) method.
add(Unit tuple)
The add(unit tuple) method will add a unit tuple (object) at the end of the Ennead tuple as the 10th element. So the whole Unit object (which wraps a value) becomes the last element. This method is useful when we combine tuples instead of raw values(string, integer, char, double, float, long, short, byte, boolean).
Below is an example to add elements in a tuple using add(Unit tuple):
import org.javatuples.Ennead; import org.javatuples.Decade; import org.javatuples.Unit; public class TupleExample { public static void main(String[] args) { Ennead<String, String, String, String, String, String, String, String, String> ennead = new Ennead<>("One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"); Unit<Double> unit = Unit.with(3.14); Decade<String, String, String, String, String, String, String, String, String, Unit<Double>> decade = ennead.add(unit); System.out.println("Decade: " + decade); } }The output of the above code is:
Decade: [One, Two, Three, Four, Five, Six, Seven, Eight, Nine, [3.14]]
Using addAtX() Method
The addAtX() method adds a value at a specific position. X is just a number that tells the position where we want to insert the value in the tuple.The X starts from 0, and X can be any number from 0 to 8 (because Ennead has 9 elements).
-
addAt0(value) adds at the beginning.
-
addAt1(value) adds at index 1.
- ...
- addAt8(value) adds just before the last element.
Below is an example to add elements to a tuple using the addAtX():
import org.javatuples.Ennead; import org.javatuples.Decade; public class AddAtXExample { public static void main(String[] args) { Ennead<String, String, String, String, String, String, String, String, String> ennead = new Ennead<>("One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"); Decade<String, String, String, String, String, String, String, String, String, String> decade = ennead.addAt3("Inserted"); System.out.println("Decade: " + decade); } }
The output of the above code is:
Decade: [One, Two, Three, Inserted, Four, Five, Six, Seven, Eight, Nine]