A Triplet is a Tuple from JavaTuples library that deals with 3 elements. Since this Triplet is a generic class, it can hold any type of value in it.
Since Triplet is a Tuple, hence it also has all the characteristics of JavaTuples:
- They are Typesafe
- They are Immutable
- They are Iterable
- They are Serializable
- They are Comparable (implements Comparable<Tuple>)
- They implement equals() and hashCode()
- They also implement toString()
Class Declaration
public final class Triplet<A, B, C> extends Tuple implements IValue0<A>, IValue1<B>, IValue2<C>
Class Hierarchy
Object ↳ org.javatuples.Tuple ↳ org.javatuples.Triplet<A, B, C>
Creating Triplet Tuple
- From Constructor:
Syntax:
Triplet<A, B, C> triplet = new Triplet<A, B, C>(value1, value2, value3);
Example:
Java
// Below is a Java program to create // a Triplet tuple from Constructor import java.util.*; import org.javatuples.Triplet; class GfG { public static void main(String[] args) { Triplet<Integer, String, String> triplet = new Triplet<Integer, String, String>(Integer.valueOf( 1 ), "Lazyroar" , "A computer portal" ); System.out.println(triplet); } } |
Output:
[1, Lazyroar, A computer portal]
- Using with() method: The with() method is a function provided by the JavaTuples library, to instantiate the object with such values.
Syntax:
Triplet<type1, type2, type3> triplet = Triplet.with(value1, value2, value3);
Example:
Java
// Below is a Java program to create // a Triplet tuple from with() method import java.util.*; import org.javatuples.Triplet; class GfG { public static void main(String[] args) { Triplet<Integer, String, String> triplet = Triplet.with(Integer.valueOf( 1 ), "Lazyroar" , "A computer portal" ); System.out.println(triplet); } } |
Output:
[1, Lazyroar, A computer portal]
- From other collections: The fromCollection() method is used to create a Tuple from a collection, and fromArray() method is used to create from an array. The collection/array must have the same type as of the Tuple and the number of values in the collection/array must match the Tuple class.
Syntax:
Triplet<type1, type2, type3> triplet = Triplet.fromCollection(collectionWith_2_value); Triplet<type1, type2, type3> triplet = Triplet.fromArray(arrayWith_2_value);
Example:
Java
// Below is a Java program to create // a Triplet tuple from Collection import java.util.*; import org.javatuples.Triplet; class GfG { public static void main(String[] args) { // Creating Triplet from List List<String> list = new ArrayList<String>(); list.add( "Lazyroar" ); list.add( "A computer portal" ); list.add( "for geeks" ); Triplet<String, String, String> triplet = Triplet.fromCollection(list); // Creating Triplet from Array String[] arr = { "Lazyroar" , "A computer portal" , "for geeks" }; Triplet<String, String, String> otherTriplet = Triplet.fromArray(arr); System.out.println(triplet); System.out.println(otherTriplet); } } |
Output:
[Lazyroar, A computer portal, for geeks] [Lazyroar, A computer portal, for geeks]
Getting Value
The getValueX() method can be used to fetch the value in a Tuple at index X. The indexing in Tuples start with 0. Hence the value at index X represents the value at position X+1.
Syntax:
Triplet<type1, type2, type3> triplet = new Triplet<type1, type2, type3>(value1, value2, value3); type1 val1 = triplet.getValue0();
Example:
Java
// Below is a Java program to get // a Triplet value import java.util.*; import org.javatuples.Triplet; class GfG { public static void main(String[] args) { Triplet<Integer, String, String> triplet = Triplet.with(Integer.valueOf( 1 ), "Lazyroar" , "A computer portal" ); System.out.println(triplet.getValue0()); System.out.println(triplet.getValue2()); } } |
Output:
1 A computer portal
Setting Triplet Value
Since the Tuples are immutable, it means that modifying a value at an index is not possible. Hence, JavaTuples offer setAtX(value) which creates a copy of the Tuple with a new value at index X, and returns that Tuple.
Syntax:
Triplet<type1, type2, type3> triplet = new Triplet<type1, type2, type3> (value1, value2, value3); Triplet<type1, type2, type3> otherTriplet = triplet.setAtX(value);
Example:
Java
// Below is a Java program to set // a Triplet value import java.util.*; import org.javatuples.Triplet; class GfG { public static void main(String[] args) { Triplet<Integer, String, String> triplet = Triplet.with(Integer.valueOf( 1 ), "Lazyroar" , "A computer portal" ); Triplet<Integer, String, String> otherTriplet = triplet.setAt1( "A computer portal for geeks" ); System.out.println(otherTriplet); } } |
Output:
[1, Lazyroar, A computer portal for geeks]
Adding a Value
Adding a value can be done with the help of addAtX() method, where X represent the index at which the value is to be added. This method returns a Tuple of element one more than the called Tuple.
Syntax:
Triplet<type1, type2, type3> triplet = new Triplet<type1, type2, type3>(value1, value2, value3); Quartet<type 1, type 2, type 3, type 4> quartet = triplet.addAt3(value);
Example:
Java
// Below is a Java program to add // a value import java.util.*; import org.javatuples.Triplet; import org.javatuples.Quartet; class GfG { public static void main(String[] args) { Triplet<Integer, String, String> triplet = Triplet.with(Integer.valueOf( 1 ), "Lazyroar" , "A computer portal" ); Quartet<Integer, String, String, String> quartet = triplet.addAt3( "for geeks" ); System.out.println(quartet); } } |
Output:
[1, Lazyroar, A computer portal, for geeks]
Searching in Triplet
An element can be searched in a tuple with the pre-defined method contains(). It returns a boolean value whether the value is present or not.
Syntax:
Triplet<type1, type2, type3> triplet = new Triplet<type1, type2, type3>(value1, value2, value3); boolean res = triplet.contains(value2);
Example:
Java
// Below is a Java program to search // a value in a Triplet import java.util.*; import org.javatuples.Triplet; class GfG { public static void main(String[] args) { Triplet<Integer, String, String> triplet = Triplet.with(Integer.valueOf( 1 ), "Lazyroar" , "A computer portal" ); boolean exist = triplet.contains( "Lazyroar" ); boolean exist1 = triplet.contains( 4 ); System.out.println(exist); System.out.println(exist1); } } |
Output:
true false
Iterating through Triplet
Since Triplet implement the Iterable<Object> interface. It means that they can be iterated in the same way as collections or arrays.
Syntax:
Triplet<type1, type2, type3> triplet = new Triplet<type1, type2, type3>(value1, value2, value3); for (Object item : triplet) { ... }
Example:
Java
// Below is a Java program to iterate // a Triplet import java.util.*; import org.javatuples.Triplet; class GfG { public static void main(String[] args) { Triplet<Integer, String, String> triplet = Triplet.with(Integer.valueOf( 1 ), "Lazyroar" , "A computer portal" ); for (Object item : triplet) System.out.println(item); } } |
Output:
1 Lazyroar A computer portal