Sometimes you need tuples in Java. Or not.
April 22, 2015
Posted by on
Tuple is an ordered list of elements. In Java that is List<Object>. Even though it exists there is an extra need from programmers for tuples. You can see that there is a package named javatuples that defines tuples that contain 1, 2 up to 10 elements. (Btw: There is a class in the package named Unit that contains one element. WAT?) There is a long discussion on stackoverflow about tuples.
But where does it come from? Why do some Java programmers long for tuples? The answer is that tuples are part of the language constructs of other languages. They date back to such old ages that only program archeologist can remember. Languages like LISP use tuples. Python is also lurking here from the last century. Why did they implement a feature like tuples? Perhaps it seemed to be a good idea. If it was not coming from the past, Java developer would not long for it. Which itself is a hint: do you really need it? But the fact is fact:
Java misses tuples. THIS IS A LIE!
Which is not true for two reasons:
- There is no need for tuples.
- There is a built in type in Java that can handle tuple
There is an interface named java.util.Map.Entry that is there just to hold two objects and there is a simple implementation java.util.AbstractMap.SimpleEntry. Thus Java does not misses tuples and neither do I.