Octet Class in JavaTuples
A Octet is a Tuple from JavaTuples library that deals with 3 elements. Since this Octet is a generic class, it can hold any type of value in it.
Since Octet is a Tuple, hence it also has all the characterstics 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 Octet<A, B, C, D, E, F, G, H> extends Tuple
implements IValue0<A>, IValue1<B;>, IValue2<C>, IValue3<D>, IValue4<E>,
IValue5<F, IValue6<G, IValue7<H>
Class hierarchy
Object
↳ org.javatuples.Tuple
↳ org.javatuples.Octet<A, B, C, D, E, F, G, H>
Creating Octet Tuple
- From Constructor:
Syntax:
Octet<A, B, C, D, E, F, G, H> octet = new Octet<A, B, C, D, E, F, G, H> (value1, value2, value3, value4, value5, value6, value7, value8);Example:
// Below is a Java program to create// a Octet tuple from Constructorimportjava.util.*;importorg.javatuples.Octet;classGfG {publicstaticvoidmain(String[] args){Octet<Integer, Integer, Integer.Integer, Integer, Integer, Integer, Integer> octet= Octet.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));System.out.println(octet);}}chevron_rightfilter_noneOutput:
[1, 2, 3, 4, 5, 6, 7, 8]
- Using with() method: The with() method is a function provided by the JavaTuples library, to instantiate the object with such values.
Syntax:
Octet<type1, type2, type3, type4, type5, type6, type7> octet = Octet.with(value1, value2, value3, value4, value5, value6, value7, value8);Example:
// Below is a Java program to create// a Octet tuple from with() methodimportjava.util.*;importorg.javatuples.Octet;classGfG {publicstaticvoidmain(String[] args){Octet<Integer, Integer, Integer.Integer, Integer, Integer, Integer, Integer> octet= Octet.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));System.out.println(octet);}}chevron_rightfilter_noneOutput:
[1, 2, 3, 4, 5, 6, 7, 8]
- 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:
Octet<type1, type2, type3, type4, type5, type6, type7> octet = Octet.fromCollection(collectionWith_8_value); Octet<type1, type2, type3, type4, type5, type6, type7> octet = Octet.fromArray(arrayWith_8_value);Example:
// Below is a Java program to create// a Octet tuple from Collectionimportjava.util.*;importorg.javatuples.Octet;classGfG {publicstaticvoidmain(String[] args){// Creating Octet from ListList<Integer> list =newArrayList<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);Octet<Integer, Integer, Integer, Integer, Integer, Integer, Integer> octet= Octet.fromCollection(list);// Creating Octet from ArrayInteger[] arr = {1,2,3,4,5,6,7,8};Octet<Integer, Integer, Integer, Integer, Integer, Integer, Integer> otherOctet= Octet.fromArray(arr);System.out.println(octet);System.out.println(otherOctet);}}chevron_rightfilter_noneOutput:
[1, 2, 3, 4, 5, 6, 7, 8] [1, 2, 3, 4, 5, 6, 7, 8]
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:
Octet<type1, type2, type3, type4, type5, type6, type7> octet =
new Octet<type1, type2, type3, type4, type5, type6, type7>
(value1, value2, value3, value4, value5, value6, value7, value8);
type1 val1 = octet.getValue0();
Example:
// Below is a Java program to get // a Octet value import java.util.*; import org.javatuples.Octet; class GfG { public static void main(String[] args) { Octet<Integer, Integer, Integer.Integer, Integer, Integer, Integer, Integer> octet = Octet.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)); System.out.println(octet.getValue0()); System.out.println(octet.getValue2()); } } |
Output:
1 3
Setting Octet 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:
Octet<type1, type2, type3, type4, type5, type6, type7> octet =
new Octet<type1, type2, type3, type4, type5, type6, type7>
(value1, value2, value3, value4, value5, value6, value7, value8);
Octet<type1, type2, type3, type4, type5, type6, type7>
otherOctet = octet.setAtX(value);
Example:
// Below is a Java program to set // a Octet value import java.util.*; import org.javatuples.Octet; class GfG { public static void main(String[] args) { Octet<Integer, Integer, Integer.Integer, Integer, Integer, Integer, Integer> octet = Octet.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)); Octet<Integer, Integer, Integer.Integer, Integer, Integer, Integer, Integer> otherOctet = octet.setAt3(40); System.out.println(otherOctet); } } |
Output:
[1, 2, 3, 40, 5, 6, 7, 8]
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:
Octet<type1, type2, type3, type4, type5, type6, type7> octet =
new Octet<type1, type2, type3, type4, type5, type6, type7>
(value1, value2, value3, value4, value5, value6, value7, value8);
Octet<type 1, type 2, type 3, type 4, type 5, type 6, type 7> octet =
octet.addAtx(value);
Example:
// Below is a Java program to add // a value import java.util.*; import org.javatuples.Octet; import org.javatuples.Ennead; class GfG { public static void main(String[] args) { Octet<Integer, Integer, Integer.Integer, Integer, Integer, Integer, Integer> octet = Octet.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)); Ennead<Integer, Integer, Integer, Integer.Integer, Integer, Integer, Integer, Integer> ennead = octet.addAt8(9); System.out.println(ennead); } } |
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Searching in Octet
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:
Octet<type1, type2, type3, type4, type5, type6, type7> octet =
new Octet<type1, type2, type3, type4, type5, type6, type7>
(value1, value2, value3, value4, value5, value6, value7, value8);
boolean res = octet.contains(value2);
Example:
// Below is a Java program to search // a value in a Octet import java.util.*; import org.javatuples.Octet; class GfG { public static void main(String[] args) { Octet<Integer, Integer, Integer.Integer, Integer, Integer, Integer, Integer> octet = Octet.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)); boolean exist = octet.contains(5); boolean exist1 = octet.contains(false); System.out.println(exist); System.out.println(exist1); } } |
Output:
true false
Iterating through Octet
Since Octet implement the Iterable<Object> interface. It means that they can be iterated in the same way as collections or arrays.
Syntax:
Octet<type1, type2, type3, type4, type5, type6, type7> octet =
new Octet<type1, type2, type3, type4, type5, type6, type7>
(value1, value2, value3, value4, value5, value6, value7, value8);
for (Object item : octet) {
...
}
Example:
// Below is a Java program to iterate // a Octet import java.util.*; import org.javatuples.Octet; class GfG { public static void main(String[] args) { Octet<Integer, Integer, Integer.Integer, Integer, Integer, Integer, Integer> octet = Octet.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)); for (Object item : octet) System.out.println(item); } } |
Output:
1 2 3 4 5 6 7 8
Recommended Posts:
- Implement Octet Class from Septet Class in Java using JavaTuples
- Implement Ennead Class from Octet Class in Java using JavaTuples
- Implement Quintet Class with Quartet Class in Java using JavaTuples
- Implement Triplet Class with Pair Class in Java using JavaTuples
- Implement Pair Class with Unit Class in Java using JavaTuples
- Implement Decade Class from Ennead Class in Java using JavaTuples
- Implement Sextet Class from Quintet Class in Java using JavaTuples
- Implement Septet Class from Sextet Class in Java using JavaTuples
- Implement Quartet Class with Triplet Class in Java using JavaTuples
- Unit Class in JavaTuples
- Decade Class in JavaTuples
- LabelValue Class in JavaTuples
- KeyValue Class in JavaTuples
- Ennead Class in JavaTuples
- Triplet Class in JavaTuples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



