JavaTuples - Triplet Class

The org.javatuples.Triplet class represents a Tuple with three elements.
Following is the declaration for org.javatuples.Triplet class −
public final class Triplet<A,B,C>
   extends Tuple
      implements IValue0<A>, IValue1<B>, IValue2<C>

Triplet(A value0, B value1, C value2)
This creates a Triplet Tuple.
Similarly setAt1() upto setAt2() set the value at index 1 and so on.
Quartet add(Unit tuple)
This method returns a Quartet tuple.
Similarly other methods to add tuples are available e.g. add(Pair tuple) returns Quintet and upto add(Septet tuple) returns Decade tuple.
Quartet add(X0 value)
This method add a value to the tuple and returns a Quartet tuple. 
Similarly other methods to add values are available e.g. add(X0 value0, X1 value1) returns Quintet and so on upto add() with seven parameters.
Quartet addAt0(Unit value)
This method add a Unit tuple at index 0 and returns a Quartet tuple.
Similarly other methods to add tuples are available e.g. addAt0(Pair value) returns Quintet and so on upto addAt0(Septet). Other similar method are addAt1(Unit value) which add a unit at index0 and have similar methods upto addAt2(Septet).
Quartet addAt0(X0 value)
This method add a value at index 0 and returns a Quartet tuple.
Similarly other methods to add values are available e.g. addAt0(X0 value0, X1 value1) returns Quintet and so on upto addAt0() with seven parameters. Other similar method are addAt1(X0 value) which add a value at index0 and have similar methods upto addAt2() with seven parameters.
static <X> Triplet<X,X,X> fromArray(X[] array)
Create tuple from array.
static <X> Triplet<X,X,X> fromCollection(Collection<X> collection)
Create tuple from collection.
static <X> Triplet<X,X,X> fromIterable(Iterable<X> iterable)
Create tuple from iterable.
static <X> Triplet<X,X,X> fromIterable(Iterable<X> iterable, int index)
Create tuple from iterable, starting from the specified index.
int getSize()
Return the size of the tuple.
A getValue0()
Returns the value of the tuple at index 0.
Similarly getValue1() upto getValue2() returns the value at index 1 and so on.
Pair<B,C> removeFrom0()
Return the tuple after removing value of the tuple at index 0.
Similarly removeFrom1() upto removeFrom2() returns the tuple after removing value of the tuple at index 1 and so on.
<X> Triplet<X,B,C> setAt0(X value)
Set the value of the tuple at index 0.
static <A> Triplet<A,B,C> with(A value0, B value1, C value2)
Create the tuple using given value.
This class inherits methods from the following classes −
org.javatuples.Tuple
org.javatuples.Tuple
Object
Object
Let's see Triplet Class in action. Here we'll see how to use various methods.
Create a java class file named TupleTester in C:\>JavaTuples.
File: TupleTester.java
package com.tutorialspoint;
import java.util.ArrayList;
import java.util.List;
import org.javatuples.Pair;
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);
      boolean isPresent = triplet.contains(5);
      System.out.println("5 is present: " + isPresent);
      List<Integer> list = new ArrayList<>();
      list.add(1);
      list.add(2);
      list.add(3);
      Quartet<Integer, Integer, Integer, String> quartet = triplet.add("Test");
      System.out.println(quartet);
      Integer value = triplet.getValue0();
      System.out.println(value);
      Pair<Integer, Integer> pair = triplet.removeFrom0();
      System.out.println(pair);
      Triplet<Integer, Integer, Integer> triplet1 = 
         Triplet.fromCollection(list);   
      System.out.println(triplet1);
   }
}
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

Verify the Output
[5, 6, 7]
5 is present: true
[5, 6, 7, Test]
5
[6, 7]
[1, 2, 3]

 Print
 Add Notes
 Bookmark this page