Unit Class in JavaTuples
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 characterstics of JavaTuples:
- They are Typesafe
- They are Immutable
- They are Iterable
- They are Serializable
- They are Comparable (implements Comparable<Tuple>)
- They implement equals() and hashCode()
- They also implement toString()
Class Declaration
public final class Unit<A> extends Tuple implements IValue0<A>
Class hierarchy
Object
↳ org.javatuples.Tuple
↳ org.javatuples.Unit<A>
Creating Unit Tuple
-
From Constructor:
Syntax:
Unit<A> unit = new Unit<A>(value);
Example:
// Below is a Java program to create// a Unit tuple from Constructorimportjava.util.*;importorg.javatuples.Unit;classGfG {publicstaticvoidmain(String[] args){Unit<String> unit=newUnit<String>("GeeksforGeeks");System.out.println(unit);}}chevron_rightfilter_noneOutput:
[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:
// Below is a Java program to create// a Unit tuple from with() methodimportjava.util.*;importorg.javatuples.Unit;classGfG {publicstaticvoidmain(String[] args){Unit<String> unit= Unit.with("GeeksforGeeks");System.out.println(unit);}}chevron_rightfilter_noneOutput:
[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:
// Below is a Java program to create// a Unit tuple from Collectionimportjava.util.*;importorg.javatuples.Unit;classGfG {publicstaticvoidmain(String[] args){// Creating Unit from ListList<String> list =newArrayList<String>();list.add("GeeksforGeeks");Unit<String> unit= Unit.fromCollection(list);// Creating Unit from ArrayString[] arr = {"A computer portal"};Unit<String> otherUnit= Unit.fromArray(arr);System.out.println(unit);System.out.println(otherUnit);}}chevron_rightfilter_noneOutput:
[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:
// Below is a Java program to get // a Unit value import java.util.*; import org.javatuples.Unit; class GfG { public static void main(String[] args) { Unit<String> unit = Unit.with("GeeksforGeeks"); System.out.println(unit); } } |
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:
// Below is a Java program to set // a Unit value import java.util.*; import org.javatuples.Unit; class GfG { public static void main(String[] args) { Unit<String> unit = Unit.with("GeeksforGeeks"); Unit<String> otherUnit = unit.setAt0("A computer portal"); System.out.println(otherUnit); } } |
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 element one more than the called Tuple.
Syntax:
Unit<type1> unit = new Unit<type 1>(value); Pair<type1, type2> pair = unit.addAt1(value2);
Example:
// Below is a Java program to add // a value import java.util.*; import org.javatuples.Unit; import org.javatuples.Pair; class GfG { public static void main(String[] args) { Unit<String> unit = Unit.with("GeeksforGeeks"); Pair<String, String> pair = unit.addAt1("A computer portal"); System.out.println(pair); } } |
Output:
[GeeksforGeeks, A computer portal]
Searching in a Tuple
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:
// Below is a Java program to search a value import java.util.*; import org.javatuples.Unit; class GfG { public static void main(String[] args) { Unit<String> unit = Unit.with("GeeksforGeeks"); boolean exist = unit.contains("GeeksforGeeks"); boolean exist1 = unit.contains(4); System.out.println(exist); System.out.println(exist1); } } |
Output:
true false
Iterating through Unit
Since Unit implement 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:
// Below is a Java program to get // a Unit value import java.util.*; import org.javatuples.Unit; class GfG { public static void main(String[] args) { Unit<String> unit = Unit.with("GeeksforGeeks"); for (Object item : unit) System.out.println(item); } } |
Output:
GeeksforGeeks
Recommended Posts:
- Implement Pair Class with Unit Class in Java using JavaTuples
- Implement Quartet Class with Triplet Class in Java using JavaTuples
- Implement Triplet Class with Pair Class in Java using JavaTuples
- Implement Quintet Class with Quartet Class in Java using JavaTuples
- Implement Sextet Class from Quintet Class in Java using JavaTuples
- Implement Septet Class from Sextet Class in Java using JavaTuples
- Implement Ennead Class from Octet Class in Java using JavaTuples
- Implement Octet Class from Septet Class in Java using JavaTuples
- Implement Decade Class from Ennead Class in Java using JavaTuples
- Octet Class in JavaTuples
- KeyValue Class in JavaTuples
- Decade Class in JavaTuples
- LabelValue Class in JavaTuples
- Septet Class in JavaTuples
- Triplet Class in JavaTuples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



