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 characterstics 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 Quartet<A, B, C, D> extends Tuple implements IValue0<A>, IValue1<B>, IValue2<C>, IValue3<D>
Class hierarchy
Object
↳ org.javatuples.Tuple
↳ org.javatuples.Quartet<A, B, C, D>
Creating Quartet Tuple
- From Constructor:
Syntax:
Quartet<A, B, C, D> quartet =
new Quartet<A, B, C, D>
(value1, value2, value3, value4);
- Example:
Java
// Below is a Java program to create// a Quartet tuple from Constructorimport java.util.*;import org.javatuples.Quartet;class GfG { public static void main(String[] args) { Quartet<Integer, String, String, Double> quartet = Quartet.with(Integer.valueOf(1), "GeeksforGeeks", "A computer portal", Double.valueOf(20.18)); System.out.println(quartet); }} |
- Output:
[1, GeeksforGeeks, A computer portal, 20.18]
- Using with() method: The with() method is a function provided by the JavaTuples library, to instantiate the object with such values.
Syntax:
Quartet<type1, type2, type3, type4> quartet =
Quartet.with(value1, value2, value3, value4);
- Example:
Java
// Below is a Java program to create// a Quartet tuple from with() methodimport java.util.*;import org.javatuples.Quartet;class GfG { public static void main(String[] args) { Quartet<Integer, String, String, Double> quartet = Quartet.with(Integer.valueOf(1), "GeeksforGeeks", "A computer portal", Double.valueOf(20.18)); System.out.println(quartet); }} |
- Output:
[1, GeeksforGeeks, A computer portal, 20.18]
- 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:
Quartet<type1, type2, type3, type4> quartet =
Quartet.fromCollection(collectionWith_2_value);
Quartet<type1, type2, type3, type4> quartet =
Quartet.fromArray(arrayWith_2_value);
- Example:
Java
// Below is a Java program to create// a Quartet tuple from Collectionimport java.util.*;import org.javatuples.Quartet;class GfG { public static void main(String[] args) { // Creating Quartet from List List<String> list = new ArrayList<String>(); list.add("GeeksforGeeks"); list.add("A computer portal"); list.add("for geeks"); list.add("by Sandeep Jain"); Quartet<Strin, String, String, String> quartet = Quartet.fromCollection(list); // Creating Quartet from Array String[] arr = { "GeeksforGeeks", "A computer portal", "for geeks", "by Sandeep Jain" }; Quartet<String, String, String, String> otherQuartet = Quartet.fromArray(arr); System.out.println(quartet); System.out.println(otherQuartet); }} |
- Output:
[GeeksforGeeks, A computer portal, for geeks, by Sandeep Jain] [GeeksforGeeks, A computer portal, for geeks, by Sandeep Jain]
Getting Value
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:
Java
// Below is a Java program to get// a Quartet valueimport java.util.*;import org.javatuples.Quartet;class GfG { public static void main(String[] args) { Quartet<Integer, String, String, Double> quartet = Quartet.with(Integer.valueOf(1), "GeeksforGeeks", "A computer portal", Double.valueOf(20.18)); System.out.println(quartet.getValue0()); System.out.println(quartet.getValue2()); }} |
Output:
1 A computer portal
Setting Quartet Value
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:
Java
// Below is a Java program to set// a Quartet valueimport java.util.*;import org.javatuples.Quartet;class GfG { public static void main(String[] args) { Quartet<Integer, String, String, Double> quartet = Quartet.with(Integer.valueOf(1), "GeeksforGeeks", "A computer portal", Double.valueOf(20.18)); Quartet<Integer, String, String, Double> otherQuartet = quartet.setAt3(2.018); System.out.println(otherQuartet); }} |
Output:
[1, GeeksforGeeks, A computer portal, 2.018]
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:
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:
Java
// Below is a Java program to add// a valueimport java.util.*;import org.javatuples.Quartet;import org.javatuples.Quintet;class GfG { public static void main(String[] args) { Quartet<Integer, String, String, Double> quartet = Quartet.with(Integer.valueOf(1), "GeeksforGeeks", "A computer portal", Double.valueOf(20.18)); Quintet<Integer, String, String, Double, Boolean> quintet = quartet.addAt4(true); System.out.println(quintet); }} |
Output:
[1, GeeksforGeeks, A computer portal, for geeks, 20.18, true]
Searching in Quartet
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:
Java
// Below is a Java program to search// a value in a Quartetimport java.util.*;import org.javatuples.Quartet;class GfG { public static void main(String[] args) { Quartet<Integer, String, String, Double> quartet = Quartet.with(Integer.valueOf(1), "GeeksforGeeks", "A computer portal", Double.valueOf(20.18)); boolean exist = quartet.contains(20.18); boolean exist1 = quartet.contains(4); System.out.println(exist); System.out.println(exist1); }} |
Output:
true false
Iterating through Quartet
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:
Java
// Below is a Java program to iterate// a Quartetimport java.util.*;import org.javatuples.Quartet;class GfG { public static void main(String[] args) { Quartet<Integer, String, String, Double> quartet = Quartet.with(Integer.valueOf(1), "GeeksforGeeks", "A computer portal", Double.valueOf(20.18)); for (Object item : quartet) System.out.println(item); }} |
Output:
1 GeeksforGeeks A computer portal 20.18
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Implement Quintet Class with Quartet Class in Java using JavaTuples
- Implement Quartet Class with Triplet Class in Java using JavaTuples
- Implement Pair Class with Unit Class in Java using JavaTuples
- Implement Triplet Class with Pair Class in Java using JavaTuples
- Implement Octet Class from Septet Class in Java using JavaTuples
- Implement Ennead Class from Octet Class in Java using JavaTuples
- Implement Sextet Class from Quintet Class in Java using JavaTuples
- Implement Septet Class from Sextet Class in Java using JavaTuples
- Implement Decade Class from Ennead Class in Java using JavaTuples
- Ennead Class in JavaTuples
- Unit Class in JavaTuples
- Pair Class in JavaTuples
- Triplet Class in JavaTuples
- Quintet Class in JavaTuples
- Sextet Class in JavaTuples
- Septet Class in JavaTuples
- Octet Class in JavaTuples
- Decade Class in JavaTuples
- KeyValue Class in JavaTuples
- LabelValue Class in JavaTuples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : saptarshi ghosh


