Win a copy of High Performance Python for Data Analytics this week in the Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Sybex 816(Kindle) Appendix: Answers to Review Questions, Chapter 20, question 17 - errata

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assuming the /fox/food‐schedule.csv file exists with the specified contents, what is the expected output of calling printData() on it?  
             /fox/food-schedule.csv
    6am,Breakfast
    9am,SecondBreakfast
    12pm,Lunch
    6pm,Dinner

    void printData(Path path) throws IOException {
       Files.readAllLines(path) // r1
          .flatMap(p -> Stream.of(p.split(","))) // r2
          .map(q -> q.toUpperCase())  // r3
          .forEach(System.out::println);
    }

As a correct answer in Appendix, we have just option B.
I completely agree that this option is correct, because line r2
really doesn't compile. But, why line r3 was not included in the list of correct answers too?
Because it as well have a compile error, for the given code.
Tell me please, if my vision regarding the correct answers is correct or not?
Thanks.
 
Marshal
Posts: 71760
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is that the Boyarsky and Selikoff book?
What was the correct answer given? Remember most people here don't have that book.
Why do you think r2 won't compile? I have found out why: readAllLines(Path) returns a List<Stream> rather than a Stream<String>. The compile‑time error occurs on r2; anything following is a continuation of the same error.
 
Mihail Kuceruk
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ritchie.
Thank you very much, for this response.
Your words really make sense to me now, and why it was chosen just B like a response, and not C too, for example.
Here is by the way, the original response from the book itself.

B. The readAllLines() method returns a List, not a Stream. Therefore, the call to flatMap() is invalid, and option B is correct. If the Files.lines() method were instead used, it would print the contents of the file one capitalized word at a time, with commas removed.

Boyarsky, Jeanne; Selikoff, Scott. OCP Oracle Certified Professional Java SE 11 Developer Complete Study Guide (p. 1172). Wiley. Kindle Edition.
 
Campbell Ritchie
Marshal
Posts: 71760
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mihail Kuceruk wrote:Hi Ritchie.
Thank you very much . . . .

That's a pleasure Maybe Jeanne will come along to confirm my opinion. Remember the exam will probably say there is only one correct answer.
 
author & internet detective
Posts: 40393
821
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you change readAllLines() to lines(), the code compiles.

Line r2 is the compiler error because of the return type from the previous line. While line r3 may give a compiler error, it is because r2 doesn't compile.
 
Mihail Kuceruk
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeanne, thank you very much for your confirmation too. 
Some time ago, this moment was a little bit tricky for me. But I've got the idea 
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic