Quartet Class in JavaTuples
The Quartet class in JavaTuples denotes a tuple data structure with four values. It is similar to other tuple types but with four elements.
Syntax
Quartet quartet = new Quartet(value1, value2, value3, value4);
Here,
Quartet: Specifies the Quartet class in the JavaTuples library.
(values 1, 2, 3, and 4): These are the values passed to the Quartet constructor to set up its elements. value1, value2, value3, and value4 are variables that contain values of types A, B, C, and D, respectively.
Characteristics
Simplicity: Quartet is a straightforward tuple class containing four values.
Immutability: Quartet, like all other tuple types in JavaTuples, is immutable, which means its values cannot be modified after instantiation.
Considerations
Clarity in Usage: When using Quartet within collections or as return values, it's essential to ensure clarity about its purpose to avoid confusion.
Usage
Creating a Quartet class
FileName: QuartetExample.java
import org.javatuples.Quartet; public class QuartetExample { public static void main(String[] args) { // Creating a Quartet tuple with four values Quartetquartet = new Quartet<>("Alice", 25, 5.8, true); // Displaying the Quartet tuple System.out.println("Quartet tuple: " + quartet); // Accessing values held by the Quartet tuple String name = quartet.getValue0(); Integer age = quartet.getValue1(); Double height = quartet.getValue2(); Boolean isStudent = quartet.getValue3(); // Displaying the values System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("Height: " + height); System.out.println("Is Student: " + isStudent); } }
Output:
Name: Alice Age: 25 Height: 5.8 Is Student: true
Methods
1. Setting Value
The setAtX(value) method replaces the value at the specified index with the provided value, resulting in a new Quartet with the updated value.
Here is an example of using setAtX() method,
FileName: SettingValuesExample.java
import org.javatuples.Quartet; public class SettingValuesExample { public static void main(String[] args) { // Creating a Quartet tuple with four values Quartetquartet = new Quartet<>("Alice", 25, 5.8, true); // Setting a new value at index 0 (creating a new Quartet with updated value) Quartet updatedQuartet = quartet.setAt0("Bob"); // Displaying the updated Quartet tuple System.out.println(updatedQuartet); } }
Output:
[Bob, 25, 5.8, true]
2. Adding Value
The addAtX(value) methods add a value at the specified index, resulting in a new tuple with additional 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]
3. Size
The getSize() method returns the Quartet's size, which is always 4, as it has four elements.
Here is an example of using getSize() method,
FileName: SizeExample.java
import org.javatuples.Quartet; public class SizeExample { public static void main(String[] args) { // Creating a Quartet tuple with four values Quartetquartet = new Quartet<>("Alice", 25, 5.8, true); // Getting size int size = quartet.getSize(); System.out.println("Size of Quartet: " + size); } }
Output:
Size of Quartet: 4
4. Equality Comparison
The equals() method compares two Quartet objects to determine whether their elements are equal.
Here is an example of using equals() method,
FileName: EqualityExample.java
import org.javatuples.Quartet; public class EqualityExample { public static void main(String[] args) { // Creating two Quartet tuples with the same values Quartetquartet1 = new Quartet<>("Hello", 5, 3.14, true); Quartet quartet2 = new Quartet<>("Hello", 5, 3.14, true); // Checking if the Quartet tuples are equal boolean isEqual = quartet1.equals(quartet2); // Displaying the result System.out.println("Quartets are equal: " + isEqual); } }
Output:
Quartets are equal: true
5. Hash Code Calculation
The hashCode() method returns the Hash Code of the Quartet.
Here is an example of using hashCode() method,
FileName: HashCodeExample.java
import org.javatuples.Quartet; public class HashCodeExample { public static void main(String[] args) { // Creating a Quartet tuple with four values Quartetquartet = new Quartet<>("Hello", 5, 3.14, true); // Getting the hash code of the Quartet tuple int hashCode = quartet.hashCode(); // Displaying the hash code System.out.println("Hash code of Quartet: " + hashCode); } }
Output:
Hash code of Quartet: -15152517
6. Converting to Array
The toArray() method converts the Quartet to an Object array.
Here is an example of using toArray() method,
FileName: ToArrayExample.java
import org.javatuples.Quartet; public class ToArrayExample { public static void main(String[] args) { // Creating a Quartet tuple with four values Quartetquartet = new Quartet<>("Hello", 5, 3.14, true); // Converting Quartet tuple to array Object[] array = quartet.toArray(); // Displaying the array System.out.println("Array from Quartet: " + java.util.Arrays.toString(array)); } }
Output:
Array from Quartet: [Hello, 5, 3.14, true]
7. Iteration in a Quartet
Iterating over a Quartet tuple involves accessing each element of the tuple sequentially.
Here is an example of using iteration,
FileName: IterateExample.java
import org.javatuples.Quartet; public class IterateExample { public static void main(String[] args) { // Creating a Quartet tuple with four values Quartetquartet = new Quartet<>("Hello", 5, 3.14, true); // Iterating through the Quartet tuple for (Object item : quartet) { System.out.println(item); } } }
Output:
Hello 5 3.14 true