Java Deep

Pure Java, what else

Sometimes you need tuples in Java. Or not.

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:

  1. There is no need for tuples.
  2. 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.


4 responses to “Sometimes you need tuples in Java. Or not.

  1. Martin Grajcar April 22, 2015 at 10:13 pm

    I disagree a bit. There’s sometimes a need for a tuple, e.g., when you need to return two or more things and there’s no behavior to add. Fortunately, this is pretty rare and writing one more trivial class once per year is not that bad (especially when using project Lombok). Concerning


    , it’s half-immutable, which makes it rarely suitable. But all in all, having no tuples in Java is a good thing as it’d be surely overused.

    Liked by 1 person

  2. satyr April 23, 2015 at 4:40 pm

    I think that using java.util.AbstractMap.SimpleEntry is like demanding that Java developers would think outside the box (some of them would say or think – ‘duhh it’s from AbstractMap package, it’s too complicated, method can return only one object unless it’s a collection otherwise it’s a violation of OOP’). Furthermore as Martin mentioned it’s half-immutable.


    • Peter Verhas April 23, 2015 at 5:12 pm

      some of them would say or think – ‘duhh it’s from AbstractMap package…

      I would not call that person Java developer. Not even a junior Java developer. Inner classes are essential part of the language. Should be known.

      Liked by 1 person

  3. Homer October 24, 2016 at 6:32 pm

    Java methods are invoked with tuples. A tuple is a list with a fixed size and fixed type by index position. Entry can only replace a 2-tuple.

    If Java supported tuples, you could write:
    (String, String) parsePair(String stuff) { return (…, …); }

    The results could be used directly with method invocation:
    some.method(parsePair(stuff)); // where method(String x, String y);;

    Without support for tuples, you have to create a holder type (or hijack something like Entry):
    map.put(e.getKey(), e.getValue())


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: