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

34

u/ObscureCulturalMeme May 07 '20 edited May 08 '20
  1. The built-in Collections framework goes a long, long way. Even when it comes time to roll a custom structure, basing it off one of those gets you a very solid start.

  2. I believe that a pair of Countdown Latch objects, properly applied, can solve all communication problems including peace in the Middle East.

  3. What I've found myself missing is not so much a data structure exactly, but the ability to return more than one thing from a function, the way (for example) Lua can. The way to do that in Java (and its parent languages) is to build an aggregate structure and return that "one" thing, but it's tedious no matter how you implement the aggregates. I'm looking forward to someday working with java 14 and its records, for this reason.

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.

3

u/wildjokers May 07 '20

but the ability to return more than one thing from a function, the way (for example) Lua can

This is absolutely the thing I miss most in Java after working in groovy and kotlin.

When I was first learning groovy and I saw it allowed multiple return values I was like "why would I ever need that?". Oh, how naive I was. I ended up using it all the time. Then when I would switch back to Java I missed it greatly (and still do everyday).

2

u/philipwhiuk Employed Java Developer May 07 '20

The built-in Collections framework goes a long, long way. Even when it comes time to roll a custom structure, basing it off one of those gets you a very solid start.

And you should only bother when there's a really sizeable need to IMO.

2

u/[deleted] May 08 '20

[removed] — view removed comment

2

u/ObscureCulturalMeme May 08 '20

Hadn't seen that one before, thanks! I'm a big fan of the idea of compile-time annotation processors. (In practice they've always been a pain in the ass to get running with Ant or Gradle, but everything improves with time.)

3

u/MrSquicky May 07 '20

I believe that a pair of Countdown Latch objects, properly applied, can solve all communication problems including peace in the Middle East.

I'd love to hear more about this. I'm using Spring's DeferredResult in what I think is a similar context. I'm aware of CountDownLatches, but I've never used them.

1

u/Yithar Intermediate Brewer May 08 '20 edited May 08 '20

I have used them in combination before. Hmm, thinking about it, maybe they're not so different.

I see DeferredResult a sort of like callbacks in Javascript with the event loop. Like frontend JS often involves fetching JSON or something.

I feel CountdownLatch is more of like you're waiting for a certain number of things to happen, like maybe you need to get 2 messages back before something that can happen. I can't go into too much detail I think, but basically I was waiting for 2 requests I sent over the messaging protocol, to respond back. And then the logic would execute to process the responses so the GraphQL server could respond to the request it originally received.

It seems to me as similar to race conditions and data races. You can have a race condition without a data race, for example.

0

u/DepletedSensation May 07 '20

Im not 100% sure what he might mean here. But like JavaScript can return more than one object from a list based off of a filter or value. (Say if price is less than 5$, or if title starts with "A")

To my knowledge there is no built in function for this in Java, you have to write a stream or your own method that returns a list with this desired filter applied.

3

u/MrSquicky May 07 '20

Oh, no, that I get and totally agree with. He's looking for a decomposable tuple return. Me too.

So like you could return ("foo", 10) from a function and read that as (text, count) and have text = "foo" and count = 10.

Python does this really nicely. Kotlin has it too, but it's not quite as nice because, as far as I know, it needs to be typed (although I can't imagine wanting to return more than 3 things from a function anyway). It kind of sucks to return Object[] and then need to read it out of it.

---

CountDownLatches are used with Threads for controlling concurrent access. I'm aware of the concept, sort of. If my understanding is right, they're sort of like Futures with a numeric countdown you can attach to it so that when the countdown goes to zero, it completes. I've never used them and would be interested in learning how other people are using them. I love finding out cool usages of parts of a languages that I didn't know about.

I found myself in a situation where we're doing real time messaging, but not able to use web sockets because of technical limitations, so I'm using long polling by leveraging Spring's DeferredResult and I'm not sure if we're talking in the same area or not.

1

u/DepletedSensation May 14 '20

Yea I mean the way I would handle that is by using Pairs and stuff.

Also, about the Future and Threads, well it's so rarely we get to use them unless we really need to right? It kind of sucks. I can deffinitely recommend a book, "Java Concurrency In Practice" by Goetz. He helped design the Concurrency package in Java 6. :)

1

u/Yithar Intermediate Brewer May 08 '20

What I've found myself missing is not so much a data structure exactly, but the ability to return more than one thing from a function, the way (for example) Lua can. The way to do that in Java (and its parent languages) is to build an aggregate structure and return that "one" thing, but it's tedious no matter how you implement the aggregates.

Yeah this is one of the reasons I love Scala. And Scala also supports pattern matching as well.

https://docs.scala-lang.org/tour/tuples.html

1

u/[deleted] Jun 06 '20

How will records imrove that? As transfer objects?

1

u/ObscureCulturalMeme Jun 06 '20

Reduces the tedium