answer: B
A. Failed
there is no such method as average() in Stream
B. correct
IntStream
OptionalDouble average()
public double orElse(double other)
If a value is present, returns the value, otherwise returns other.
so it returns double
C. Failed
Failed
this expression returns OptionalDouble
so
double average = players.stream().mapToDouble(a -> a.score).average();
returns
MyClass.java:17: error: incompatible types: OptionalDouble cannot be converted to double
D. Failed
there is no such method as average() in Stream
B => to contact orElse return a double. Lonely average return a optionalDouble
OTHHERS stream not contain average, InsStream or InDouble yes but with orElse clause return a primitive value
The correct statement to insert on line 1 to calculate the average of the Player's score is option B: `players.stream().mapToInt(a -> a.score).average().orElse(0.0);`. This statement uses the `mapToInt` method to convert the stream of `Player` objects into an `IntStream` of their scores, then calculates the average using the `average` method, and finally returns the result or 0.0 if no result is present using the `orElse` method.
double average = players.stream().mapToInt(a -> a.score).average().orElse(0.0);
upvoted 1 times
...
Log in to ExamTopics
Sign in:
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
ASPushkin
1 month, 2 weeks agod7bb0b2
6 months agoOmnisumem
9 months, 1 week agoStavok
11 months, 3 weeks agoJtic
1 year, 4 months ago