Tutorialspoint.dev

Quintet Class in JavaTuples

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

Since Quintet 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 Quintet<A, B, C, D, E> extends Tuple
implements IValue0<A>, IValue1<B>, IValue2<C>, IValue3<D>, IValue4<E>

Class Hierarchy

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

Creating Quintet Tuple

  • From Constructor:

    Syntax:

    Quintet<A, B, C, D, E> quintet = 
        new Quintet<A, B, C, D, E>
            (value1, value2, value3, value4, value5);
    

    Example:

    // Below is a Java program to create
    // a Quintet tuple from Constructor
      
    import java.util.*;
    import org.javatuples.Quintet;
      
    class GfG {
        public static void main(String[] args)
        {
            Quintet<Integer, String, String, Double, Boolean> quintet
                = Quintet.with(Integer.valueOf(1),
                               "GeeksforGeeks",
                               "A computer portal",
                               Double.valueOf(20.18),
                               true);
      
            System.out.println(quintet);
        }
    }

    Output:

    br>
    [1, GeeksforGeeks, A computer portal, 20.18, true]
    
  • Using with() method: The with() method is a function provided by the JavaTuples library, to instantiate the object with such values.

    Syntax:

    Quintet<type1, type2, type3, type4, type5> quintet = 
        Quintet.with(value1, value2, value3, value4, value5);
    

    Example:

    // Below is a Java program to create
    // a Quintet tuple from with() method
      
    import java.util.*;
    import org.javatuples.Quintet;
      
    class GfG {
        public static void main(String[] args)
        {
            Quintet<Integer, String, String, Double, Boolean> quintet
                = Quintet.with(Integer.valueOf(1),
                               "GeeksforGeeks",
                               "A computer portal",
                               Double.valueOf(20.18),
                               true);
      
            System.out.println(quintet);
        }
    }

    Output:

    [1, GeeksforGeeks, A computer portal, 20.18, true]
    
  • 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:

    Quintet<type1, type2, type3, type4, type5> quintet = 
        Quintet.fromCollection(collectionWith_5_value);
    
    Quintet<type1, type2, type3, type4, type5> quintet = 
        Quintet.fromArray(arrayWith_5_value);
    

    Example:

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

    Output:

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

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:

Quintet<type1, type2, type3, type4, type5> quintet = 
    new Quintet<type1, type2, type3, type4, type5>
                   (value1, value2, value3, value4, value5);

type1 val1 = quintet.getValue0();

Example:

// Below is a Java program to get
// a Quintet value
  
import java.util.*;
import org.javatuples.Quintet;
  
class GfG {
    public static void main(String[] args)
    {
        Quintet<Integer, String, String, Double, Boolean> quintet
            = Quintet.with(Integer.valueOf(1),
                           "GeeksforGeeks",
                           "A computer portal",
                           Double.valueOf(20.18),
                           true);
  
        System.out.println(quintet.getValue0());
        System.out.println(quintet.getValue2());
    }
}

Output:



1
A computer portal

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

Quintet<type1, type2, type3, type4, type5> quintet = 
    new Quintet<type1, type2, type3, type4, type5>
                  (value1, value2, value3, value4, value5);

Quintet<type1, type2, type3, type4, type5> 
    otherQuintet = quintet.setAtX(value);

Example:

// Below is a Java program to set
// a Quintet value
  
import java.util.*;
import org.javatuples.Quintet;
  
class GfG {
    public static void main(String[] args)
    {
        Quintet<Integer, String, String, Double, Boolean> quintet
            = Quintet.with(Integer.valueOf(1),
                           "GeeksforGeeks",
                           "A computer portal",
                           Double.valueOf(20.18),
                           true);
  
        Quintet<Integer, String, String, Double> otherQuintet
            = quintet.setAt3(2.018);
  
        System.out.println(otherQuintet);
    }
}

Output:

[1, GeeksforGeeks, A computer portal, 2.018, true]

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:

Quintet<type1, type2, type3, type4, type5> quintet = 
    new Quintet<type1, type2, type3, type4, type5>
                   (value1, value2, value3, value4, value5);

Quintet<type 1, type 2, type 3, type 4, type 5> quintet = 
    quintet.addAtx(value);

Example:

// Below is a Java program to add
// a value
  
import java.util.*;
import org.javatuples.Quintet;
import org.javatuples.Sextet;
  
class GfG {
    public static void main(String[] args)
    {
        Quintet<Integer, String, String, Double, Boolean> quintet
            = Quintet.with(Integer.valueOf(1),
                           "GeeksforGeeks",
                           "A computer portal",
                           Double.valueOf(20.18),
                           true);
  
        Quintet<Integer, String, String, Double, Boolean, Boolean> sextet
            = quintet.addAt5(false);
  
        System.out.println(sextet);
    }
}

Output:

[1, GeeksforGeeks, A computer portal, for geeks, 20.18, true, false]

Searching in Quintet

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:



Quintet<type1, type2, type3, type4, type5> quintet = 
    new Quintet<type1, type2, type3, type4, type5>
                   (value1, value2, value3, value4, value5);

boolean res = quintet.contains(value2);

Example:

// Below is a Java program to search
// a value in a Quintet
  
import java.util.*;
import org.javatuples.Quintet;
  
class GfG {
    public static void main(String[] args)
    {
        Quintet<Integer, String, String, Double, Boolean> quintet
            = Quintet.with(Integer.valueOf(1),
                           "GeeksforGeeks",
                           "A computer portal",
                           Double.valueOf(20.18),
                           true);
  
        boolean exist = quintet.contains(20.18);
        boolean exist1 = quintet.contains(false);
  
        System.out.println(exist);
        System.out.println(exist1);
    }
}

Output:

true
false

Iterating through Quintet

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

Syntax:

Quintet<type1, type2, type3, type4, type5> quintet = 
    new Quintet<type1, type2, type3, type4, type5>
                   (value1, value2, value3, value4, value5);

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

Example:

// Below is a Java program to iterate
// a Quintet
  
import java.util.*;
import org.javatuples.Quintet;
  
class GfG {
    public static void main(String[] args)
    {
        Quintet<Integer, String, String, Double, Boolean> quintet
            = Quintet.with(Integer.valueOf(1),
                           "GeeksforGeeks",
                           "A computer portal",
                           Double.valueOf(20.18),
                           true);
  
        for (Object item : quintet)
            System.out.println(item);
    }
}

Output:

1
GeeksforGeeks
A computer portal
20.18
true




This article is attributed to GeeksforGeeks.org

You Might Also Like

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter