Saturday, December 28, 2024
Google search engine
HomeLanguagesJavaEnnead Class in JavaTuples

Ennead Class in JavaTuples

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

Java




// Below is a Java program to create
// a Ennead tuple from Constructor
 
import java.util.*;
import org.javatuples.Ennead;
 
class GfG {
    public static void main(String[] args)
    {
        Ennead<Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer> ennead
            = Ennead.with(Integer.valueOf(1),
                          Integer.valueOf(2),
                          Integer.valueOf(3),
                          Integer.valueOf(4),
                          Integer.valueOf(5),
                          Integer.valueOf(6),
                          Integer.valueOf(7),
                          Integer.valueOf(8),
                          Integer.valueOf(9));
 
        System.out.println(ennead);
    }
}


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

Java




// Below is a Java program to create
// a Ennead tuple from with() method
 
import java.util.*;
import org.javatuples.Ennead;
 
class GfG {
    public static void main(String[] args)
    {
        Ennead<Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer> ennead
            = Ennead.with(Integer.valueOf(1),
                          Integer.valueOf(2),
                          Integer.valueOf(3),
                          Integer.valueOf(4),
                          Integer.valueOf(5),
                          Integer.valueOf(6),
                          Integer.valueOf(7),
                          Integer.valueOf(8),
                          Integer.valueOf(9));
 
        System.out.println(ennead);
    }
}


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

Java




// Below is a Java program to create
// a Ennead tuple from Collection
 
import java.util.*;
import org.javatuples.Ennead;
 
class GfG {
    public static void main(String[] args)
    {
        // Creating Ennead from List
        List<Integer> list = new ArrayList<Integer>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        list.add(6);
        list.add(7);
        list.add(8);
        list.add(9);
 
        Ennead<Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer> ennead
            = Ennead.fromCollection(list);
 
        // Creating Ennead from Array
        Integer[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
 
        Ennead<Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer> otherEnnead
            = Ennead.fromArray(arr);
 
        System.out.println(ennead);
        System.out.println(otherEnnead);
    }
}


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

Java




// Below is a Java program to get
// a Ennead value
 
import java.util.*;
import org.javatuples.Ennead;
 
class GfG {
    public static void main(String[] args)
    {
        Ennead<Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer> ennead
            = Ennead.with(Integer.valueOf(1),
                          Integer.valueOf(2),
                          Integer.valueOf(3),
                          Integer.valueOf(4),
                          Integer.valueOf(5),
                          Integer.valueOf(6),
                          Integer.valueOf(7),
                          Integer.valueOf(8),
                          Integer.valueOf(9));
 
        System.out.println(ennead.getValue0());
        System.out.println(ennead.getValue2());
    }
}


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:  

Java




// Below is a Java program to set
// a Ennead value
 
import java.util.*;
import org.javatuples.Ennead;
 
class GfG {
    public static void main(String[] args)
    {
        Ennead<Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer> ennead
            = Ennead.with(Integer.valueOf(1),
                          Integer.valueOf(2),
                          Integer.valueOf(3),
                          Integer.valueOf(4),
                          Integer.valueOf(5),
                          Integer.valueOf(6),
                          Integer.valueOf(7),
                          Integer.valueOf(8),
                          Integer.valueOf(9));
 
        Ennead<Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer> otherEnnead
            = ennead.setAt3(40);
 
        System.out.println(otherEnnead);
    }
}


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:  

Java




// Below is a Java program to add
// a value
 
import java.util.*;
import org.javatuples.Ennead;
import org.javatuples.Decade;
 
class GfG {
    public static void main(String[] args)
    {
        Ennead<Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer> ennead
            = Ennead.with(Integer.valueOf(1),
                          Integer.valueOf(2),
                          Integer.valueOf(3),
                          Integer.valueOf(4),
                          Integer.valueOf(5),
                          Integer.valueOf(6),
                          Integer.valueOf(7),
                          Integer.valueOf(8),
                          Integer.valueOf(9));
 
        Decade<Integer, Integer, Integer, Integer, Integer, Integer.Integer, Integer, Integer, Integer, Integer> ennead
            = ennead.addAt9(10);
 
        System.out.println(ennead);
    }
}


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:  

Java




// Below is a Java program to search
// a value in a Ennead
 
import java.util.*;
import org.javatuples.Ennead;
 
class GfG {
    public static void main(String[] args)
    {
        Ennead<Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer> ennead
            = Ennead.with(Integer.valueOf(1),
                          Integer.valueOf(2),
                          Integer.valueOf(3),
                          Integer.valueOf(4),
                          Integer.valueOf(5),
                          Integer.valueOf(6),
                          Integer.valueOf(7),
                          Integer.valueOf(8),
                          Integer.valueOf(9));
 
        boolean exist = ennead.contains(5);
        boolean exist1 = ennead.contains(false);
 
        System.out.println(exist);
        System.out.println(exist1);
    }
}


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

Java




// Below is a Java program to iterate
// a Ennead
 
import java.util.*;
import org.javatuples.Ennead;
 
class GfG {
    public static void main(String[] args)
    {
        Ennead<Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer> ennead
            = Ennead.with(Integer.valueOf(1),
                          Integer.valueOf(2),
                          Integer.valueOf(3),
                          Integer.valueOf(4),
                          Integer.valueOf(5),
                          Integer.valueOf(6),
                          Integer.valueOf(7),
                          Integer.valueOf(8),
                          Integer.valueOf(9));
 
        for (Object item : ennead)
            System.out.println(item);
    }
}


Output: 

1
2
3
4
5
6
7
8
9

 

RELATED ARTICLES

Most Popular

Recent Comments