![]() |
VOOZH | about |
The Unit class in JavaTuples is a tuple with no values. It is often used as a placeholder when you need to represent an empty tuple, for example, as the second value in a Pair when you only need the first value.
The Unit class is a subclass of the Tuple class, which means that it has all the methods and properties of a tuple, even though it has no values. For example, you can get the size of a Unit object using the getSize() method, which will return 0.
Output:
Unit size: 0
Unit values: ()
As the Unit class in JavaTuples is simply a placeholder for an empty tuple, there are no real advantages or disadvantages to using it. However, here are some things to consider:
In this example, we create a Unit object and display its size and values. As expected, the size is 0 and the values are empty parentheses.
The Unit class in JavaTuples is a container that holds a single value. It is part of the JavaTuples library, which provides a set of tuple classes that can hold multiple values. The Unit class is the simplest of the tuple classes and holds only a single value.
The Unit class has the following constructor and methods:
Constructor:
Unit(T value): Constructs a new Unit object with the specified value.
Methods:
getValue(): Returns the value held by the Unit object.
toString(): Returns a string representation of the Unit object.
Output:
Hello (Hello)
A Unit is a Tuple from JavaTuples library that deals with only 1 element. Since this Unit is a generic class, it can hold any type of value in it.
Since Unit is a Tuple, hence it also has all the characteristics of JavaTuples:
public final class Unit<A> extends Tuple implements IValue0<A>
Object ↳ org.javatuples.Tuple ↳ org.javatuples.Unit<A>
Creating Unit Tuple
Syntax:
Unit<A> unit = new Unit<A>(value);
Example:
Output:
[GeeksforGeeks]
Using with() method: The with() method is a function provided by the JavaTuples library, to instantiate the object with such values.
Syntax:
Unit<type 1> unit = Unit.with(value);
Example:
Output:
[GeeksforGeeks]
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 that of the Tuple and the number of values in the collection/array must match with the Tuple class.
Syntax:
Unit<type> unit = Unit.fromCollection(collectionWith_1_value); Unit<type> unit = Unit.fromArray(arrayWith_1_value);
Example:
Output:
[GeeksforGeeks] [A computer portal]
Getting Value
The getValueX() method can be used to fetch the value in a Tuple at index X. The indexing in Tuples starts with 0. Hence, the value at index X represents the value at position X+1.
Syntax:
Unit<type 1> unit = new Unit<type 1>(value); type1 val1 = unit.getValue0();
Example:
Output:
GeeksforGeeks
Setting Unit 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:
Unit<type1> unit = new Unit<type1>(value); type1 val1 = unit.setAt0();
Example:
Output:
[A computer portal]
Adding a value
Adding a value can be done with the help of addAtX() method, where X represents the index at which the value is to be added. This method returns a Tuple of elements one more than the called Tuple.
Syntax:
Unit<type1> unit = new Unit<type 1>(value); Pair<type1, type2> pair = unit.addAt1(value2);
Example:
Output:
[GeeksforGeeks, A computer portal]
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:
Unit<type1> unit = new Unit<type 1>(value); boolean res = unit.contains(value);
Example:
Output:
true false
Since Unit implements the Iterable<Object> interface. It means that they can be iterated in the same way as collections or arrays.
Syntax:
Unit<type 1> unit = new Unit<type 1>(value);
for (Object item : unit) {
...
}
Example:
Output:
GeeksforGeeks