Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tuple (unmodifiable ordered list of heterogeneous elements) support in Java

Tags:

java

tuples

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).

like image 272
Oleksandr Karaberov Avatar asked Dec 20 '12 17:12

Oleksandr Karaberov


People also ask

What is heterogeneous List in Java?

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.

What is List and tuple in Java?

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.

What is a tuple in Java?

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.

Why are there no tuples in Java?

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.


2 Answers

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.

like image 146
yshavit Avatar answered Oct 13 '22 22:10

yshavit


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)

like image 28
Display Name Avatar answered Oct 14 '22 00:10

Display Name