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 Constructor
import
java.util.*;
import
org.javatuples.Unit;
class
GfG {
public
static
void
main(String[] args)
{
Unit<String> unit
=
new
Unit<String>(
"GeeksforGeeks"
);
System.out.println(unit);
}
}
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:
// Below is a Java program to create
// a Unit tuple from with() method
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]
- 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 Collection
import
java.util.*;
import
org.javatuples.Unit;
class
GfG {
public
static
void
main(String[] args)
{
// Creating Unit from List
List<String> list =
new
ArrayList<String>();
list.add(
"GeeksforGeeks"
);
Unit<String> unit
= Unit.fromCollection(list);
// Creating Unit from Array
String[] arr = {
"A computer portal"
};
Unit<String> otherUnit
= Unit.fromArray(arr);
System.out.println(unit);
System.out.println(otherUnit);
}
}
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:
// 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
leave a comment
0 Comments