Implement Pair Class with Unit Class in Java using JavaTuples
Deploying JavaTuples means users can implement a pair class with a unit class in Java by utilizing the pair class that comes with the library and by creating a new unit class that extends the library's unit class.
A Pair, a Tuple from the JavaTuples package, handles two different elements. This Pair is a generic class, so it can contain any value. A Unit, a Tuple from the JavaTuples package, handles one element. It can store any value.
Various Approaches to Unit Class and Pair Class Implementation:
- By using direct values
- By using add() method of the Unit class
- By using addAtX() method of the Unit class
Approach: By using direct values
The algorithm used for this technique initializes a Unit instance with the string value "Hello World" and then displays it. Then, it creates a Pair instance with an integer value of 1 and the Unit instance's initial value; however, it accesses an undefined variable unit rather than u, leading to a compilation error.
With the update, the text "Hello World" and the numeric value 1 would be correctly created as a Pair instance. The constructed Pair instance will be printed out in the final step.
Implementation:
FileName: PairClassWithDirectvalues.java
import java.util.*; import java.io.*; import org.javatuples.*; public class PairClassWithDirectvalues { public static void main(String[] args) { // Creation of a Unit Unitu = new Unit ("Hello World "); // displaying the initially created unit System.out.println("The given Unit: " + u); // Creation of a Pair from a Unit Pair p = new Pair (Integer.valueOf(1),u.getValue0()); // displaying the Pair System.out.println("The Pair class is: " + p); } }
Output:
The given Unit: [Hello World] The Pair class is : [1, Hello World]
Approach: By using direct values
The technique that is used below demonstrates how to use the Pair and Unit classes from the org.javatuples package. Using the Unit.with() method, it first creates a Unit instance with the string "Hello" and outputs it. Subsequently, it creates a Pair by attaching the text "World" to the Unit's existing value using the add() method of the Unit instance. Lastly, it prints the Pair instance that was formed, with the strings "Hello" and "World" making up its elements.
Implementation:
FileName: AddMethodPairClass.java
import java.util.*; import java.io.*; import org.javatuples.*; public class AddMethodPairClass { public static void main(String[] args) { // Create a unit object by // utilising the with() function. Unitu = Unit.with("Hello "); // displaying the unit System.out.println("The given Unit: " + u); // Create a pair using the add() function. Pair p = u.add("World"); // displaying the Pair System.out.println("The Pair class is: " + p); } }
Output:
The given Unit: [Hello] The Pair class is: [Hello, World]
Approach: By using addAtX() method of the Unit class
The Unit and Pair classes obtained from the org.javatuples package are used in this approach implementation. First, it uses the Unit.with() function to create a Unit instance with the text "Hello" and generate it. The string "World" is then inserted at the 0th place or any place, essentially prepending it to the current value of the Unit, using the addAt0() method of the Unit instance to construct a Pair. Finally, it puts out the Pair instance that was produced, with the strings "World" and "Hello" forming up its members.
Implementation:
FileName: AddAtXPublicclass.java
import java.util.*; import java.io.*; import org.javatuples.Pair; import org.javatuples.Unit; public class AddAtXPublicclass { public static void main(String[] args) { // Create a unit object by // utilising the with() function. Unitu = Unit.with("Hello"); // displaying unit System.out.println("The given Unit: " + u); // Using the addAtX() method, create a pair Pair p = u.addAt0("World"); // displaying the Pair System.out.println("The Pair class is: " + p); } }
Output:
The given Unit: [Hello]
The Pair class is: [World, Hello]