My Hashtable in Java would benefit from a value having a tuple structure. What data structure can I use in Java to do that?
Hastable<Long, Tuple<Set<Long>,Set<Long>>> table = ...
My Hashtable in Java would benefit from a value having a tuple structure. What data structure can I use in Java to do that?
Hastable<Long, Tuple<Set<Long>,Set<Long>>> table = ...
I don't think there is a general purpose tuple class in Java but a custom one might be as easy as the following:
public class Tuple<X, Y> {
private X x;
private Y y;
public Tuple(X x, Y y) {
this.x = x;
this.y = y;
}
public X getX() {
return this.x;
}
public Y getY() {
return this.y;
}
}
Of course, there are some important implications of how to design this class further regarding equality, immutability, etc., especially if you plan to use instances as keys for hashing.
Create a class that describes the concept you're actually modeling and use that. It can just store two Set<Long>
and provide accessors for them, but it should be named to indicate what exactly each of those sets is and why they're grouped together.
Here's this exact same question elsewhere, that includes a more robust equals
, hash
that maerics alludes to:
That discussion goes on to mirror the maerics vs ColinD approaches of "should I re-use a class Tuple with an unspecific name, or make a new class with specific names each time I encounter this situation". Years ago I was in the latter camp; I've evolved into supporting the former.