VOOZH about

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

⇱ Quartet Class in JavaTuples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Quartet Class in JavaTuples

Last Updated : 17 Feb, 2022

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: 

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

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:

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:

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

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

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:  

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:  

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:  

Output: 

1
GeeksforGeeks
A computer portal
20.18


 

Comment
Article Tags:
Article Tags: