Why isn't there a (standard, Java certified) solution, as part of the Java language itself, to return multiple values from a Java method, rather than developers having to use their own means, such as Maps, Lists, Pairs, etc.? Why does Java not support n-tuple objects?
Especially thinking for trivial private methods that may modify two objects together (in tandem), and in which case a typed-object as a return sounds overkill.
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.
In Python, you can return multiple values by simply return them separated by commas. In Python, comma-separated values are considered tuples without parentheses, except where required by syntax. For this reason, the function in the above example returns a tuple with each value as an element.
You can return only one value in Java. If needed you can return multiple values using array or an object.
JavaScript doesn't support functions that return multiple values. However, you can wrap multiple values into an array or an object and return the array or the object. Use destructuring assignment syntax to unpack values from the array, or properties from objects.
I assume the OP means "Why does Java not support n-tuple objects?". Python, Haskell, Lisp, ML etc have heterogeneous n-tuple capabilities. Also often times the ability to apparently return multiple objects in a language is syntactical sugar (ie in python return 'a','b').
The reason of course is language design and consistency. Java prefers being very explicit and does not like anonymous data structures (although I wish we had anonymous closures).
For example in Java there is no way to say I would like a callback that takes these type parameters and returns this. Some people feel this a huge weakness others like the consistency and explicitness.
IMHO although its annoying I frequently combat this issue by making static inline classes:
private static class Combo { String name; int weight; }
Yes its tedious but then later on I often reuse and refactor those classes making them top level and adding behavior. Infact one of the advantages with going this route is that its much easier to add new fields where is the anonymous data structure (like in FP languages) it becomes much more difficult to add a field (you end up changing a ton of code).
I should note that for 2-tuples some people use (or abuse) java.util.Map.Entry
as there is an java.util.AbstractMap.SimpleEntry
in Java 6. Also Some people now use Commons Lang3's Pair support (2-tuple).
Scala has n-tuple support by sort of cheating and having a whole bunch of 2-16 tuple interfaces that are standard in the language and are syntactically hidden from the programmer.
For purely educational reasons you may want to see how other languages accomplish this.
UPDATE: for Java 8
Java 8 will/maybe (so heres my number... call me maybe) support an interface called java.lang.BiValue
with a concrete implementation that you can use called java.lang.BiVal
. These classes are to help support the new lambda functionality. But notice this is only for 2-tuples.
UPDATE: for 2015
Java 8 did not gain support for tuples.
UPDATE: from author 2015
If you still would like tuple support there are three libraries that support tuples well:
Java methods return exactly zero or one value; that is the standard for java. If you need multiple values returned, create an object with the multiple values and return it.
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