• 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
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Stream destructive methods  RSS feed

 
Ranch Hand
Posts: 77
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: 6684
464
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):
 
Police line, do not cross. Well, this tiny ad can go through:
ScroogeXHTML - the small and fast RTF to HTML converter library
https://coderanch.com/t/707504/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!