How to implement Triplet class using Pair class?
Following example shows how to accomplish the above task. Each tuple has add() and addAtX() methods to convert the tuple.
Create a java class file named TupleTester in C:\>JavaTuples.
File: TupleTester.java
package com.howcodex; import org.javatuples.Triplet; import org.javatuples.Pair; public class TupleTester { public static void main(String args[]){ Pair<Integer, Integer> pair = Pair.with(5,6); System.out.println(pair); Triplet<Integer, Integer, String> triplet = pair.add("test"); Triplet<String, Integer, Integer> triplet1 = pair.addAt0("test"); System.out.println(triplet); System.out.println(triplet1); } }
Verify the result
Compile the classes using javac compiler as follows −
C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/howcodex/TupleTester.java
Now run the TupleTester to see the result −
C:\JavaTuples>java -cp .;javatuples-1.2.jar com.howcodex.TupleTester
Verify the Output
[5, 6] [5, 6, test] [test, 5, 6]