Implementing Pair Using Unit Class

Implementing Pair Using Unit Class

This topic is about Implementing Pair Using Unit Class.

Problem Description

How to implement Pair class using Unit class?

Example

Following example shows how to accomplish the above task. Each tuple has add() and addAtX() methods to convert the tuple.

Create a java class file named TupleTester in C:\>JavaTuples.

File: TupleTester.java

package com.Adglob;
import org.javatuples.Pair;
import org.javatuples.Unit;
public class TupleTester {
   public static void main(String args[]){
      Unit<Integer> unit = Unit.with(5);
      System.out.println(unit);
      Pair<Integer, String> pair = unit.add("test");
      Pair<String, Integer> pair1 = unit.addAt0("test");
      System.out.println(pair);
      System.out.println(pair1);
   }
}

Verify the result

Compile the classes using javac compiler as follows −

C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/Adglob/TupleTester.java

Now run the TupleTester to see the result −

C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.Adglob.TupleTester

Output

Verify the Output

[5]
[5, test]
[test, 5]

In this topic we learned about Implementing Pair Using Unit Class. To know more, Click Here.

This Post Has 2 Comments

Leave a Reply