Implementing Quartet Using Triplet Class


Advertisements

Problem Description

How to implement Quartet class using Triplet 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.tutorialspoint;
import org.javatuples.Quartet;
import org.javatuples.Triplet;
public class TupleTester {
   public static void main(String args[]){
      Triplet<Integer, Integer, Integer> triplet = Triplet.with(5,6,7);
      System.out.println(triplet);
      Quartet<Integer, Integer, Integer, String> quartet = triplet.add("test");
      Quartet<String, Integer, Integer, Integer> quartet1 = triplet.addAt0("test");
      System.out.println(quartet);
      System.out.println(quartet1);
   }
}

Verify the result

Compile the classes using javac compiler as follows −

C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java

Now run the TupleTester to see the result −

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

Output

Verify the Output

[5, 6, 7]
[5, 6, 7, test]
[test, 5, 6, 7]
Advertisements