Triplet Class in JavaTuples
Last Updated :
17 Feb, 2022
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
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
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
import java.util.*;
import org.javatuples.Triplet;
class GfG {
public static void main(String[] args)
{
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);
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
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
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
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
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
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
Similar Reads
JavaTuples | Introduction
Tuple The word "tuple" means "a data structure consisting of multiple parts". Hence Tuples can be defined as a data structure that can hold multiple values and these values may/may not be related to each other. Example: [Geeks, 123, *@] In this example, the values in the tuple are not at all relat
6 min read
Unit Class in JavaTuples
The Unit class in JavaTuples is a tuple with no values. It is often used as a placeholder when you need to represent an empty tuple, for example, as the second value in a Pair when you only need the first value. The Unit class is a subclass of the Tuple class, which means that it has all the methods
7 min read
Pair Class in JavaTuples
A Pair is a Tuple from JavaTuples library that deals with 2 elements. Since this Pair is a generic class, it can hold any type of value in it.Since Pair is a Tuple, hence it also has all the characteristics of JavaTuples: They are TypesafeThey are ImmutableThey are IterableThey are SerializableThey
5 min read
Triplet Class in JavaTuples
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 TypesafeThey are ImmutableThey are IterableThey are Seriali
5 min read
Quartet Class in JavaTuples
A Quartet is a Tuple from JavaTuples library that deals with 4 elements. Since this Quartet is a generic class, it can hold any type of value in it. Since Quartet is a Tuple, hence it also has all the characteristics of JavaTuples: They are TypesafeThey are ImmutableThey are IterableThey are Seriali
5 min read
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 characteristics of JavaTuples: They are TypesafeThey are ImmutableThey are IterableThey are Serializ
5 min read
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 characteristics of JavaTuples: They are TypesafeThey are ImmutableThey are IterableThey are Serializabl
6 min read
Septet Class in JavaTuples
A Septet is a Tuple from JavaTuples library that deals with 3 elements. Since this Septet is a generic class, it can hold any type of value in it.Since Septet is a Tuple, hence it also has all the characteristics of JavaTuples: They are TypesafeThey are ImmutableThey are IterableThey are Serializabl
6 min read
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 characteristics of JavaTuples: They are TypesafeThey are ImmutableThey are IterableThey are SerializableTh
6 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 Serializab
6 min read