Triplet Class in JavaTuples

JavaObject Oriented ProgrammingProgramming

A Triplet class is a Tuple of three elements. It is part of the JavaTuples library.

To work with Triplet class in JavaTuples, you need to import the following package −

import org.javatuples.Triplet;

Example

Let us see an example to implement the Triplet class −

import org.javatuples.Triplet;
public class Demo {
   public static void main(String[] args) {
      Triplet < String, String, String > t = new Triplet < String, String, String > ("One", "Two", "Three","Four", "Five");
      System.out.println(t);
   }
}

Output

[One, Two, Three, Four, Five]

Example

Let us see another example to get value from Triplet class −

import org.javatuples.Triplet;
public class Demo {
   public static void main(String[] args) {
      Triplet < String, String, String > t = new Triplet < String, String, String > ("One", "Two", "Three","Four", "Five");
      System.out.println(t);
      System.out.println("Get Value: " + t.getValue0());
   }
}

Output

[One, Two, Three, Four, Five]
Get Value: One

Example

Let us see another example to set the Triplet value in JavaTuples and a copy with a new value at the 1st index −

import org.javatuples.Triplet;
public class Demo {
   public static void main(String[] args) {
      Triplet < String, String, String > t1 = Triplet.with("Movies", "Web Series", "TV Shows");
      System.out.println(t1);
      Triplet < String, String, String > t2 = t1.setAt1("Songs");
      System.out.println(t2);
   }
}

Output

[Movies, Web Series, TV Shows]
[Movies, Songs, TV Shows]
raja
Published on 26-Sep-2019 12:43:41
Advertisements