This question already has an answer here:
- A Java collection of value pairs? (tuples?) 17 answers
My Hashtable in Java would benefit from a value having a tuple structure. What data structure can I use in Java to do that?
Hashtable<Long, Tuple<Set<Long>,Set<Long>>> table = ...
javatuples is a dedicated project for tuples in Java.
To supplement @maerics's answer, here is the
Comparable
tuple:Apache Commons provided some common java utilities including a Pair. It implements
Map.Entry
,Comparable
andSerializable
.If you are looking for a built-in Java two-element tuple, try
AbstractMap.SimpleEntry
.You can use Google Guava Table
Here's this exact same question elsewhere, that includes a more robust
equals
,hash
that maerics alludes to:http://groups.google.com/group/comp.lang.java.help/browse_thread/thread/f8b63fc645c1b487/1d94be050cfc249b
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.