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 Prutor {
public static void main(String[] args)
{
Unit<String> unit
= new Unit<String>("PrutordotAi");
System.out.println(unit);
}
}
Output:
[PrutordotAi]
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 Prutor {
public static void main(String[] args)
{
Unit<String> unit
= Unit.with("PrutordotAi");
System.out.println(unit);
}
}
Output:
[PrutordotAi]
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 Prutor {
public static void main(String[] args)
{
// Creating Unit from List
List<String> list = new ArrayList<String>();
list.add("PrutordotAi");
Unit<String> unit
= Unit.fromCollection(list);
// Creating Unit from Array
String[] arr = { "A coding Lab" };
Unit<String> otherUnit
= Unit.fromArray(arr);
System.out.println(unit);
System.out.println(otherUnit);
}
}
Output:
[PrutordotAi]
[A coding Lab]
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 Prutor {
public static void main(String[] args)
{
Unit<String> unit
= Unit.with("PrutordotAi");
System.out.println(unit);
}
}
Output:
PrutordotAi
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 Prutor {
public static void main(String[] args)
{
Unit<String> unit
= Unit.with("PrutordotAi");
Unit<String> otherUnit
= unit.setAt0("A coding Lab");
System.out.println(otherUnit);
}
}
Output:
[A coding Lab]
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 Prutor {
public static void main(String[] args)
{
Unit<String> unit
= Unit.with("PrutordotAi");
Pair<String, String> pair
= unit.addAt1("A coding Lab");
System.out.println(pair);
}
}
Output:
[PrutordotAi, A coding Lab]
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 Prutor {
public static void main(String[] args)
{
Unit<String> unit
= Unit.with("PrutordotAi");
boolean exist = unit.contains("PrutordotAi");
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 Prutor {
public static void main(String[] args)
{
Unit<String> unit
= Unit.with("PrutordotAi");
for (Object item : unit)
System.out.println(item);
}
}
Output:
PrutordotAi