List<Person> friends = Arrays.asList(new Person("Bob", 31), new Person("Paul", 32), new Person("John", 33));
double averageAge = friends.stream().filter(f->f.getAge()<30)
.mapToInt(f->f.getAge()).average().getAsDouble();
System.out.println(averageAge);
As per the logic, "average()" method returns OptionalDouble and if it is empty then getAsDouble throws an exception. Though the explanation correctly explains this but the answer selected in the test is incorrect. It still shows 0.0 as the correct answer.
Any comments?
About Question enthuware.ocpjp.v8.2.1810 :
Moderators: Site Manager, fjwalraven
-
- Posts: 2
- Joined: Wed May 24, 2017 8:20 pm
- Contact:
-
- Site Admin
- Posts: 10045
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1810 :
I see that option 2 (It will throw an exception at runtime. ) is set as the correct option and not option 3 (0.0). Can you please post a screen shot if you see anything different?
-Paul.
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Wed May 24, 2017 8:20 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1810 :
Apologies for the delay. I got the reason for confusion here. Screenshot attached below. As per comments it is mentioned that
"Since there is no element in the stream anyway, the stream remains empty. Finally, the average() method computes the average of all the elements, which is 0.0."
I read the documentation and it is fine that in case of an empty stream OptionalDouble.empty will be returned. Below is the JavaDoc snippet.
/**
* Returns an {@code OptionalDouble} describing the arithmetic mean of elements of
* this stream, or an empty optional if this stream is empty. This is a
* special case of a
* <a href="package-summary.html#Reduction">reduction</a>
The above text will need some correction to clearly mention the way it is described in JavaDoc.
Thanks
Sorabh
"Since there is no element in the stream anyway, the stream remains empty. Finally, the average() method computes the average of all the elements, which is 0.0."
I read the documentation and it is fine that in case of an empty stream OptionalDouble.empty will be returned. Below is the JavaDoc snippet.
/**
* Returns an {@code OptionalDouble} describing the arithmetic mean of elements of
* this stream, or an empty optional if this stream is empty. This is a
* special case of a
* <a href="package-summary.html#Reduction">reduction</a>
The above text will need some correction to clearly mention the way it is described in JavaDoc.
Thanks
Sorabh
- Attachments
-
- Screenshot
- question_java8.JPG (131.33 KiB) Viewed 4500 times
Who is online
Users browsing this forum: No registered users and 37 guests