![]() |
VOOZH | about |
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:
public final class Triplet<A, B, C> extends Tuple implements IValue0<A>, IValue1<B>, IValue2<C>
Object ↳ org.javatuples.Tuple ↳ org.javatuples.Triplet<A, B, C>
Triplet<A, B, C> triplet = new Triplet<A, B, C>(value1, value2, value3);
Example:
Output:
[1, GeeksforGeeks, A computer portal]
Triplet<type1, type2, type3> triplet = Triplet.with(value1, value2, value3);
Example:
Output:
[1, GeeksforGeeks, A computer portal]
Triplet<type1, type2, type3> triplet = Triplet.fromCollection(collectionWith_2_value); Triplet<type1, type2, type3> triplet = Triplet.fromArray(arrayWith_2_value);
Example:
Output:
[GeeksforGeeks, A computer portal, for geeks] [GeeksforGeeks, A computer portal, for geeks]
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:
Output:
1 A computer portal
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:
Output:
[1, GeeksforGeeks, A computer portal for geeks]
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:
Output:
[1, GeeksforGeeks, A computer portal, for geeks]
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:
Output:
true false
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:
Output:
1 GeeksforGeeks A computer portal