
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 to Pair 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 Pair Class
The Pair class is one of the classes from the JavaTuple library. It is a fixed-size tuple that holds 2 values. When we add a value to a Pair tuple, it becomes a Triplet (a tuple that holds 3 values).
Constructor of Pair Tuple Class
Below is the declaration of the pair tuple class:
Pair(A value0, B value1)
Adding a Value to Pair Tuple in Java
Here are the following different methods to add a value to the pair tuple:
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)
The add(X0 value) Method
The add(X0 value) method of the pair 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).
Following is an example to add the value in a tuple using the add(X0 value) method:
import org.javatuples.Pair; import org.javatuples.Triplet; public class PairAddExample { public static void main(String[] args) { Pair<String, Integer> pair = Pair.with("Hello", 100); Triplet<String, Integer, Character> triplet = pair.add('A'); System.out.println("Triplet: " + triplet); } }
The output of the above code is:
Triplet: [Hello, 100, A]
The add(Unit tuple)
The add(unit tuple) method will add a unit tuple (object) at the end of the pair tuple as the 3rd 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.Pair; import org.javatuples.Unit; import org.javatuples.Triplet; public class PairAddUnitExample { public static void main(String[] args) { Pair<String, Integer> pair = Pair.with("Hello", 100); Unit<Character> unit = Unit.with('A'); Triplet<String, Integer, Unit<Character>> triplet = pair.add(unit); System.out.println("Triplet: " + triplet); } }
The output of the above code is:
Triplet: [Hello, 100, [A]]
The 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; X starts from 0.
-
addAt0(value) adds at the beginning.
- addAt1(value) adds at index 1
Following is an example to add elements to a tuple using the addAtX():
import org.javatuples.Pair; import org.javatuples.Triplet; public class Demo { public static void main(String[] args) { Pair < String, String > p = Pair.with("Mobile", "Desktop"); System.out.println("Initial = " + p); Triplet < String, String, String > res = p.addAt0("Tablet"); System.out.println("Result = " + res); } }
The output of the above code is:
Initial = [Mobile, Desktop] Result = [Tablet, Mobile, Desktop]