Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning 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
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Sybex CSG 11 - strange example

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • 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: 40241
819
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • 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: 40241
819
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic