I wonder why Java doesn’t have a tuple data structure implementation in its standard library. For instance C++ has a very good implementation of this fixed-size collection of heterogeneous values. The same in Haskell. In Java I only know javatuples and some support in Functional Java library via Product (P1 - P8) types. I wonder why tuple or at least pair not in standard library at all? Even Android SDK developers added their own implementation of 2-tuple (pair).
I wonder why Java doesn’t have a tuple data structure implementation in its standard
Share
The “Java way” is to define use-specific classes rather than these sorts of lightweight semi-class types. If you think about it, a tuple is really just a simplified struct; the Java folks would prefer you to just go ahead and create the struct.
This perspective is changing a bit, especially in Java 8 with its lambdas (which put pressure on the JDK to provide generic
Function-type interfaces rather than use-case-specific interfaces likeFooCallback). But it’s still a fairly strong mindset for a lot of Java developers, and there’s some sense to it. Java is a very statically typed language; a tuple is somewhere between static typing and dynamic typing, in that there’s nothing in the type system to prevent you from thinking this(int, String)that represents a customer ID and name is actually an(int, String)representing an order ID and its description.See, for instance, this discussion (“Tuples for n >= 2”) on the issue within the Guava project. Granted, that’s not official; but it’s a good representation of the Java mindset.