VOOZH about

URL: https://www.geeksforgeeks.org/java/quintet-class-in-java-tuples/

⇱ Quintet Class in JavaTuples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Quintet Class in JavaTuples

Last Updated : 4 Aug, 2021

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 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 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
  • Output: 
[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
  • Output: 
     
[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
  • Output: 
     
[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

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

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
 

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

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

Output: 

1
GeeksforGeeks
A computer portal
20.18
true


 

Comment
Article Tags:
Article Tags: