Win a copy of Transfer Learning for Natural Language Processing (MEAP) 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
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Example use cases for Java Tuple?

 
Ranch Hand
Posts: 100
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I come across Java Tuple - https://howtodoinjava.com/java/basics/java-tuples/

When we are mixed objects, the above says we can go for a tuple

In practical, need example of the use cases we might go over for a tuple instead of normal list/set in java

Thanks.
 
Marshal
Posts: 4856
317
IntelliJ IDE Python Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the tutorial you link

howtodoinjava.com wrote:A tuple can be seen as an ordered collection of objects of different types. These objects do not necessarily relate to each other in any way, but collectively they will have some meaning.


To me that describes a class.

I've developed Java professionally for well over a decade and I've never had the need to use a Tuple type of data structure. So my list of practical use case examples is none, an empty List, a zero element Tuple.
 
Marshal
Posts: 68917
275
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Johnny Joseph wrote:. . . https://howtodoinjava.com/java/basics/java-tuples/ . . .

Another confused tutorial, making some unsupported assertions,
  • that a Tuple will give the fastest execution and the lowest memory consumption,
  • that a Tuple is immutable and a List is mutable, but forgetting the existence of unmodifiable Lists, (Maybe it is simply out of date.)
  • and not explaining the difference between a pair and a Map.Entry.
  • Remember that an object containing fields pointing to mutable reference types needs special precautions to maintain immutability.
     
    Tim Cooke
    Marshal
    Posts: 4856
    317
    IntelliJ IDE Python Java Linux
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The javatuples library mentioned in that tutorial is of low quality in my opinion. My trust is weak especially because the automated test suite is not very rich at all which does not convey confidence. For a library that is providing basic data structures that I may choose to use throughout an application I need it to be bullet proof and I need proof from the author that it is bullet proof by way of comprehensive automated tests.

    I'll add that library to my "avoids" list.
     
    Campbell Ritchie
    Marshal
    Posts: 68917
    275
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Isn't it unfortunate, the amount of poor quality software out there, and the number of tutorials I don't think are any good
     
    Tim Cooke
    Marshal
    Posts: 4856
    317
    IntelliJ IDE Python Java Linux
    • Likes 3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Realising that most of the stuff on the internet is crap is half the battle, and being able to recognise it as crap is the other half.
     
    Saloon Keeper
    Posts: 21975
    150
    Android Eclipse IDE Tomcat Server Redhat Java Linux
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Tim Cooke wrote:From the tutorial you link

    howtodoinjava.com wrote:A tuple can be seen as an ordered collection of objects of different types. These objects do not necessarily relate to each other in any way, but collectively they will have some meaning.


    To me that describes a class.



    Not really. First of all, the members of a class are not ordered. Tuple members are.

    Secondly, classes typically contain both data and logic. Tuples are purely data.

    Finally, classes have fixed property quantities and types. Tuples often have neither. That's what distinguishes a tuple from its relatives, the pair, triple, etc., which are specific expressions of the general tuple.

    I occasionally do use tuples in Java. The best tuples to use are simply arrays of a homogeneous type. The worst are those whose only common type is java.lang.Object. Incidentally, there is a situation in Java Persistence Architecture (JPA) where there was simply no alternative but to return a tuple, since the process in question returned raw database field values instead of an Entity object graph. It is fortunately very rarely required.

    It's common in languages like Python and Perl for a method to return tuples and often to return tuples of no fixed length. They can deal with it, since typing is an after-the-fact affair in those languages anyway. In Java, it's much more annoying, since Java is "supposed" to be type-safe at compile time, but obviously it cannot be if you're simply pulling elements from an array of Object. You could use a support library that holds a map for the tuple element types and does typesafe element extraction, but that's a lot of extra overhead for something that shouldn't be done at all if you can avoid it.
     
    Sheriff
    Posts: 15525
    263
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I think it depends on the context and the semantics you want to create in your program. If the idea of a "tuple" is something that comes from the problem domain, maybe it's worthwhile having a Tuple class in a Java program. Otherwise, I don't know if it's worth your time and effort in creating and maintaining such a class when there are other more common ways of doing things in Java that you could do with a Tuple in other languages.

    Kotlin has the Pair and Triple classes as part of its standard library. I've found use for both these classes, especially with destructuring that Kotlin provides:

    On line 1, the x and y variables will be assigned the first and second Int values entered, respectively. Note that the readInt() method is not part of the standard Kotlin library as far as I know, it's something I've written.

    A similar thing happens to m, n, and o on line 2 although at that point, I'd probably just do this:

    On line 4, first will be assigned "John", second "Jacob", and third "Jingleheimer". The last part, "Schmidt" will simply get thrown away and eventually GC'd.

    My point being that if a tool is there, you'll eventually find ways to use it. Uses of tuples like the above in Kotlin do have their downside (see https://medium.com/@dmstocking/kotlin-destructuring-considered-harmful-2a5fa2397cac) but as with all tools, you should educate yourself on how they are supposed to be used and avoid harmful ways of using them.
     
    Tim Holloway
    Saloon Keeper
    Posts: 21975
    150
    Android Eclipse IDE Tomcat Server Redhat Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The Pair is something I liked about Smalltalk and one of the things I miss most in Java. Instead of having a universal Pair, everything invents their own. Starting with HashMap.
     
    Marshal
    Posts: 25452
    65
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    There must be quite a few use cases for Tuples because Java 14 contains an implementation named "Records" which looks a lot like that: JEP 359: Records (Preview). But note that the linked document includes a discussion of why a record is not a tuple.
     
    Saloon Keeper
    Posts: 11899
    253
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Aye, I celebrate that the Java designers held off long enough on a dedicated general purpose tuple type in favor of their new record feature.

    Tuples fly in the face of object oriented design. On the other hand, creating new classes for a return type of just one method was kind of bothersome because of how wordy Java can be. Records, as Java calls them, are the best of both worlds. You get a dedicated data type that clearly expresses what it means and what you can do with it without the nag of having to declare a whole bunch of getters and override the equality methods.

    I would also like it if Java supported object destructuring (assigning an object reference to a tuple of variables, to unpack its members), but only if the variable names have to match the member names in the object, or have another way to specify which members need to be unpacked to which variables. Relying on member order or on the members having special names like component1 is just plain horrible, and then you might as well just go back to using your general purpose generic tuple types that convey no information. I do fear that such a feature is easy to overuse, so maybe it's good we don't have it.
     
    Tim Holloway
    Saloon Keeper
    Posts: 21975
    150
    Android Eclipse IDE Tomcat Server Redhat Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Stephan van Hulst wrote:
    I would also like it if Java supported object destructuring (assigning an object reference to a tuple of variables, to unpack its members), but only if the variable names have to match the member names in the object, or have another way to specify which members need to be unpacked to which variables. Relying on member order or on the members having special names like component1 is just plain horrible, and then you might as well just go back to using your general purpose generic tuple types that convey no information. I do fear that such a feature is easy to overuse, so maybe it's good we don't have it.



    I think that what you are describing here is similar to what Dozer does. I've used Dozer to transfer data from one class object to another and it supports both by-name automatic copying and mapped copying.
     
    Don't count your weasels before they've popped. And now for a mulberry bush related tiny ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
      Bookmark Topic Watch Topic
    • New Topic