A Quintet is a Tuple from JavaTuples library that deals with 3 elements. Since this Quintet is a generic class, it can hold any type of value in it.
Since Quintet 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 Quintet<A, B, C, D, E> extends Tuple implements IValue0<A>, IValue1<B;>, IValue2<C>, IValue3<D>, IValue4<E>
Class Hierarchy
Object
↳ org.javatuples.Tuple
↳ org.javatuples.Quintet<A, B, C, D, E>
Creating Quintet Tuple
- From Constructor:
Syntax:
Quintet<A, B, C, D, E> quintet = new Quintet<A, B, C, D, E> (value1, value2, value3, value4, value5);Example:
// Below is a Java program to create// a Quintet tuple from Constructorimportjava.util.*;importorg.javatuples.Quintet;classGfG {publicstaticvoidmain(String[] args){Quintet<Integer, String, String, Double, Boolean> quintet= Quintet.with(Integer.valueOf(1),"GeeksforGeeks","A computer portal",Double.valueOf(20.18),true);System.out.println(quintet);}}chevron_rightfilter_noneOutput:
[1, GeeksforGeeks, A computer portal, 20.18, true]
- Using with() method: The with() method is a function provided by the JavaTuples library, to instantiate the object with such values.
Syntax:
Quintet<type1, type2, type3, type4, type5> quintet = Quintet.with(value1, value2, value3, value4, value5);Example:
// Below is a Java program to create// a Quintet tuple from with() methodimportjava.util.*;importorg.javatuples.Quintet;classGfG {publicstaticvoidmain(String[] args){Quintet<Integer, String, String, Double, Boolean> quintet= Quintet.with(Integer.valueOf(1),"GeeksforGeeks","A computer portal",Double.valueOf(20.18),true);System.out.println(quintet);}}chevron_rightfilter_noneOutput:
[1, GeeksforGeeks, A computer portal, 20.18, true]
- 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:
Quintet<type1, type2, type3, type4, type5> quintet = Quintet.fromCollection(collectionWith_5_value); Quintet<type1, type2, type3, type4, type5> quintet = Quintet.fromArray(arrayWith_5_value);Example:
// Below is a Java program to create// a Quintet tuple from Collectionimportjava.util.*;importorg.javatuples.Quintet;classGfG {publicstaticvoidmain(String[] args){// Creating Quintet from ListList<Integer> list =newArrayList<Integer>();list.add(1);list.add(2);list.add(3);list.add(4);list.add(5);Quintet<Integer, Integer, Integer, Integer, Integer> quintet= Quintet.fromCollection(list);// Creating Quintet from ArrayInteger[] arr = {1,2,3,4,5};Quintet<Integer, Integer, Integer, Integer, Integer> otherQuintet= Quintet.fromArray(arr);System.out.println(quintet);System.out.println(otherQuintet);}}chevron_rightfilter_noneOutput:
[1, 2, 3, 4, 5] [1, 2, 3, 4, 5]
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:
Quintet<type1, type2, type3, type4, type5> quintet =
new Quintet<type1, type2, type3, type4, type5>
(value1, value2, value3, value4, value5);
type1 val1 = quintet.getValue0();
Example:
// Below is a Java program to get // a Quintet value import java.util.*; import org.javatuples.Quintet; class GfG { public static void main(String[] args) { Quintet<Integer, String, String, Double, Boolean> quintet = Quintet.with(Integer.valueOf(1), "GeeksforGeeks", "A computer portal", Double.valueOf(20.18), true); System.out.println(quintet.getValue0()); System.out.println(quintet.getValue2()); } } |
Output:
1 A computer portal
Setting Quintet 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:
Quintet<type1, type2, type3, type4, type5> quintet =
new Quintet<type1, type2, type3, type4, type5>
(value1, value2, value3, value4, value5);
Quintet<type1, type2, type3, type4, type5>
otherQuintet = quintet.setAtX(value);
Example:
// Below is a Java program to set // a Quintet value import java.util.*; import org.javatuples.Quintet; class GfG { public static void main(String[] args) { Quintet<Integer, String, String, Double, Boolean> quintet = Quintet.with(Integer.valueOf(1), "GeeksforGeeks", "A computer portal", Double.valueOf(20.18), true); Quintet<Integer, String, String, Double> otherQuintet = quintet.setAt3(2.018); System.out.println(otherQuintet); } } |
Output:
[1, GeeksforGeeks, A computer portal, 2.018, true]
Adding a value
Adding a value can be done with the help of addAtX() method, where X represents the index at which the value is to be added. This method returns a Tuple of element one more than the called Tuple.
Syntax:
Quintet<type1, type2, type3, type4, type5> quintet =
new Quintet<type1, type2, type3, type4, type5>
(value1, value2, value3, value4, value5);
Quintet<type 1, type 2, type 3, type 4, type 5> quintet =
quintet.addAtx(value);
Example:
// Below is a Java program to add // a value import java.util.*; import org.javatuples.Quintet; import org.javatuples.Sextet; class GfG { public static void main(String[] args) { Quintet<Integer, String, String, Double, Boolean> quintet = Quintet.with(Integer.valueOf(1), "GeeksforGeeks", "A computer portal", Double.valueOf(20.18), true); Quintet<Integer, String, String, Double, Boolean, Boolean> sextet = quintet.addAt5(false); System.out.println(sextet); } } |
Output:
[1, GeeksforGeeks, A computer portal, for geeks, 20.18, true, false]
Searching in Quintet
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:
Quintet<type1, type2, type3, type4, type5> quintet =
new Quintet<type1, type2, type3, type4, type5>
(value1, value2, value3, value4, value5);
boolean res = quintet.contains(value2);
Example:
// Below is a Java program to search // a value in a Quintet import java.util.*; import org.javatuples.Quintet; class GfG { public static void main(String[] args) { Quintet<Integer, String, String, Double, Boolean> quintet = Quintet.with(Integer.valueOf(1), "GeeksforGeeks", "A computer portal", Double.valueOf(20.18), true); boolean exist = quintet.contains(20.18); boolean exist1 = quintet.contains(false); System.out.println(exist); System.out.println(exist1); } } |
Output:
true false
Iterating through Quintet
Since Quintet implement the Iterable<Object> interface. It means that they can be iterated in the same way as collections or arrays.
Syntax:
Quintet<type1, type2, type3, type4, type5> quintet =
new Quintet<type1, type2, type3, type4, type5>
(value1, value2, value3, value4, value5);
for (Object item : quintet) {
...
}
Example:
// Below is a Java program to iterate // a Quintet import java.util.*; import org.javatuples.Quintet; class GfG { public static void main(String[] args) { Quintet<Integer, String, String, Double, Boolean> quintet = Quintet.with(Integer.valueOf(1), "GeeksforGeeks", "A computer portal", Double.valueOf(20.18), true); for (Object item : quintet) System.out.println(item); } } |
Output:
1 GeeksforGeeks A computer portal 20.18 true
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 Sextet Class from Quintet 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 Quartet Class with Triplet 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 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
- Sextet Class in JavaTuples
- Septet Class in JavaTuples
- Octet Class in JavaTuples
- Decade Class in JavaTuples
- KeyValue Class in JavaTuples
- LabelValue Class in JavaTuples
- Quartet 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.

