Open Zhiaowei99 opened 3 years ago
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!
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!
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.
Streams can only be used once, once consumed if cannot be reused.
As what is already mentioned above, the stream has been operated on and to solve this question, you can try using reduce instead.
You can also consider using IntSummaryStatistics and then retrieve the maximum and minimum.
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):
Screenshots (if any):