About Question enthuware.ocpjp.v8.2.1886 :

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
beaucarnes
Posts: 4
Joined: Sat May 07, 2016 9:58 am
Contact:

About Question enthuware.ocpjp.v8.2.1886 :

Post by beaucarnes »

In the code below, should "double d = allbooks.stream()" actually be "double d = bs.stream()"? Maybe I am just missing something.

Code: Select all

Given that Book is a valid class with appropriate constructor and getPrice and getTitle methods that returns a double and a String respectively, consider the following code:  
List<List<Book>> bs = Arrays.asList(
         Arrays.asList(
                 new Book("Windmills of the Gods", 7.0),
                 new Book("Tell me your dreams",9.0) ),
         Arrays.asList(
                 new Book("There is a hippy on the highway", 5.0),
                 new Book("Easy come easy go", 5.0)) );
  double d = allbooks.stream()
         INSERT CODE HERE //1
         INSERT CODE HERE //2
         .sum(); System.out.println(d);
  What can be inserted in the above code so that it will print 26.0?

admin
Site Admin
Posts: 8996
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1886 :

Post by admin »

You are right. Fixed.
thank you for your feedback!
Paul.
If you like our products and services, please help us by posting your review here.

ntotomanov
Posts: 6
Joined: Tue Jan 19, 2016 4:30 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1886 :

Post by ntotomanov »

Hello, in the answers there is re-defined bs variable which makes it quite confusing:

Code: Select all

List<List<Book>> bs = Arrays.asList( ...
double d = bs.stream().flatMap(bs -> bs.stream()).mapToDouble(book -> book.getPrice());
should be something like:

Code: Select all

double d = bs.stream().flatMap(ss -> ss.stream()).mapToDouble(book -> book.getPrice());
Attachments
Redefined variable
Redefined variable
redefined-var.png (121.45 KiB) Viewed 2111 times

admin
Site Admin
Posts: 8996
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1886 :

Post by admin »

You are right. Fixed.
thank you for your feedback!
Paul.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 0 guests