This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of Machine Learning with R: Expert techniques for predictive modeling and have Brett Lantz on-line!
See this thread for details.
Win a copy of Machine Learning with R: Expert techniques for predictive modeling this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Why does Java not have anything like Tuple of Scala?

 
Ranch Hand
Posts: 1258
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does Java not have anything like Tuple of Scala.  Example of Scala tuple : (word, 1).  We have array In Java but array elements can have only one type unlike Scala tuple which does lot require elements to have same type.

Thanks
 
Marshal
Posts: 65821
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is nothing stopping you from creating your own Tuple classes. But many people doubt the propriety of a Tuple class. Look here or here, where you will find people doubt whether a tuple class sits well with the concept of strict typing.
Please read those two threads carefully and see how much they help answer your question.
 
Marshal
Posts: 14060
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'd really have to ask the designers of the language that kind of question. Java was meant to be a general-purpose object-oriented language with one of its main goals being a relatively easy transition for programmers already familiar with C/C++. Since C/C++ don't have a built-in way to represent Tuples, it's no surprise that neither does Java.  

Edit: I'm not suggesting that's the reason though. There could have been any number of other considerations as the threads that Campbell cited demonstrate but again, you'd really have to ask someone who took part in the language design. It could very well be the case also that the idea of adding a Tuple class never occurred to the designers.
 
Saloon Keeper
Posts: 21137
134
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Tuple in Java is simply an array of Objects.

And occasionally, I do see code that returns a tuple that way.

But Java is a strongly-typed language, and generic tuples don't have the benefit of strong typing, which means weak spots in the code. Thus, it is preferable to create a custom class and return an instance of that with the "tuple" values as member properties.

That way you not only get the benefits of strong typing, you also can access elements by name instead of having to know their physical sequence within the "tuple".
 
Rancher
Posts: 3371
31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I disagree Tim, a tuple is not just an array of Objects.  A proper tuple is in fact strongly typed.  That's a major part of the point of why people want tuples.  But I agree that tuples do not have meaningful names for the elements they contain.  I think that's the best reason people have resisted adding them to Java.  I think that once Java eventually gets concise value types (similar to Kotlin's data classes or Scala's case classes) that will solve the problem nicely, making it almost as easy to declare a new type with meaningful field names as it would be to declare a new tuple.
 
Tim Holloway
Saloon Keeper
Posts: 21137
134
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would be very interested in how you make the determination that a tuple is "strongly typed". Of the 3 languages that I can think of that can return tuples: LISP (a tuple here is a list), Python, Perl - and I think I had a 4th, but it just fell out of my mind. None of those languages defines a tuple with strong typing or even fixed-length. A Python or Perl method can have multiple return statements and each statement could return a simple value, a tuple, or some other data type like an array or dictionary and no two of those return statements would be obliged to return the same sort of thing(s).

I haven't dug into Scala enough to say whether it defines fixed-type tuples, but if so, it's an outlier. Not that I'd complain. Just pointing out that no such assurances occur in most other popular languages that can return tuples.
 
Mike Simmons
Rancher
Posts: 3371
31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I suppose the problem is there is no standard Tuple implementation for Java, and we're coming from different directions and thinking of different workarounds that people have used.  I agree that an Object[] array would not be strongly typed at all, and a List<T> would be little better since we really want different types for the different elements.  

But various people have come up with more strongly-typed Java implementations, where a Pair<A, B> means a tuple of two elements, the first of type A, and the second of type B, and the compiler can know the type of each one in different contexts.  Apache Commons has Pair and Triple classes.  There's the javatuples library.  Google Guava did have tuples at one point I think, with the @Beta annotation, but dropped them, explanation here.  Scala certainly has them. . Kotlin still has Pair but dropped their other Tuple implementations. Of course, they have concise data types already, so who needs tuples as well?  And we can also point to standard Java's Map.Entry as a typed tuple.  I'm sure I've seen other home-grown typed Pair classes lurking within other libraries.

When I see people discussing "why aren't there tuples in Java", I generally interpret it as why haven't any of these strongly-typed implementations made it into the standard library.  People who don't want strongly typed probably aren't using Java anyway. . But it's a fair point that "tuple" in the outside world does not imply strong typing.

Of course, if the title of a thread says "Why does Java not have anything like Tuple of Scala?", that does suggest we're talking about a strongly-typed variant.
 
Bartender
Posts: 3519
150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The title is wrong. We have the javafx.util.Pair<K, V> class that I use regularly.
 
Mike Simmons
Rancher
Posts: 3371
31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not in the JDK though. For those not already depending on JavaFX, then to get tuples, it seems much more straightforward to import Apache Commons.
 
Saloon Keeper
Posts: 2723
355
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:We have the javafx.util.Pair<K, V> class that I use regularly.

Mike Simmons wrote:That's not in the JDK though.


I suppose that java.util.Map.Entry and java.util.AbstractMap.SimpleEntry could be used to represent a 2-tuple (and are included in the JDK)
 
Mike Simmons
Rancher
Posts: 3371
31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup.

Mike Simmons wrote:And we can also point to standard Java's Map.Entry as a typed tuple.



It's a confusing name to use if you're using it for something that's not an entry in a Map, of course...
 
Ron McLeod
Saloon Keeper
Posts: 2723
355
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mike Simmons wrote:And we can also point to standard Java's Map.Entry as a typed tuple.


Sorry .. I missed that comment in one of your previous post
 
Monica Shiralkar
Ranch Hand
Posts: 1258
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
I'm full of tinier men! And a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!