Implement Triplet Class with Pair Class in Java using JavaTuples
A pair class in Java can be converted into a triplet class by using the JavaTuples library. If a pair consists of two elements, a triplet consists of three elements. A triplet can effectively store three different components: two can be stored together, and the third can be stored separately, provided a pair of nests inside of it. This method allows for the easy handling of multiples with varying numbers of elements.
We can combine Triplet Class and Pair Class using the following ways.
- By using direct values
- By using Pair.add() method
- By using the Pair.addAtX() method
Approach: By using direct values
It creates a Pair object with a String and an Integer as its members. After that, the Pair is utilized to generate a Triplet, of which the following two elements are taken out of the Pair, and the first element is a String ("Triplet 1"). Ultimately, it prints the Triplet and the Pair to show what is inside.
Implementation:
FileName: TripletClassDirectValues.java
import java.util.*; import org.javatuples.*; public class TripletClassDirectValues { public static void main(String[] args) { //Creating the Pair Pairpair = new Pair ( Integer.valueOf(1), "HelloWorld"); // displaying the created Pair System.out.println("The Pair is: " + pair); // Creating a triplet from the pair Triplet triplet = new Triplet ( "Triplet 1", pair.getValue0(), pair.getValue1()); // displaying the triplet System.out.println("The Triplet is: " + triplet); } }
Output:
The Pair is: [1, HelloWorld] The Triplet is: [Triplet 1, 1, HelloWorld]
Approach: By using the Pair.add() method
The Pair and Triplet classes from the javatuples package are used in this JavaTuple function. The first step is to create a Pair using the string "HelloWorld" and the integer 1. It then goes on to show how to add the string "Triplet 1" to change this Pair into a Triplet. In order to show the transition, it prints the Pair and Triplet to the console at the end. All in all, it also offers a basic illustration of Java tuple manipulation.
Implementation:
FileName: TripletClassPairAdd.java
import java.util.*; import java.io.*; import org.javatuples.*; public class TripletClassPairAdd { public static void main(String[] args) { //Creating the Pair Pairp = new Pair ( Integer.valueOf(1), "HelloWorld"); // displaying the Pair System.out.println("The Pair is: " + p); // Using add() method for creating the Triplet Triplet t = p.add("Triplet 1"); // displaying the Triplet System.out.println("The Triplet is: " + t); } }
Output:
The Pair is: [1, HelloWorld] The Triplet is: [1, HelloWorld, Triplet 1]
Approach: By using the Pair.addAtX() method
The Pair and Triplet classes from the JavaTuples package are used as examples in the addAtX() method. The first step is to create a Pair using the string "HelloWorld" and the integer 1. The Pair is then changed into a Triplet
Implementation:
FileName: TripletClassAddAtX.java
import java.util.*; import java.io.*; import org.javatuples.*; public class TripletClassAddAtX { public static void main(String[] args) { //Creating the Pair Pairp = new Pair ( Integer.valueOf(1), "HelloWorld"); // displaying the Pair System.out.println("The Pair is: " + p); // Using addAtx() method for creating the Triplet Triplet t = p.addAt0("Triplet 1"); // displaying the Triplet System.out.println("Triplet: " + t); } }
Output:
The Pair is: [1, HelloWorld]
The Triplet is: [Triplet 1, 1, HelloWorld]