• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Sybex CSG 11 - strange example

 
Ranch Hand
Posts: 46
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
in a callout "Using flatMap()" by Boyarsky, Jeanne. OCP Oracle Certified Professional Java SE 11 Developer Complete Study Guide (p. 712). Wiley. Kindle Edition.

There is a strange example of using flatMap to create int stream... Why would you use a flat map for this example? FlatMap is used to flatten the content of the collection, and here the collection is ready to be streamed as is, right?

I tested with this code, prints the same result:

       var integerList = List.of(1, 2, 3, 4, 5, 6);

       IntStream flatInts = integerList.stream().flatMapToInt(x -> IntStream.of(x));
       IntStream ints = integerList.stream().mapToInt(x -> x);

       flatInts.forEach(System.out::println);
       ints.forEach(System.out::println);

I got used that they explain some strange solutions later in the text, but there is no explanation for this...

Am I missing something?
 
author & internet detective
Posts: 42135
937
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rade,
Yes, those examples are equivalent. We were trying to highlight the name of the method so used the simplest possible example.

Page 700 had already introduced the flatMap() method and provided a more realistic example. (lists with 0, 1, and 2 elements) being combined.

So page 712 assumes you've already read what flatMap() does and is just showing the names of the methods for primitive streams.
 
Rade Koncar
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, thanks Jeanne!
btw, I proposed one errata on this thread https://coderanch.com/t/734831/certification/Java-OCP-Developer-Complete-Study (it is the one about numeric literals)
 
Jeanne Boyarsky
author & internet detective
Posts: 42135
937
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
I didn't get to that thread yet. But the "Kindle numbering problem" isn't an errata. It is an Amazon rendering thing that we nor our publisher have any control over.
reply
    Bookmark Topic Watch Topic
  • New Topic