Implement Pair Class with Unit Class in Java using JavaTuples

Java 8Object Oriented ProgrammingProgramming

Following is an example to implement Pair class from Unit class in Java −

Example

import org.javatuples.Unit;
import org.javatuples.Pair;
public class MyDemo {
   public static void main(String[] args) {
      Unit<String> unit = Unit.with("Tutorial");
      System.out.println("Unit class element: " + unit);
      Pair<String, String> pair = unit.addAt0("Learning");
      System.out.println("Pair (Implemented from Unit Tuple): " + pair);
   }
}

Output

Unit class element: [Tutorial]
Pair (Implemented from Unit): [Learning, Tutorial]

Let us see another example wherein we will be implementing Pair class from Unit class −

Example

import org.javatuples.Unit;
import org.javatuples.Pair;
public class MyDemo {
   public static void main(String[] args) {
      Unit<String> unit = Unit.with("Tutorial");
      System.out.println("Unit class element = " + unit);
      Pair<String, String> pair = unit.addAt1("Learning");
      System.out.println("Pair Class (Implemented from Unit Tuple) = " + pair);
   }
}

Output

Unit class element = [Tutorial]
Pair Class (Implemented from Unit Tuple) = [Tutorial, Learning]
raja
Published on 20-Sep-2019 07:43:45
Advertisements