The Wayback Machine - https://web.archive.org/web/20240712172732/https://www.geeksforgeeks.org/triplet-class-in-java-tuples/
Open In App

Triplet Class in JavaTuples

Last Updated : 17 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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

Since Triplet 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 Triplet<A, B, C> extends Tuple
    implements IValue0<A>, IValue1<B>, IValue2<C> 

Class Hierarchy 

Object
  ↳ org.javatuples.Tuple
      ↳ org.javatuples.Triplet<A, B, C> 

Creating Triplet Tuple

  • From Constructor: 
    Syntax:
Triplet<A, B, C> triplet = 
    new Triplet<A, B, C>(value1, value2, value3);

Example: 

Java




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


Output: 

[1, GeeksforGeeks, A computer portal]
  • Using with() method: The with() method is a function provided by the JavaTuples library, to instantiate the object with such values.
    Syntax:
Triplet<type1, type2, type3> triplet = 
       Triplet.with(value1, value2, value3);

Example: 

Java




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


Output: 

[1, GeeksforGeeks, A computer portal]
  • 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:
Triplet<type1, type2, type3> triplet = 
    Triplet.fromCollection(collectionWith_2_value);

Triplet<type1, type2, type3> triplet = 
    Triplet.fromArray(arrayWith_2_value);

Example: 

Java




// Below is a Java program to create
// a Triplet tuple from Collection
 
import java.util.*;
import org.javatuples.Triplet;
 
class GfG {
    public static void main(String[] args)
    {
        // Creating Triplet from List
        List<String> list = new ArrayList<String>();
        list.add("GeeksforGeeks");
        list.add("A computer portal");
        list.add("for geeks");
 
        Triplet<String, String, String> triplet
            = Triplet.fromCollection(list);
 
        // Creating Triplet from Array
        String[] arr = { "GeeksforGeeks",
                         "A computer portal",
                         "for geeks" };
 
        Triplet<String, String, String> otherTriplet
            = Triplet.fromArray(arr);
 
        System.out.println(triplet);
        System.out.println(otherTriplet);
    }
}


Output: 

[GeeksforGeeks, A computer portal, for geeks]
[GeeksforGeeks, A computer portal, for geeks]

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:  

Triplet<type1, type2, type3> triplet = 
    new Triplet<type1, type2, type3>(value1, value2, value3);

type1 val1 = triplet.getValue0();

Example:  

Java




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


Output: 

1
A computer portal

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

Triplet<type1, type2, type3> triplet = 
    new Triplet<type1, type2, type3>
                (value1, value2, value3);

Triplet<type1, type2, type3> 
    otherTriplet = triplet.setAtX(value);

Example: 

Java




// Below is a Java program to set
// a Triplet value
  
import java.util.*;
import org.javatuples.Triplet;
  
class GfG {
    public static void main(String[] args)
    {
        Triplet<Integer, String, String> triplet
            = Triplet.with(Integer.valueOf(1), "GeeksforGeeks",
                                          "A computer portal");
  
        Triplet<Integer, String, String> otherTriplet
            = triplet.setAt1("A computer portal for geeks");
  
        System.out.println(otherTriplet);
    }
}


Output: 

[1, GeeksforGeeks, A computer portal for geeks]

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: 

Triplet<type1, type2, type3> triplet = 
    new Triplet<type1, type2, type3>(value1, value2, value3);

Quartet<type 1, type 2, type 3, type 4> quartet = 
    triplet.addAt3(value);

Example:  

Java




// Below is a Java program to add
// a value
 
import java.util.*;
import org.javatuples.Triplet;
import org.javatuples.Quartet;
 
class GfG {
    public static void main(String[] args)
    {
        Triplet<Integer, String, String> triplet
            = Triplet.with(Integer.valueOf(1),
                           "GeeksforGeeks",
                           "A computer portal");
 
        Quartet<Integer, String, String, String> quartet
            = triplet.addAt3("for geeks");
 
        System.out.println(quartet);
    }
}


Output: 

[1, GeeksforGeeks, A computer portal, for geeks]

Searching in Triplet

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:  

Triplet<type1, type2, type3> triplet = 
    new Triplet<type1, type2, type3>(value1, value2, value3);

boolean res = triplet.contains(value2);

Example:  

Java




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


Output: 

true
false 

Iterating through Triplet

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

Syntax:  

Triplet<type1, type2, type3> triplet = 
    new Triplet<type1, type2, type3>(value1, value2, value3);

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

Example:  

Java




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


Output: 

1
GeeksforGeeks
A computer portal

 



Previous Article
Next Article

Similar Reads

Implement Triplet Class with Pair Class in Java using JavaTuples
Following are the ways to implement Triplet Class with Pair Class Using direct values import java.util.*; import org.javatuples.*; class GfG { public static void main(String[] args) { // create Pair Pair&lt;Integer, String&gt; pair = new Pair&lt;Integer, String&gt;( Integer.valueOf(1), &quot;GeeksforGeeks&quot;); // Print the Pair System.out.printl
2 min read
Implement Quartet Class with Triplet Class in Java using JavaTuples
Following are the ways to implement Quartet Class with Triplet Class Using direct values import java.util.*; import org.javatuples.*; class GfG { public static void main(String[] args) { // create Triplet Triplet&lt;String, String, String&gt; triplet = new Triplet&lt;String, String, String&gt;( &quot;Triplet 1&quot;, &quot;1&quot;, &quot;GeeksforGe
3 min read
Implement Pair Class with Unit Class in Java using JavaTuples
To implement a Pair class with a Unit class in Java using JavaTuples, you can use the Pair class provided by the library and create a new Unit class that extends the Unit class provided by the library. Here is an example implementation: Here is an example Java code that uses the MyPair class with MyUnit and displays the output: Java Code import org
4 min read
Implement Quintet Class with Quartet Class in Java using JavaTuples
Following are the ways to implement Quintet Class with Quartet Class Using direct values import java.util.*; import org.javatuples.*; class GfG { public static void main(String[] args) { // create Quartet Quartet&lt;String, String, String, String&gt; quartet = new Quartet&lt;String, String, String, String&gt;( &quot;Quartet&quot;, &quot;Triplet
4 min read
Implement Octet Class from Septet Class in Java using JavaTuples
Prerequisite: Octet Class, Septet Class Below are the methods to implement a Octet Class using Septet Class in Java: Using direct values // Java program to illustrate // implementing Octet Class // from Septet Class // using direct values import java.util.*; import org.javatuples.*; class GfG { public static void main(String[] args) { // Create Sep
6 min read
Implement Ennead Class from Octet Class in Java using JavaTuples
Prerequisite: Ennead Class, Octet Class Below are the methods to implement a Ennead Class using Octet Class in Java: Using direct values // Java program to illustrate // implementing Ennead Class // from Octet Class // using direct values import java.util.*; import org.javatuples.*; class GfG { public static void main(String[] args) { // Create Oct
7 min read
Implement Sextet Class from Quintet Class in Java using JavaTuples
Prerequisite: Sextet Class, Quintet Class Below are the methods to implement a Sextet Class using Quintet Class in Java: Using direct values // Java program to illustrate // implementing Sextet Class // from Quintet Class using // direct values import java.util.*; import org.javatuples.*; class GfG { public static void main(String[] args) { // crea
5 min read
Implement Septet Class from Sextet Class in Java using JavaTuples
Prerequisite: Septet Class, Sextet Class Below are the methods to implement a Septet Class using Sextet Class in Java: Using direct values // Java program to illustrate // implementing Septet Class // from Sextet Class // using direct values import java.util.*; import org.javatuples.*; class GfG { public static void main(String[] args) { // Create
6 min read
Implement Decade Class from Ennead Class in Java using JavaTuples
Prerequisite: Decade Class, Ennead Class Below are the methods to implement a Decade Class using Ennead Class in Java: Using direct values // Java program to illustrate // implementing Decade Class // from Ennead Class // using direct values import java.util.*; import org.javatuples.*; class GfG { public static void main(String[] args) { // Create
8 min read
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 TypesafeThey are ImmutableThey are IterableThey are SerializableThey are Comparable (implements Comparable&lt;Tu
6 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg