Triplet class in Java
This post will discuss how to implement a Triplet class in Java that can store three values of any type, using the Pair class provided by the Apache Commons Lang library.
A Triplet class is a class that can store three objects of different types, that can be unrelated to each other. A triplet is a useful data structure but it is not included in the standard Java Development Kit (JDK). Many Java developers wish they had this class at their disposal. We have already learned how to create a custom Triplet class in Java that can store three values of any type. In this post, we will see how to use the Pair class from the Apache Commons Lang library to implement the Triplet class in a simple way. For example, one possible implementation of the Triplet class could be:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
import org.apache.commons.lang3.tuple.Pair; class Triplet<L, M, R> { // private field to hold the triplet private final Pair<L, Pair<M, R>> pair; // Create a triplet from specified typed params public Triplet(L left, M mid, R right) { pair = Pair.of(left, Pair.of(mid, right)); } // return left element of the triplet public L getLeft() { return pair.getKey(); } // return mid-element of the triplet public M getMiddle() { return pair.getValue().getKey(); } // Return right element of the triplet public R getRight() { return pair.getValue().getValue(); } // override the toString() method to return a string representation of the triplet @Override public String toString() { return "(" + getLeft() + "," + getMiddle() + "," + getRight() + ")"; } } class Main { public static void main(String[] args) { Triplet <String, Integer, Character> triplet = new Triplet<>("ASCII", 65, 'A'); // ASCII, 65, A System.out.println(triplet.getLeft() + ", " + triplet.getMiddle()+ ", " + triplet.getRight()); } } |
The above implementation is a little verbose. Following is the simplified version that uses MutablePair class from Apache Commons Lang. which can store two mutable values. The idea is to extend the MutablePair class and add an extra field for the middle element. We also need to provide getter and setter methods for this field, as well as override the equals, hashCode, and toString methods. For instance:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
import org.apache.commons.lang3.tuple.MutablePair; // Create custom Triplet class by extending MutablePair class Triplet<L, M, R> extends MutablePair<L, R> { // the middle element of the triplet private M middle; // constructor with three arguments public Triplet(L left, M middle, R right) { super(left, right); // call the super constructor with two arguments this.middle = middle; // set the middle element } // getter for the middle element public M getMiddle() { return middle; } // setter for the middle element public void setMiddle(M middle) { this.middle = middle; } // override the toString() method to return a string representation of the triplet @Override public String toString() { return "(" + getLeft() + "," + getMiddle() + "," + getRight() + ")"; } // override equals and hashcode } class Main { public static void main(String[] args) { Triplet<String, Integer, Character> triplet = new Triplet<>("ASCII", 65, 'A'); // ASCII, 65, A System.out.println(triplet.getLeft() + ", " + triplet.getMiddle() + ", " + triplet.getRight()); System.out.println(triplet); // (ASCII,65,A) } } |
That’s all about implementating Triplet class using Pair class in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)