Open In App

Implement Pair Class with Unit Class in Java using JavaTuples

Last Updated : 11 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

To implement a Pair class with a Unit class in Java using JavaTuples, you can use the Pair class provided by the library and create a new Unit class that extends the Unit class provided by the library.

Here is an example implementation:

Here is an example Java code that uses the MyPair class with MyUnit and displays the output:

Java
import org.javatuples.Pair;
import org.javatuples.Unit;

public class Main {
    
    public static void main(String[] args) {
        
        MyPair<String, Integer> pair1 = new MyPair<>("John", 42);
        MyPair<MyUnit, Integer> pair2 = new MyPair<>(new MyPair.MyUnit(), 10);
        
        System.out.println("Pair 1: " + pair1);
        System.out.println("Pair 2: " + pair2);
        
        String name = pair1.getValue0();
        int age = pair1.getValue1();
        
        System.out.println(name + " is " + age + " years old.");
    }
    
    public static class MyPair<A, B> extends Pair<A, B> {
        
        public MyPair(A a, B b) {
            super(a, b);
        }
        
        public static class MyUnit extends Unit {
            
            public MyUnit() {
                super();
            }
        }
    }
}

Output:

Pair 1: (John, 42)
Pair 2: (( ), 10)
John is 42 years old.
 

A Pair is a Tuple from the JavaTuples library that deals with 2 elements. Since this Pair is a generic class, it can hold any type of value in it. A Unit is a Tuple from the JavaTuples library that deals with only 1 element. Since this Unit is a generic class, it can hold any type of value in it.

Different Ways to implement Pair Class with Unit Class

  • Using direct values
  • Using add() method of Unit class 
  • Using addAtX() method of Unit class

Method 1: Using Direct Values 

Example:

Java
// Java Program to Implement Pair 
// Class with Unit Class
// Using JavaTuples Via Direct Values

// Importing required classes
import java.util.*;
import org.javatuples.*;

// Main class
class GFG {

    // Main driver method
    public static void main(String[] args)
    {

        // Creating a Unit
        Unit<String> unit
            = new Unit<String>("GeeksforGeeks");

        // Printing unit
        System.out.println("Unit: " + unit);

        // Creating Pair from Unit
        Pair<Integer, String> pair
            = new Pair<Integer, String>(Integer.valueOf(1),
                                        unit.getValue0());

        // Printing the Pair
        System.out.println("Pair: " + pair);
    }
}

Output: 

Unit: [GeeksforGeeks]
Pair: [1, GeeksforGeeks]

Method 2: Using add() Method of Unit class 

Example:

Java
// Java Program to Implement Pair Class with Unit Class
// use of add() method with Single Value

// Importing required classes
import java.util.*;
import org.javatuples.*;

// Main class
class GFG {

    // Main driver method
    public static void main(String[] args)
    {

        // Instantiate unit object
        // using with() method
        Unit<String> unit = Unit.with("Geeks");

        // Printing unit
        System.out.println("Unit: " + unit);

        // Create Pair Using add() method
        Pair<String, String> pair = unit.add("forGeeks");

        // Print the Pair
        System.out.println("Pair: " + pair);
    }
}

Output: 

Unit: [Geeks]
Pair: [Geeks, forGeeks]

Method 3: Using addAtX() method of Unit class  

Example 1: Adding Unit at Position 0 using addAt0()

Java
// Java program to Implement Pair Class with Unit Class
// By using of addAt0() method with Direct Value

// Importing required classes
import java.util.*;
import org.javatuples.Pair;
import org.javatuples.Unit;

// Main class
class GFG {

    // Main driver method
    public static void main(String[] args)
    {

        // Instantiate unit object
        // using with() method
        Unit<String> unit = Unit.with("Geeks");

        // Printing unit
        System.out.println("Unit: " + unit);

        // Creating Pair using addAtX() method
        Pair<String, String> pair = unit.addAt0("forGeeks");

        // Printing the Pair
        System.out.println("Pair: " + pair);
    }
}

Output: 

Unit: [Geeks]
Pair: [forGeeks, Geeks]

Example 2: Adding Unit at Position 1 using addAt1()

Java
// Java program to Implement 
// Pair Class with Unit Class
// Via addAt1() method with 
// Direct Value

// Importing required classes
import java.util.*;
import org.javatuples.Pair;
import org.javatuples.Unit;

// Main class
class GFG {

    // Main driver method
    public static void main(String[] args)
    {
        // Instantiate unit object
        // using with() method
        Unit<String> unit = Unit.with("Geeks");

        // Printing Unit
        System.out.println("Unit: " + unit);

        // Creating Pair
        // Using addAtX() method
        Pair<String, String> pair = unit.addAt1("forGeeks");

        // Printing the Pair
        System.out.println("Pair: " + pair);
    }
}

Output: 

Unit: [Geeks]
Pair: [Geeks, forGeeks]

Article Tags :
Practice Tags :

Similar Reads