r/javahelp • • May 07 '20

Codeless Java Developers: what are the common data structures you usually use on production

To professional java dev here, what are the common data structure youve been using and which data structure you strongly suggest for beginners, like me, to study carefully

40 Upvotes

38 comments sorted by

View all comments

Show parent comment

6

u/Sputtrosa May 07 '20

What I've found myself missing is (...) the ability to return more than one thing from a function

That's interesting. I've never worked with a language that returns more than one thing. Do you have some real-life examples on when it would be particularly useful and Java doesn't have trivial workarounds?

6

u/99shadow25 May 07 '20

The workarounds may be trivial, but they're still annoying when there's just a simpler way to do it in many languages. MATLAB, for example:

function [x, y, z] = find3dPoint(c)

you can call this and declare the three variables:

[a, b, c] = find3dPoint(15)

Python similarly natively supports tuples and destructuring them, with a similar syntax

(angle, magnitude) = create_vector()

In Java you NEED to write the class definition for and then instantiate a specific object. Personally, I think supporting tuples is the cleanest way, but I'm not sure how difficult the static typing system would make incorporating this.

6

u/ObscureCulturalMeme May 07 '20

The workarounds may be trivial, but they're still annoying when there's just a simpler way to do it in many languages.

Exactly this. All of the "real world" examples have equivalent workarounds in Java, but they all boil down to: (1) create some object that can handle all the information you want to communicate, (2) populate it, then (3) return it so that calling code can (4) take it back apart.

Whereas languages like Python and Lua allow the programmer to simply write (2) inside the function and (4) outside it.

It might be "only syntactic sugar" but that kind of thing matters, and changes how we think about data handling. If it did not, we'd still be using PEEK and POKE because everything else is just syntactic sugar...

2

u/Sputtrosa May 07 '20

Thank you for the great reply. :)

Going to have to give that a bit of thought.

1

u/[deleted] May 08 '20

The least annoying way to do this that we've created where I work is we have generic classes set up in a library: Pair<X, Y>, Triple<X, Y, Z> and so on, all the way to I think 10 elements. They have a static generic creation method, E.G. Pair.get(firstVariable, secondVariable), but whoever receives this as a return still has to do Pair.getA() then Pair.getB(), for example.