Skip to content
Related Articles

Related Articles

Implement Pair Class with Unit Class in Java using JavaTuples

Improve Article
Save Article
  • Last Updated : 18 Apr, 2022
Improve Article
Save Article

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]

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!