This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin 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
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Stream destructive methods  RSS feed

 
Ranch Hand
Posts: 72
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Im learning about Stream and have the following question. Why is the first peek() method not destructive, and thus flatMap() and forEach() are executed. And in the second stream the count() method is not reached because the peek() is destructive?

 
Marshal
Posts: 6506
441
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Bart Boersma wrote:stream2.peek(System.out::print).count(); // Why is count() ignored? Why is peek() destructive for the stream? Why is count() never reached?


That is an example why to use an IDE is not a good idea when preparing for a certification exam. It gives misdirection and you just overlook foundations, and that is becacuse IDE suggestion (am I right?).

How you decided count() is never reached? What actually is, that count is being calculated, but not stored in a variable, so result simply gets discarded.

Have that line as (and you'll see yourself):
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!