Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Sometimes You Need Tuples in Java. Or Not

DZone's Guide to

Sometimes You Need Tuples in Java. Or Not

· Java Zone
Free Resource

Try Okta to add social login, MFA, and OpenID Connect support to your Java app in minutes. Create a free developer account today and never build auth again.

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

Build and launch faster with Okta’s user management API. Register today for the free forever developer edition!

Topics:

Published at DZone with permission of Peter Verhas, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}