![]() |
VOOZH | about |
A Quartet is a Tuple from JavaTuples library that deals with 4 elements. Since this Quartet is a generic class, it can hold any type of value in it.
Since Quartet is a Tuple, hence it also has all the characteristics of JavaTuples:
public final class Quartet<A, B, C, D> extends Tuple implements IValue0<A>, IValue1<B>, IValue2<C>, IValue3<D>
Object ↳ org.javatuples.Tuple ↳ org.javatuples.Quartet<A, B, C, D>
Quartet<A, B, C, D> quartet = new Quartet<A, B, C, D> (value1, value2, value3, value4);
Output:
[1, GeeksforGeeks, A computer portal, 20.18]
Quartet<type1, type2, type3, type4> quartet = Quartet.with(value1, value2, value3, value4);
Output:
[1, GeeksforGeeks, A computer portal, 20.18]
Quartet<type1, type2, type3, type4> quartet = Quartet.fromCollection(collectionWith_2_value); Quartet<type1, type2, type3, type4> quartet = Quartet.fromArray(arrayWith_2_value);
Output:
[GeeksforGeeks, A computer portal, for geeks, by Sandeep Jain] [GeeksforGeeks, A computer portal, for geeks, by Sandeep Jain]
The getValueX() method can be used to fetch the value in a Tuple at index X. The indexing in Tuples starts with 0. Hence the value at index X represents the value at position X+1.
Syntax:
Quartet<type1, type2, type3, type4> quartet = new Quartet<type1, type2, type3, type4>(value1, value2, value3, value4); type1 val1 = quartet.getValue0();
Example:
Output:
1 A computer portal
Since the Tuples are immutable, it means that modifying a value at any 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:
Quartet<type1, type2, type3, type4> quartet = new Quartet<type1, type2, type3, type4> (value1, value2, value3, value4); Quartet<type1, type2, type3, type4> otherQuartet = quartet.setAtX(value);
Example:
Output:
[1, GeeksforGeeks, A computer portal, 2.018]
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:
Quartet<type1, type2, type3, type4> quartet = new Quartet<type1, type2, type3, type4> (value1, value2, value3, value4); Quintet<type 1, type 2, type 3, type 4, type 5> quintet = quartet.addAtx(value);
Example:
Output:
[1, GeeksforGeeks, A computer portal, for geeks, 20.18, true]
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:
Quartet<type1, type2, type3, type4> quartet = new Quartet<type1, type2, type3, type4>(value1, value2, value3, value4); boolean res = quartet.contains(value2);
Example:
Output:
true false
Since Quartet implement the Iterable<Object> interface. It means that they can be iterated in the same way as collections or arrays.
Syntax:
Quartet<type1, type2, type3, type4> quartet =
new Quartet<type1, type2, type3, type4>
(value1, value2, value3, value4);
for (Object item : quartet) {
...
}
Example:
Output:
1 GeeksforGeeks A computer portal 20.18