Pair Class in JavaTuples
The Pair class in JavaTuples represents a two-value tuple data structure. It is frequently used to keep a pair of related values together within a tuple.
Syntax
Pairpair = new Pair<>(value1, value2);
Here,
Type1 and Type2 indicate the types of the two values in the Pair tuple.
value1 and value2 are the actual values you want to store in the Pair tuple.
Characteristics
Versatility: Pairs offer a mix of simplicity and versatility, making them appropriate for a variety of use cases that require two parts.
Immutability: Pair, like other tuple types in JavaTuples, is immutable, which means that its values cannot be modified after instantiation.
Considerations
Semantic Clarity: When utilizing Pair objects, it is critical to establish semantic clarity regarding the meaning of each item in the pair to avoid code readability and maintenance issues.
Usage
Creating a Pair class
FileName: PairExample.java
import org.javatuples.Pair; public class PairExample { public static void main(String[] args) { // Creating a Pair tuple with two values Pairpair = new Pair<>("John", 25); // Displaying the Pair tuple System.out.println("Pair tuple: " + pair); // Accessing the values held by the Pair tuple String name = pair.getValue0(); Integer age = pair.getValue1(); System.out.println("Name: " + name); System.out.println("Age: " + age); } }
Output:
Pair tuple: [John, 25] Name: John Age: 25
Methods
1. Accessing Value
The getValueX() method can be used to retrieve values from a Pair.
Here is an example of using getValueX() method,
FileName: AccessingValuesExample.java
import org.javatuples.Pair; public class AccessingValuesExample { public static void main(String[] args) { // Creating a Pair tuple with two values Pairpair = new Pair<>("John", 25); // Accessing the values held by the Pair tuple String name = pair.getValue0(); Integer age = pair.getValue1(); System.out.println("Name: " + name); System.out.println("Age: " + age); } }
Output:
Name: John Age: 25
2. Setting Value
The setAtX() method can be used to create a new Pair with a changed value, even though Pairs are immutable.
Here is an example of using setAtX() method,
FileName: SettingValuesExample.java
import org.javatuples.Pair; public class SettingValuesExample { public static void main(String[] args) { // Creating a Pair tuple with two values Pairpair = new Pair<>("John", 25); // Creating a new Pair with updated values Pair updatedPair = pair.setAt0("Jane").setAt1(30); // Displaying the updated Pair tuple System.out.println(updatedPair); } }
Output:
[Jane, 30]
3. Size
A Pair always has a size of two, indicating that it holds two values.
Here is an example of using getSize() method,
FileName: SizeExample.java
import org.javatuples.Pair; public class SizeExample { public static void main(String[] args) { // Creating a Pair tuple with two values Pairpair = new Pair<>("John", 25); // Getting the size of the Pair tuple int size = pair.getSize(); // Displaying the size System.out.println("Size of Pair: " + size); } }
Output:
Size of Pair: 2
4. Equality Comparison
The equals() method checks Pairs for equality using their values.
Here is an example of using equals() method,
FileName: EqualityComparisonExample.java
import org.javatuples.Pair; public class EqualityComparisonExample { public static void main(String[] args) { // Creating two Pair tuples with the same values Pairpair1 = new Pair<>("John", 25); Pair pair2 = new Pair<>("John", 25); // Checking if the Pair tuples are equal boolean isEqual = pair1.equals(pair2); // Displaying the result System.out.println("Pairs are equal: " + isEqual); } }
Output:
Pairs are equal: true
5. Hash Code Calculation
The hashCode() method computes the hash code of a Pair based on its values.
Here is an example of using hashCode() method,
FileName: HashCodeExample.java
import org.javatuples.Pair; public class HashCodeCalculationExample { public static void main(String[] args) { // Creating a Pair tuple with two values Pairpair = new Pair<>("John", 25); // Getting the hash code of the Pair tuple int hashCode = pair.hashCode(); // Displaying the hash code System.out.println("Hash code of Pair: " + hashCode); } }
Output:
Hash code of Pair: 71751726
6. Converting to Array
Pairs can be converted into arrays by using the toArray() method.
Here is an example of using toArray() method,
FileName: ToArrayExample.java
import org.javatuples.Pair; public class ToArrayExample { public static void main(String[] args) { // Creating a Pair tuple with two values Pairpair = new Pair<>("John", 25); // Converting Pair tuple to array Object[] array = pair.toArray(); // Displaying the array System.out.println("Array from Pair: " + java.util.Arrays.toString(array)); } }
Output:
Array from Pair: [John, 25]
7. Iteration in a Unit
Pairs can be iterated with enhanced for loops since they implement the Iterable interface.
Here is an example of using iteration,
FileName: IterateExample.java
import org.javatuples.Pair; public class IterateExample { public static void main(String[] args) { // Creating a Pair tuple with two values Pairpair = new Pair<>("John", 25); // Iterating through the Pair tuple for (Object value : pair) System.out.println(value); } }
Output:
John 25
8. Concatenating Tuples
It combines two pairs to create a new pair that has all of the elements from both pairs.
Here is an example of using concatenation,
FileName: ConcatenatingExample.java
import org.javatuples.Pair; public class ConcatenatingExample { public static void main(String[] args) { // Creating two Pair tuples with two values each Pairpair1 = new Pair<>("John", 25); Pair pair2 = new Pair<>("Doe", 30); // Concatenating the two Pairs Pair concatenatedPair = new Pair<>(pair1.getValue0() + pair2.getValue0(), pair1.getValue1() + pair2.getValue1()); // Displaying the concatenated Pair tuple System.out.println("Concatenated Pair tuple: " + concatenatedPair); } }
Output:
Concatenated Pair tuple: [JohnDoe, 55]