Create Unit Tuple in Java



Java does not have any built-in tuple support by default. To use the tuple, we use the third-party library called Javatuples. Using this library, you can create a tuple of different sizes, starting from a single-element tuple, which is the Unit class, up to a ten-element tuple (Decade class).

The following is the list of JavaTuples library classes:

  • Unit: 1 element tuple
  • Pair: 2 element tuple
  • Triplet: 3 element tuple
  • Quartet: 4 element tuple
  • Quintet: 5 element tuple
  • Sextet: 6 element tuple
  • Septet: 7 element tuple
  • Octet: 8 element tuple
  • Ennead: 9 element tuple
  • Decade: 10 element tuple

Java Unit Class

The Unit class is one of the classes from the JavaTuples library. It is a fixed-size tuple that holds 1 value. When we add a value to a unit tuple, it becomes a Pair (a tuple that holds 2 values).

Constructor of the Unit tuple class

Below is the declaration of the Unit tuple class:

Unit(A value0)

Different ways of creating unit tuple in Java

Below are the different ways of creating unit tuple in Java:

Using Constructor

We can directly create a Unit tuple by passing a value to its constructor.

Unit u = new Unit("Welcome, to TutorialsPoint!");

Below is a Java program to create a Unit tuple using its constructor:

import org.javatuples.Unit;
public class Demo {
   public static void main(String[] args) {
      Unit u = new Unit("This is demo text!");
      System.out.println(u);
   }
}

Output

[This is demo text!]

Using List in Java

The List is an ordered collection in Java that allows duplicate elements. We can import it from java.util package and is part of the Collection framework

Below is a Java program to create a Unit tuple using List:

import java.util.*;
import org.javatuples.Unit;
public class Demo {
   public static void main(String[] args) {
      List  myList = new ArrayList  ();
      myList.add("This is it!");
      Unit  u = Unit.fromCollection(myList);
      System.out.println(u);
   }
}

In the above program, we are adding a single value by using List, and to convert this list into a Unit tuple, we used Unit.fromCollection(myList) method.

Output

[This is it!]

Using with() method

The with() method is used to create Unit tuples easily in Java, as it is a static utility method belonging to the Unit class.

Unit u = Unit.with("TuorialsPoint");

Below is a Java program to create a Unit tuple using with() method:

import org.javatuples.Unit;
public class Demo {
   public static void main(String[] args) {
      Unit u = Unit.with("Bingo!");
      System.out.println(u);
   }
}

Output

[Bingo!]

Using another collection in Java

To create a Unit tuple in Java, we will use the fromCollection() method is a static method that is used to create a Unit object from a Java collection. It takes the elements from Java collections and creates a tuple from them.

The fromCollection() method belongs to the unit tuple class. If the size of the collection is 1, then it will create a tuple of size 1 is not 1; it throws an IllegalArgumentException.

In the example given below, we are using the set:

import java.util.*;
import org.javatuples.Unit;

public class Demo {
   public static void main(String[] args) {
      Set mySet = new HashSet();
      mySet.add("This is from Set!");
      
      Unit u = Unit.fromCollection(mySet);
      System.out.println(u);
   }
}

Using arrays in Java

We are using the fromArray() method provided by the JavaTuples library.

fromArray() method: It accepts an array and converts it into a tuple.

Below is a Java program to create a Unit tuple using arrays:

import java.util.*;
import org.javatuples.Unit;
public class Demo {
   public static void main(String[] args) {
      String[] str = {"Tom Hanks!"};
      Unit  u = Unit.fromArray(str);
      System.out.println(u);
   }
}

Output

[This is it!]
Updated on: 2019-07-30T22:30:25+05:30

171 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements