Implement Pair Class with Unit Class in Java using JavaTuples
Last Updated :
17 Mar, 2023
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
import java.util.*;
import org.javatuples.*;
class GFG {
public static void main(String[] args)
{
Unit<String> unit
= new Unit<String>( "GeeksforGeeks" );
System.out.println( "Unit: " + unit);
Pair<Integer, String> pair
= new Pair<Integer, String>(Integer.valueOf( 1 ),
unit.getValue0());
System.out.println( "Pair: " + pair);
}
}
|
Output:
Unit: [GeeksforGeeks]
Pair: [1, GeeksforGeeks]
Method 2: Using add() Method of Unit class
Example:
Java
import java.util.*;
import org.javatuples.*;
class GFG {
public static void main(String[] args)
{
Unit<String> unit = Unit.with( "Geeks" );
System.out.println( "Unit: " + unit);
Pair<String, String> pair = unit.add( "forGeeks" );
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
import java.util.*;
import org.javatuples.Pair;
import org.javatuples.Unit;
class GFG {
public static void main(String[] args)
{
Unit<String> unit = Unit.with( "Geeks" );
System.out.println( "Unit: " + unit);
Pair<String, String> pair = unit.addAt0( "forGeeks" );
System.out.println( "Pair: " + pair);
}
}
|
Output:
Unit: [Geeks]
Pair: [forGeeks, Geeks]
Example 2: Adding Unit at Position 1 using addAt1()
Java
import java.util.*;
import org.javatuples.Pair;
import org.javatuples.Unit;
class GFG {
public static void main(String[] args)
{
Unit<String> unit = Unit.with( "Geeks" );
System.out.println( "Unit: " + unit);
Pair<String, String> pair = unit.addAt1( "forGeeks" );
System.out.println( "Pair: " + pair);
}
}
|
Output:
Unit: [Geeks]
Pair: [Geeks, forGeeks]
Share your thoughts in the comments
Please Login to comment...