VOOZH about

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

⇱ Sextet Class in JavaTuples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Sextet Class in JavaTuples

Last Updated : 11 Aug, 2021

A Sextet is a Tuple from JavaTuples library that deals with 3 elements. Since this Sextet is a generic class, it can hold any type of value in it.
Since Sextet 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 Sextet<A, B, C, D, E, F> extends Tuple
implements IValue0<A>, IValue1<B>, IValue2<C>, IValue3<D>, IValue4<E>, IValue5<F>


 

Class hierarchy


 

Object
 ↳ org.javatuples.Tuple
 ↳ org.javatuples.Sextet<A, B, C, D, E, F>


 

Creating Sextet Tuple


 

  • From Constructor:
    Syntax
     
Sextet<A, B, C, D, E, F> sextet = 
 new Sextet<A, B, C, D, E. F>
 (value1, value2, value3, value4, value5, value6);

  • Example
     

  • Output: 
     
[1, 2, 3, 4, 5, 6]

  •  
  • Using with() method: The with() method is a function provided by the JavaTuples library, to instantiate the object with such values.
    Syntax
     
Sextet<type1, type2, type3, type4, type5, type6> sextet = 
 Sextet.with(value1, value2, value3, value4, value5, value6);

  • Example
     

  • Output: 
     
[1, 2, 3, 4, 5, 6]

  •  
  • 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
     
Sextet<type1, type2, type3, type4, type5, type6> sextet = 
 Sextet.fromCollection(collectionWith_6_value);

Sextet<type1, type2, type3, type4, type5, type6> sextet = 
 Sextet.fromArray(arrayWith_6_value);

  • Example
     

  • Output: 
     
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6]

  •  


 

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
 

Sextet<type1, type2, type3, type4, type5, type6> sextet = 
 new Sextet<type1, type2, type3, type4, type5, type6>
 (value1, value2, value3, value4, value5, value6);

type1 val1 = sextet.getValue0();


Example
 

Output: 
 

1
3


 

Setting Sextet 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
 

Sextet<type1, type2, type3, type4, type5, type6> sextet = 
 new Sextet<type1, type2, type3, type4, type5, type6>
 (value1, value2, value3, value4, value5, value6);

Sextet<type1, type2, type3, type4, type5, type6> 
 otherSextet = sextet.setAtX(value);


Example
 

Output: 
 

[1, 2, 3, 40, 5, 6]


 

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
 

Sextet<type1, type2, type3, type4, type5, type6> sextet = 
 new Sextet<type1, type2, type3, type4, type5, type6>
 (value1, value2, value3, value4, value5, value6);

Sextet<type 1, type 2, type 3, type 4, type 5, type 6> sextet = 
 sextet.addAtx(value);


Example
 

Output: 
 

[1, 2, 3, 4, 5, 6, 7]


 

Searching in Sextet


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
 

Sextet<type1, type2, type3, type4, type5, type6> sextet = 
 new Sextet<type1, type2, type3, type4, type5, type6>
 (value1, value2, value3, value4, value5, value6);

boolean res = sextet.contains(value2);


Example
 

Output: 
 

true
false


 

Iterating through Sextet


Since Sextet implement the Iterable<Object> interface. It means that they can be iterated in the same way as collections or arrays.
Syntax
 

Sextet<type1, type2, type3, type4, type5, type6> sextet = 
 new Sextet<type1, type2, type3, type4, type5, type6>
 (value1, value2, value3, value4, value5, value6);

for (Object item : sextet) {
 ...
}


Example
 

Output: 
 

1
2
3
4
5
6


 

Comment
Article Tags:
Article Tags: