I'm curious if there are things in the standard class library that you find useful but not widely used.
What are some things in the class library you wish more people knew about or used?
https://docs.oracle.com/en/java/javase/17/docs/api/
https://docs.oracle.com/en/java/javase/17/docs/api/
I'm curious if there are things in the standard class library that you find useful but not widely used.
19 Comments
grossjonas@programming.dev · 8 pts · 3y
PushbackInputStream - makes it easy to write a small template engine in restricted environments
JackbyDev@programming.dev · 3 pts · 3y
Link for the curious
This does sound useful. I have to admit I don't know much about
InputStreamandOutputStreamand every time I need to do something with them (which is rare) I just search it up.pohart@lemmyrs.org · 1 pts · 3y
Ooh, I've never seen that before. And it's been there since 1.0
kaba0@programming.dev · 8 pts · 3y
HttpClient. You don’t necessarily need a third-party tool for rest calls or the like, and it really is quite handy!
Sheldan@programming.dev · 1 pts · 3y
Finally a more sane API for HTTP requests in java. Still using a library tho, can be very convenient (like okhttp)
koreth@lemm.ee · 8 pts · 3y
Sometimes people who build low-level code with mutable state that's shared across threads don't know about some of the synchronization tools Java has. A lot of people know about
synchronized, a lot of people know about semaphores, but fewer people seem to know about CountDownLatch and fewer still seem to have heard about Phaser. Those last two have saved me from having to implement my own synchronization code on a number of occasions.JackbyDev@programming.dev · 3 pts · 3y
Those sound useful. I haven't had to do much cross-thread synchronization thankfully, but I have had to use a
BlockingDequeto check that some events came and in the right order. TheCountDownLatchandPhasermay have been better.austin@programming.dev · 1 pts · 3y
I had never heard of Phaser, but it looks pretty cool. I just read Baeldung's Guide to Phaser and correct me if I'm wrong, but doesn't it kind of seem like a race condition (it could just be how they use it in the examples)?
then
if
ph.arriveAndAwaitAdvance();is called before all of theLongRunningActions are initialized, won't it proceed before it is supposed to?pohart@lemmyrs.org · 2 pts · 3y
Your analysis looks right to me. If this were mine I'd initialize all three before submitting any.
pohart@lemmyrs.org · 7 pts · 3y
Optional! I was reluctant at first because of the nullibility, but it's so useful.
I'm not sure this is what you were going for. I think most people know about optional and are skeptical because it's not a fix for nullability.
BitPirate@feddit.de · 3 pts · 3y
Don't forget its subtypes. OptionalInt, OptionalDouble etc. This avoids auto(un)boxing.
pohart@lemmyrs.org · 1 pts · 3y
Oh wow that's great. I didn't know about those at all.
JackbyDev@programming.dev · 2 pts · 3y
I didn't have a specific goal, but yeah I do wish more people used Optional.
My one "gripe" with Optional (and I use it lightly) is that they mean for it to only be used as a return type instead of anywhere something can be optional. It can still be used as that though, they just don't recommend it.
pohart@lemmyrs.org · 1 pts · 3y
IMHO it should be used for parameters, as long as there is more than one optional parameter. If rather all non nullable parameters though, which is usually doable.
JackbyDev@programming.dev · 2 pts · 3y
I've waffled on it. Currently my opinion is to use whatever nullable annotation the project uses (if any, otherwise JetBrains's). Essentially, I'm not sure if the official recommendation to avoid
Optionalfor uses other than return types has reasoning I'm missing.I do use it like this though and lament that we don't have an Elvis operator (
?:)presumably_wrong@lemm.ee · 1 pts · 3y
Instead of wrapping it in an optional you can do
Objects.requireNonNullElse(value, defaultValue)austin@programming.dev · 1 pts · 3y
Optional has more syntactic sugar for more complex scenarios / functional call chaining that prevents repetitive
ifchecksThis is completely null safe, the function calls are only made if the object is not null
fabian@programming.dev · 4 pts · 3y
Not sure if it counts as not widely used, but I think the Collections.unmodifiable(Collection|Set|List...) methods are very useful.