VOOZH about

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

⇱ Ennead Class in JavaTuples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Ennead Class in JavaTuples

Last Updated : 3 Aug, 2021

A Ennead is a Tuple from JavaTuples library that deals with 9 elements. Since this Ennead is a generic class, it can hold any type of value in it.

Since Ennead 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 Ennead<A, B, C, D, E, F, G, H, I>
extends Tuple
implements IValue0<A&gt, IValue1<B>, IValue2<C>, IValue3<D>, IValue4<E>, 
 IValue5<F, IValue6<G, IValue7<H&gt, IValue8<I> 

Class hierarchy

Object
 ↳ org.javatuples.Tuple
 ↳ org.javatuples.Ennead<A, B, C, D, E, F, G, H, I> 

Creating Ennead Tuple 

  • From Constructor:
    Syntax
Ennead<A, B, C, D, E, F, G, H, I> ennead = 
 new Ennead<A, B, C, D, E, F, G, H, I>
 (value1, value2, value3, value4, value5, value6, value7, value8, value9);
  • Example

Output: 

[1, 2, 3, 4, 5, 6, 7, 8, 9]
  • Using with() method: The with() method is a function provided by the JavaTuples library, to instantiate the object with such values.
    Syntax
Ennead<type1, type2, type3, type4, type5, type6, type7, type8> ennead = 
 Ennead.with(value1, value2, value3, value4, value5, value6, value7, value8, value9);
  • Example

Output: 

[1, 2, 3, 4, 5, 6, 7, 8, 9]
  • 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
Ennead<type1, type2, type3, type4, type5, type6, type7, type8> ennead = 
 Ennead.fromCollection(collectionWith_9_value);

Ennead<type1, type2, type3, type4, type5, type6, type7, type8> ennead = 
 Ennead.fromArray(arrayWith_9_value);
  • Example

Output: 

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

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:  

Ennead<type1, type2, type3, type4, type5, type6, type7, type8> ennead = 
 new Ennead<type1, type2, type3, type4, type5, type6, type7, type8>
 (value1, value2, value3, value4, value5, value6, value7, value8, value9);

type1 val1 = ennead.getValue0();

Example

Output: 

1
3

Setting Ennead 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:  

Ennead<type1, type2, type3, type4, type5, type6, type7, type8> ennead = 
 new Ennead<type1, type2, type3, type4, type5, type6, type7, type8>
 (value1, value2, value3, value4, value5, value6, value7, value8, value9);

Ennead<type1, type2, type3, type4, type5, type6, type7, type8> 
 otherEnnead = ennead.setAtX(value);

Example:  

Output: 

[1, 2, 3, 40, 5, 6, 7, 8, 9] 

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:  

Ennead<type1, type2, type3, type4, type5, type6, type7, type8> ennead = 
 new Ennead<type1, type2, type3, type4, type5, type6, type7, type8>
 (value1, value2, value3, value4, value5, value6, value7, value8, value9);

Ennead<type 1, type 2, type 3, type 4, type 5, type 6, type 7, type 8> ennead = 
 ennead.addAtx(value);

Example:  

Output: 

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 

Searching in Ennead

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:  

Ennead<type1, type2, type3, type4, type5, type6, type7, type8> ennead = 
 new Ennead<type1, type2, type3, type4, type5, type6, type7, type8>
 (value1, value2, value3, value4, value5, value6, value7, value8, value9);

boolean res = ennead.contains(value2);

Example:  

Output: 

true
false

Iterating through Ennead

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

Syntax:  

Ennead<type1, type2, type3, type4, type5, type6, type7, type8> ennead = 
 new Ennead<type1, type2, type3, type4, type5, type6, type7, type8>
 (value1, value2, value3, value4, value5, value6, value7, value8, value9);

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


Example

Output: 

1
2
3
4
5
6
7
8
9


 

Comment
Article Tags:
Article Tags: