Implement Triplet Class with Pair Class in Java using JavaTuples

Java 8Object Oriented ProgrammingProgramming

At first, create a Pair class −

Pair<Integer, String>pair = new Pair<Integer, String>(Integer.valueOf(25), "green");

Now, implement Triplet class with Pair class −

Triplet<Integer, String, String>
triplet = pair.add("magenta");

Following is an example to implement Triplet class with Pair class in Java −

Example

import org.javatuples.Pair;
import org.javatuples.Triplet;
public class MyDemo {
   public static void main(String[] args) {
      Pair<Integer, String>pair = new Pair<Integer, String>(Integer.valueOf(25), "green");
      System.out.println("Pair class = " + pair);
      Triplet<Integer, String, String>triplet = pair.add("magenta");
      System.out.println("Triplet class elements (Implemented from Pair class) = " + triplet);
   }
}

Output

Pair class = [25, green]
Triplet class elements (Implemented from Pair class) = [25, green, magenta]
raja
Published on 20-Sep-2019 07:45:59
Advertisements