Unit Class in Java Tuples
The Unit class in JavaTuples represents a tuple data structure that contains only one value. It is the most basic version of a tuple, acting as a container for one member. In other words, a Unit tuple represents a single item within a tuple structure.
Characteristics
Simplicity: The simplest tuple class is Unit, which has only one value.
Immutability: Like the other tuple classes in JavaTuples, Unit is immutable, which means that its value cannot be changed after instantiation.
Considerations
Use in Collections: When used within collections or as return values, clarity about the purpose of the Unit tuple is essential to avoid confusion.
Usage
Creating a Unit class
FileName: UnitExample.java
import org.javatuples.Unit; public class UnitExample { public static void main(String[] args) { // Creating a Unit tuple with a single value Unitunit = new Unit<>("Hello"); // Displaying the Unit tuple System.out.println("Unit tuple: " + unit); // Accessing the value held by the Unit tuple String value = unit.getValue0(); System.out.println("Value: " + value); } }
Output:
Unit tuple: [Hello] Value: Hello
Methods
1. Accessing Value
The getValueX() method can be used to obtain the value stored in a Unit.
Here is an example of using getValueX() method,
FileName: GetValueExample.java
import org.javatuples.Unit; public class SetValueExample { public static void main(String[] args) { // Creating a Unit tuple with a single value Unitunit = Unit.with("Old Value"); // Setting a new value at index 0 Unit updatedUnit = unit.setAt0("New Value"); // Displaying the updated Unit tuple System.out.println(updatedUnit); } }
Output:
World
2. Setting Value
The setAtX() method can be used to create a new Unit with a changed value, even though Units are immutable.
Here is an example of using setAtX() method,
FileName: SetValueExample.java
import org.javatuples.Unit; public class SetValueExample { public static void main(String[] args) { // Creating a Unit tuple with a single value Unitunit = Unit.with("Old Value"); // Setting a new value at index 0 Unit updatedUnit = unit.setAt0("New Value"); // Displaying the updated Unit tuple System.out.println(updatedUnit); } }
Output:
[New Value]
3. Adding Value
While more values cannot be directly added to a Unit, the addAtX() methods can be used to create a new tuple with more elements.
Here is an example of using addAtX() method,
FileName: AddValueExample.java
import org.javatuples.Unit; import org.javatuples.Pair; public class AddValueExample { public static void main(String[] args) { // Creating a Unit tuple with a single value Unitunit = Unit.with("Hello"); // Adding a value to the Unit tuple to create a Pair tuple Pair pair = unit.addAt1("World"); // Displaying the resulting Pair tuple System.out.println(pair); } }
Output:
[Hello, World]
4. Size
A Unit's size is always 1, indicating that it only contains one value.
Here is an example of using getSize() method,
FileName: SizeExample.java
import org.javatuples.Unit; public class SizeExample { public static void main(String[] args) { // Creating a Unit tuple with a single value Unitunit = Unit.with("Hello"); // Getting the size of the Unit tuple int size = unit.getSize(); // Displaying the size System.out.println("Size of Unit: " + size); } }
Output:
Size of Unit: 1
5. Equality Comparison
The equals() method compares units for equality.
Here is an example of using equals() method,
FileName: EqualityExample.java
import org.javatuples.Unit; public class EqualityExample { public static void main(String[] args) { // Creating two Unit tuples with the same value Unitunit1 = Unit.with("Hello"); Unit unit2 = Unit.with("Hello"); // Checking if the Unit tuples are equal boolean isEqual = unit1.equals(unit2); // Displaying the result System.out.println("Units are equal: " + isEqual); } }
Output:
Units are equal: true
6. Hash Code Calculation
The hashCode() method returns the hash code of a Unit based on its value.
Here is an example of using hashCode() method,
FileName: HashCodeExample.java
import org.javatuples.Unit; public class HashCodeExample { public static void main(String[] args) { // Creating a Unit tuple with a single value Unitunit = Unit.with("Java"); // Getting the hash code of the Unit tuple int hashCode = unit.hashCode(); // Displaying the hash code System.out.println("Hash code of Unit: " + hashCode); } }
Output:
Hash code of Unit: 2301568
7. Converting to Array
Units can be transformed into arrays by calling the toArray() function.
Here is an example of using toArray() method,
FileName: ToArrayExample.java
import org.javatuples.Unit; public class ToArrayExample { public static void main(String[] args) { // Creating a Unit tuple with a single value Unitunit = Unit.with("Java"); // Converting Unit tuple to array Object[] array = unit.toArray(); // Displaying the array System.out.println("Array from Unit: " + java.util.Arrays.toString(array)); } }
Output:
Array from Unit: [Java]
8. Searching in a Unit
As Units implement the Iterable interface, they can be iterated through using enhanced for loops.
Here is an example of using iteration,
FileName: IterateExample.java
import java.util.*; import org.javatuples.Unit; public class IterateExample { public static void main(String[] args) { // Creating a Unit tuple with a single value Unitunit = Unit.with("Java"); // Iterating through the Unit tuple for (Object i : unit) System.out.println(i); } }
Output:
Java
9. Concatenating Tuples
It combines two tuples to form a new tuple that contains all of the elements from both tuples.
Here is an example of using concatenation,
FileName: ConcatenatingTuplesExample.java
import org.javatuples.Unit; public class ConcatenatingTuplesExample { public static void main(String[] args) { // Creating two Unit tuples with single values Unitunit1 = Unit.with("Hello"); Unit unit2 = Unit.with("World"); // Concatenating the two Unit tuples Unit concatenatedUnit = Unit.with(unit1.getValue0() + unit2.getValue0()); // Displaying the concatenated Unit tuple System.out.println("Concatenated Unit tuple: " + concatenatedUnit); } }
Output:
Concatenated Unit tuple: [HelloWorld]