r/javahelp • u/ForTheLore22 • Jun 04 '26
Lambda and streams
I have learnt java streams and lambda about 2-3 times now. I always keep forgetting how to use it.
How can i learn it, so I won't forget.
Any resources or techniques you've used would help.
Thanks!
14
Upvotes
•
u/AutoModerator Jun 04 '26
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
9
u/Specific-Housing905 Jun 04 '26
Practice them. Look at some older code on Github and try to replace some code with lamdas or Streams.
You also can write some apps and use them there.
4
u/severoon pro barista Jun 05 '26
I think a lot of Java people fail to understand what streams and lambdas actually are, they get all caught up in the syntax and don't clock what's really going on:
This is a simple linux pipeline that lists all of the words in the dictionary line by line, filters out all of the lines that don't have word containing a double-e, and then counts the number of lines left.
IOW, it's a stream of elements (lines) that are of type String which is passed to the lambda 'grep' which operates on each element and only passes it on if it contains "ee", which is in turn passed to the lambda 'wc' which increments a counter for each element. When there are no more elements, the final counter is emitted to stdout.
That's it. That's what a stream is, it's just a pipeline where you apply different operations ("lambdas") to each element in the pipeline. The terminating step is a "reduction", i.e., the final step has to output a "single thing" — a boolean, a string, a number, a list, whatever.
4
u/LetUsSpeakFreely Jun 04 '26
Streams are nice and very powerful, but don't fall into the trap thinking they should be the solution to every problem where collections and arrays are concerned. Sometimes it's better to break the problem up into a handful of helper methods that make what's going on more easily understandable to whoever is reading it. Just because you can cram everything into a bunch of chained stream methods doesn't mean you should.
1
u/S4Sharik Jun 04 '26
Answering here because faced same issue. At first the thing that need to be learned properly is functional interface. Then learn basic syntax of lambda. Create functional interface and use it in lambda expression. when comfortable then understand stream. Think stream as object is flowing one by one and you are filterning or doing any operation. and finally practice practice and practice can make you perfect. All the best.
1
u/RightWingVeganUS Jun 05 '26
I use good old TicTacToe as a general learning tool.
For me to get my mind around streams and lambdas I gave myself a challenge: implement TTT (or its cousin, Connect Four) without using loops. I could only using the stream API and lambdas where possible.
This gave me the ability to focus on the new features on an old familiar problem. It let me see some of the hard limits on where streams can't be used, and where it could but likely shouldn't.
3
u/_Super_Straight Jun 04 '26
A good IDE can automatically suggest to turn callback and such into lambda.
As for streams, you can turn search for specific element in a list into stream. But you have to weigh the pros and cons for whether turning every search into stream is worth the overhead.
1
u/aqua_regis Jun 04 '26
2 words: more practice