Implement Triplet class (Or a 3-Tuple) in Java
Here, we will know how to implement our own Triplet class in Java.
Actually, A Triplet is a container to store a triplet of three objects. Since JDK doesn’t provides any implementation of the Triplet class, some programmers often miss this class in Java. Here, we will see how to implement our own Triplet class in Java which we can be easily customized to suit in your project.
Writing a Triplet class is actually very simple in Java. Below is simple implementation of custom Triplet class in Java which has:
- Three public fields – first, second and third
- A private constructor
- Overridden toString() method to print the Triplet instance
- Overridden hashCode() and equals() methods to ensure the desired behavior in hash-based collections.
- Finally, a static factory method of() for creating a Typed and immutable Triplet instance which internally calls the private constructor.
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; // first field of a Triplet
public final V second; // second field of a Triplet
public final T third; // 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" 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);
}
}
// Implementing Triplet Class in Java
class MainClass
{
public static void main(String[] args)
{
Triplet<String, Integer, Character> t1 = Triplet.of("James", 36, 'M');
Triplet<String, Integer, Character> t2 = Triplet.of("John", 20, 'F');
Triplet<String, Integer, Character> t3 = Triplet.of("James", 36, 'M');
List<Triplet<String, Integer, Character>> pairs = new ArrayList<>();
pairs.add(t1);
pairs.add(t2);
pairs.add(t3);
System.out.println(pairs);
Set<Triplet<String, Integer, Character>>
distinctTriplets = new HashSet<>(pairs);
System.out.println(distinctTriplets);
}
}
C:\>javac MainClass.java
C:\>java MainClass
[(James, 36, M), (John, 20, F), (James, 36, M)]
[(John, 20, F), (James, 36, M)]
Recent Comments