Here's how to implement a pair-by-unit class:
1. Using direct values
import java.util.*; import org.javatuples.*; class GfG { public static void main(String[] args) { // Create a Unit Unit unit = new Unit("GeeksforGeeks"); // print unit System.out.println("Unit: " + unit); // create Pair from Unit Pair<Integer, String> pair = new Pair<Integer, String>( Integer.valueOf(1), unit.getValue0()); // Print the Pair System.out.println("Pair: " + pair); } }
Output:
Unit: [GeeksforGeeks] Pair: [1, GeeksforGeeks]
2. Using Unit.add() method
// Below is a Java program to demonstrate // use of add() method with // single value import java.util.*; import org.javatuples.*; class GfG { public static void main(String[] args) { // Using with() method to instantiate unit object Unit unit = Unit.with("Geeks"); // print unit System.out.println("Unit: " + unit); // Using add() to create Pair Pair<String, String> pair = unit.add("forGeeks"); // Print the Pair System.out.println("Pair: " + pair); } }
Output:
Unit: [Geeks] Pair: [Geeks, forGeeks]
3. Using Unit.addAtX() method
// Below is a Java program to demonstrate // use of addAt0() method with // direct value import java.util.*; import org.javatuples.Unit; import org.javatuples.Pair; class GfG { public static void main(String[] args) { // Using with() method to instantiate unit object Unit unit = Unit.with("Geeks"); // print unit System.out.println("Unit: " + unit); // Using addAtX() to create Pair Pair<String, String> pair = unit.addAt0("forGeeks"); // Print the Pair System.out.println("Pair: " + pair); } }
Output:
Unit: [Geeks] Pair: [forGeeks, Geeks]
// Below is a Java program to demonstrate // use of addAt1() method with // direct value import java.util.*; import org.javatuples.Unit; import org.javatuples.Pair; class GfG { public static void main(String[] args) { // Using with() method to instantiate unit object Unit unit = Unit.with("Geeks"); // print unit System.out.println("Unit: " + unit); // Using addAtX() to create Pair Pair<String, String> pair = unit.addAt1("forGeeks"); // Print the Pair System.out.println("Pair: " + pair); } }
Output:
Unit: [Geeks] Pair: [Geeks, forGeeks]