Open In App

Unit Class in JavaTuples

Improve
Improve
Like Article
Like
Save
Share
Report

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.

Here is an example of how to create and use a Unit object in JavaTuples:

Java




import org.javatuples.Unit;
 
public class Main {
     
    public static void main(String[] args) {
         
        Unit unit = new Unit();
         
        System.out.println("Unit size: " + unit.getSize());
        System.out.println("Unit values: " + unit);
    }
}


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:

Advantages:

  1. Unit objects can be used in place of tuples with one or more values when you don’t actually need any values.
  2. Using a Unit object can help make your code more readable and self-documenting, since it is clear that there are no values associated with the tuple.

Disadvantages:

  1. If you are using Unit objects in a collection or as a return value, it may not be immediately clear to other developers what the purpose of the empty tuple is.
  2. If you are not careful, you may accidentally pass a Unit object to a method that expects a tuple with one or more values, which could cause unexpected behavior.
    Overall, the Unit class is a useful tool when you need to represent an empty tuple, but it should be used judiciously and with care.

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.

Here is an example of using the Unit class:

Java




import org.javatuples.Unit;
 
public class UnitExample {
    public static void main(String[] args) {
        Unit<String> unit = new Unit<>("Hello");
        System.out.println(unit.getValue()); // "Hello"
        System.out.println(unit.toString()); // "(Hello)"
    }
}


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: 

  • 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

Java




// 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:  

Java




// 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

Java




// 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: 

Java




// 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

Java




// 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 elements one more than the called Tuple.

Syntax

Unit<type1> unit = new Unit<type 1>(value);
Pair<type1, type2> pair = unit.addAt1(value2);

Example

Java




// 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:  

Java




// 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 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

Java




// 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


Last Updated : 14 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads