Implement Quartet Class with Triplet Class in Java using JavaTuples

Java 8Object Oriented ProgrammingProgramming

Following is an example to implement Quartet class from Triplet class in Java −

Example

import org.javatuples.Triplet;
import org.javatuples.Quartet;
public class MyDemo {
   public static void main(String[] args) {
      Triplet<String, String, String>triplet = new Triplet<String, String, String>("Gray", "Blue", "Gray100");
      System.out.println("Triplet elements = " + triplet);
      Quartet<String, String, String, String>quartet = triplet.add("Blue30");
      System.out.println("Quartet (Implemented from Triplet) = " + quartet);
   }
}

Output

Triplet elements = [Gray, Blue, Gray100]
Quartet (Implemented from Triplet) = [Gray, Blue, Gray100, Blue30]

Let us now see another example to implement Quartet class from Triplet. This inserts element at position 3rd −

Example

import org.javatuples.Triplet;
import org.javatuples.Quartet;
public class MyDemo {
   public static void main(String[] args) {
      Triplet<String, String, String>triplet = new Triplet<String, String, String>("Gray", "Blue", "Gray100");
      System.out.println("Triplet elements = " + triplet);
      Quartet<String, String, String, String>quartet = triplet.addAt2("Blue30");
      System.out.println("Quartet (Implemented from Triplet) = " + quartet);
   }
}

Output

Triplet elements = [Gray, Blue, Gray100]
Quartet (Implemented from Triplet) = [Gray, Blue, Blue30, Gray100]
raja
Published on 24-Sep-2019 07:15:09
Advertisements