Win a copy of Spring in Action (5th edition) this week in the Spring 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
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Stream destructive methods  RSS feed

 
Ranch Hand
Posts: 66
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: 6257
420
BSD
  • 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):
 
We don't have time for this. We've gotta save the moon! Or check this out:
Download Free Java APIs to Work with Office Files and PDF
htttp://www.e-iceblue.com/free-apis.html
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!