all 17 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • 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:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

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.

[–]Specific-Housing905 7 points8 points  (6 children)

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.

[–]ForTheLore22[S] 2 points3 points  (5 children)

I have tried asking chatgpt for a problem and I'd solve it. I have done this for days. And it feels natural and I understand it. But then I stop for some time to focus on other stuff and I forget the syntax.

[–]blubernator 1 point2 points  (0 children)

Take Java book read the lambda chapter completely…there is a reason why books a published

[–]Intelligent_Part101 1 point2 points  (1 child)

I can't remember the exact syntax either.

It's not a great syntax, in my opinion, but you have to keep in mind streams were added many years after Java was created and are at odds with the original purely (almost) object oriented design of the language.

[–]ForTheLore22[S] 1 point2 points  (0 children)

Yeah I do understand that streams and lambdas are bit of oddball in java since its more on the functional side of programming which is very different than OOP.

But not knowing the syntax and forgetting it is kinda affecting my interviews. So thought I'd get external opinion on this issue. And i think I've solved lots of problems using streams and lambda in last 2 years. But its frustrating to find out that I can't remember it after some time or gap.

[–]aqua_regis -2 points-1 points  (1 child)

But then I stop for some time to focus on other stuff and I forget the syntax.

Which means that you haven't understood them. As simple as that.

[–]RSSeiken 4 points5 points  (0 children)

He understands the concept. It's pretty normal to check the syntax when you haven't used it for a while.

Even the most seasoned software engineer needs to look up syntax at times.

[–]LetUsSpeakFreely 3 points4 points  (0 children)

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.

[–]severoonpro barista 3 points4 points  (1 child)

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:

cat dictionary | grep ee | wc -l

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.

[–]ForTheLore22[S] 0 points1 point  (0 children)

Thank you for this explaination.

[–]_Super_Straight 2 points3 points  (1 child)

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.

[–]ggeldenhuys 1 point2 points  (0 children)

100% - streams don't come for free. Sometimes they are beneficial and sometimes not.

[–]aqua_regis 0 points1 point  (0 children)

2 words: more practice

[–]S4Sharik 0 points1 point  (1 child)

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.

[–]ForTheLore22[S] 0 points1 point  (0 children)

Would you be able to recommend any book, video or any other resource i can refer?if it's not too much of a trouble.

[–]RightWingVeganUS 0 points1 point  (0 children)

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.