Page 1 of 1

About Question enthuware.ocpjp.v8.2.1886 :

Posted: Sat May 07, 2016 8:25 pm
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?

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

Posted: Sat May 07, 2016 10:46 pm
by admin
You are right. Fixed.
thank you for your feedback!
Paul.

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

Posted: Mon May 23, 2016 6:34 am
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());

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

Posted: Mon May 23, 2016 7:01 am
by admin
You are right. Fixed.
thank you for your feedback!
Paul.