Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1819SEM1 Question 3 #397

Open
Zhiaowei99 opened this issue Apr 27, 2021 · 7 comments
Open

1819SEM1 Question 3 #397

Zhiaowei99 opened this issue Apr 27, 2021 · 7 comments
Labels
🧠 Finals Q&A Questions related to Finals

Comments

@Zhiaowei99
Copy link

Summary

I have trouble using the stream method. I can't figure out why. Can anyone help me? :))

Description

Post the question that you would like your peers or seniors to answer.

Suggested Answer (if any):

Screenshot 2021-04-28 at 12 13 15 AM

Screenshots (if any):

Screenshot 2021-04-28 at 12 11 35 AM

@Zhiaowei99 Zhiaowei99 added the 🧠 Finals Q&A Questions related to Finals label Apr 27, 2021
@ShyamalSankar
Copy link

Once you process the stream it is gone. Ie if you find the max you cannot use the stream again to find the minimum.

I suggest converting every element of the IntStream to an instance of the minMax class.
Like so: instream.map(x -> new MinMax(x,x)).reduce(lambda)

Inside your lambda you should extract the min of x and the min of y and compare them and get the minimum between the 2.
Do the same for the max and then create a new min max instance and return it. Hope this helps!

@eseutella
Copy link

Hello here's my attempt. I map each element in the stream to a new MinMax(element, element) and then I use reduce to get the maximum and minimum values. Hope this helps!
Screenshot 2021-04-28 001853

@Jeyxiang
Copy link

Hi you can only operate on the stream once. Meaning to say instream can only be used once, so after calling a terminal operation on a stream (max() in this case), you cannot use the same stream again. You can check out this link for a clearer explanation.

@Jjjetplane
Copy link

Streams can only be used once, once consumed if cannot be reused.

@seanangwj
Copy link

As what is already mentioned above, the stream has been operated on and to solve this question, you can try using reduce instead.

@markwty
Copy link

markwty commented Apr 28, 2021

You can also consider using IntSummaryStatistics
image
and then retrieve the maximum and minimum.

@chooweixiang98
Copy link

chooweixiang98 commented Apr 28, 2021

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🧠 Finals Q&A Questions related to Finals
Projects
None yet
Development

No branches or pull requests

8 participants