Triplet Class in JavaTuples
In JavaTuples, the Triplet class represents a three-value tuple. It is a type-safe container capable of containing three elements, making it easy to group numerous values into a single object.
Syntax
Triplet triplet = new Triplet(value1, value2, value3);
Here,
Triplet: Defines the Triplet class in the JavaTuples library.
(values 1, 2, and 3): These are the values supplied to the Triplet constructor to set up its elements. value1, value2, and value3 are variables that store values of types A, B, and C, respectively.
Characteristics
Versatility: Triplet is a simple way to store and modify three linked variables as one entity.
Immutability: Triplet, like the other tuple classes in JavaTuples, is immutable, which means its values cannot be modified once instantiated. It provides thread safety and predictability in multi-threaded situations.
Considerations
Type Safety: Triplet achieves type safety by declaring the types of its elements at build time, which prevents runtime errors.
Clarity: When utilizing Triplet within collections or as return values, explicit naming, and documentation are required to express the meaning and purpose of each value in the triplet.
Usage
Creating a Triplet class
FileName: TripletExample.java
import org.javatuples.Triplet; public class TripletExample { public static void main(String[] args) { // Creating a Triplet tuple with three values Triplettriplet = Triplet.with("Hello", 5, true); // Displaying the Triplet tuple System.out.println("Triplet tuple: " + triplet); } }
Output:
Triplet tuple: [Hello, 5, true]
Methods
1. Accessing Value
The getValueX() method retrieves the value stored at the supplied index (0-based) in the triplet.
Here is an example of using getValueX() method,
FileName: GetValueExample.java
import org.javatuples.Triplet; public class GetValueExample { public static void main(String[] args) { // Creating a Triplet tuple with three values Triplettriplet = Triplet.with("Hello", 5, true); // Accessing values String firstValue = triplet.getValue0(); Integer secondValue = triplet.getValue1(); Boolean thirdValue = triplet.getValue2(); // Displaying values System.out.println("First Value: " + firstValue); System.out.println("Second Value: " + secondValue); System.out.println("Third Value: " + thirdValue); } }
Output:
First Value: Hello Second Value: 5 Third Value: true
2. Setting Value
The setAtX(value) method generates a new Triplet in which the value at the provided index is replaced by the given value.
Here is an example of using setAtX() method,
FileName: SetValueExample.java
import org.javatuples.Triplet; public class SetValueExample { public static void main(String[] args) { // Creating a Triplet tuple with three values Triplettriplet = Triplet.with("Hello", 5, true); // Setting new values Triplet updatedTriplet = triplet.setAt0("New Value") .setAt1(10) .setAt2(false); // Displaying updated Triplet tuple System.out.println("Updated Triplet tuple: " + updatedTriplet); } }
Output:
Updated Triplet tuple: [New Value, 10, false]
3. Adding Value
The addAtX(value) methods add a value at the specified index (beginning with 0) to form a new tuple with more members.
Here is an example of using addAtX() method,
FileName: AddValueExample.java
import org.javatuples.Triplet; import org.javatuples.Quartet; public class AddValueExample { public static void main(String[] args) { // Creating a Triplet tuple with three values Triplettriplet = Triplet.with("Hello", 5, true); // Adding a value to the Triplet tuple to create a Quartet tuple Quartet quartet = triplet.addAt3(3.14); // Displaying the resulting Quartet tuple System.out.println("Quartet tuple: " + quartet); } }
Output:
Quartet tuple: [Hello, 5, true, 3.14]
4. Size
The getSize() method returns the Triplet's size, which is always 3 because it has three elements.
Here is an example of using getSize() method,
FileName: SizeExample.java
import org.javatuples.Triplet; public class SizeExample { public static void main(String[] args) { // Creating a Triplet tuple with three values Triplettriplet = Triplet.with("Hello", 5, true); // Getting the size of the Triplet tuple int size = triplet.getSize(); // Displaying the size System.out.println("Size of Triplet: " + size); } }
Output:
Size of Triplet: 3
5. Equality Comparison
The 'equals()' method compares two Triplet objects to see if their elements are equal.
Here is an example of using equals() method,
FileName: EqualityExample.java
import org.javatuples.Triplet; public class EqualityExample { public static void main(String[] args) { // Creating two Triplet tuples with the same values Triplettriplet1 = Triplet.with("Hello", 5, true); Triplet triplet2 = Triplet.with("Hello", 5, true); // Checking if the Triplet tuples are equal boolean isEqual = triplet1.equals(triplet2); // Displaying the result System.out.println("Triplets are equal: " + isEqual); } }
Output:
Triplets are equal: true
6. Hash Code Calculation
The hashCode() method returns the Hash Code of the Triplet.
Here is an example of using hashCode() method,
FileName: HashCodeExample.java
import org.javatuples.Triplet; public class HashCodeExample { public static void main(String[] args) { // Creating a Triplet tuple with three values Triplettriplet = Triplet.with("Hello", 5, true); // Getting the hash code of the Triplet tuple int hashCode = triplet.hashCode(); // Displaying the hash code System.out.println("Hash code of Triplet: " + hashCode); } }
Output:
Hash code of Triplet: -1824571878
7. Converting to Array
The toArray() method turns the Triplet into an Object array, allowing for simple manipulation and access to its elements.
Here is an example of using toArray() method,
FileName: ToArrayExample.java
import org.javatuples.Triplet; public class ToArrayExample { public static void main(String[] args) { // Creating a Triplet tuple with three values Triplettriplet = Triplet.with("Hello", 5, true); // Converting Triplet tuple to array Object[] array = triplet.toArray(); // Displaying the array System.out.println("Array from Triplet: " + java.util.Arrays.toString(array)); } }
Output:
Array from Triplet: [Hello, 5, true]
8. Iteration in a Triplet
Triplet implements the Iterable interface, which allows it to be iterated using extended for loops, making it easy to traverse and analyze its elements.
Here is an example of using iteration,
FileName: IterateExample.java
import java.util.*; import org.javatuples.Triplet; public class IterateExample { public static void main(String[] args) { // Creating a Triplet tuple with three values Triplettriplet = Triplet.with("Hello", 5, true); // Iterating through the Triplet tuple for (Object item : triplet) System.out.println(item); } }
Output:
Hello 5 true