VOOZH about

URL: https://www.geeksforgeeks.org/java/triplet-class-in-java-tuples/

⇱ Triplet Class in JavaTuples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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

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

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

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:  

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

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:  

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:  

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:  

Output: 

1
GeeksforGeeks
A computer portal


 

Comment
Article Tags:
Article Tags: