Implement Triplet class (or a 3-Tuple) in Java
This post will discuss how to implement our own Triplet class in Java.
A Triplet is a container to store a triplet of three objects. Since JDK doesn’t implement the Triplet class, some programmers often miss this class in Java. This post will discuss how to implement our own Triplet class in Java, which can be easily customized to suit our style.
Writing a Triplet class is actually very simple in Java. Following is a simple implementation of the custom Triplet class in Java, which has the following fields and methods:
- Three public fields –
first,secondandthird. - A private constructor.
- Overridden hashCode() and equals() methods to ensure the desired behavior in hash-based collections.
- Overridden
toString()method to print theTripletinstance. - Finally, a static factory method
of()for creating a typed and immutableTripletinstance which internally calls the private constructor.
The following program demonstrates it:
|
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; // Triplet class class Triplet<U, V, T> { public final U first; // the first field of a triplet public final V second; // the second field of a triplet public final T third; // the third field of a triplet // Constructs a new triplet with the given values private Triplet(U first, V second, T third) { this.first = first; this.second = second; this.third = third; } @Override public boolean equals(Object o) { /* Checks specified object is "equal to" the current object or not */ if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } Triplet triplet = (Triplet) o; // call `equals()` method of the underlying objects if (!first.equals(triplet.first) || !second.equals(triplet.second) || !third.equals(triplet.third)) { return false; } return true; } @Override public int hashCode() { /* Computes hash code for an object by using hash codes of the underlying objects */ int result = first.hashCode(); result = 31 * result + second.hashCode(); result = 31 * result + third.hashCode(); return result; } @Override public String toString() { return "(" + first + ", " + second + ", " + third + ")"; } // Factory method for creating a typed immutable instance of triplet public static <U, V, T> Triplet <U, V, T> of(U a, V b, T c) { return new Triplet <>(a, b, c); } } // Program to implement Triplet class in Java class Main { public static void main(String[] args) { Triplet<String, Integer, Character> p1 = Triplet.of("David", 26, 'M'); Triplet<String, Integer, Character> p2 = Triplet.of("Lisa", 20, 'F'); Triplet<String, Integer, Character> p3 = Triplet.of("David", 26, 'M'); List<Triplet<String, Integer, Character>> pairs = new ArrayList<>(); pairs.add(p1); pairs.add(p2); pairs.add(p3); System.out.println(pairs); Set<Triplet<String, Integer, Character>> distinctTriplets = new HashSet<>(pairs); System.out.println(distinctTriplets); } } |
Output:
[(David, 26, M), (Lisa, 20, F), (David, 26, M)]
[(Lisa, 20, F), (David, 26, M)]
That’s all about Triplet class implementation 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 :)