Tutorialspoint.dev

Sextet Class in JavaTuples

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

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

    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:

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

    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:

    // Below is a Java program to create
    // a Sextet tuple from Collection
      
    import java.util.*;
    import org.javatuples.Sextet;
      
    class GfG {
        public static void main(String[] args)
        {
            // Creating Sextet 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);
      
            Sextet<Integer, Integer, Integer, Integer, Integer, Integer> sextet
                = Sextet.fromCollection(list);
      
            // Creating Sextet from Array
            Integer[] arr = { 1, 2, 3, 4, 5, 6 };
      
            Sextet<Integer, Integer, Integer, Integer, Integer, Integer> otherSextet
                = Sextet.fromArray(arr);
      
            System.out.println(sextet);
            System.out.println(otherSextet);
        }
    }

    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:

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

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:

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

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:

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

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:

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

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:

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

Output:

1
2
3
4
5
6




This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter