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).
Normally, for such a purpose, we would use a heterogeneous list, which in Java is just the raw list type List<?> or List<Object> . Since every class in Java is a subclass of Object (and now that we have autoboxing), such a list can contain any Java value. There are many kinds of situation where this would be necessary.
Lists are designed to store elements of a single type. Out of all data structures, a tuple is considered to be the fastest, and they consume the least amount of memory. While array and list are mutable which means you can change their data value and modify their structures, a tuple is immutable.
Tuple. Tuple is a sequence of objects which may or may not be of same type. Consider the following example − [12,"TutorialsPoint", java.sql.Connection@li757b] Above object is a tuple of three elements, an Integer, a string and a Connection Object.
Tuple in Java The functionality of a tuple can be implemented using the List and Array data structure but these data structures do not hold different types of data types by design. Hence, it is clear that heterogeneous tuple using a standard data structure (List/ Array) is not possible in Java.
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 like FooCallback
). 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.
There is a nice library called javatuples
. It defines generic tuple types for arities from 1(Unit
) to 10(Decade
) and all essential methods like equals
, hashCode
, toString
and even compareTo
.
Official site: http://www.javatuples.org/
Maven dependency:
<dependency>
<groupId>org.javatuples</groupId>
<artifactId>javatuples</artifactId>
<version>[version]</version>
<scope>compile</scope>
</dependency>
(at the moment latest version is 1.2
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With