Implementing Triplet Using Pair Class

Implementing Triplet Using Pair Class

This topic is about Implementing Triplet Using Pair Class.

Problem Description

How to implement Triplet class using Pair class?

Example

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.Adglob;
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/Adglob/TupleTester.java

Now run the TupleTester to see the result −

C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.Adglob.TupleTester

Output

Verify the Output

[5, 6]
[5, 6, test]
[test, 5, 6]

In this topic we learned about Implementing Triplet Using Pair Class. To know more, Click Here.

This Post Has 2 Comments

Leave a Reply