This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification 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
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Help with behavior of peek() on a stream adding strings in a list

 
Pablo Napoli
Ranch Hand
Posts: 77
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys. I'm studying chapter 4 of ocp Study Guide by Jeanne and Scott and inspired in one exercise of page 201 I did another one to understand more how peek() works but I can't understand what's going on.

First of all I want to say that I noticed it by chance that if I add count() method at the end, the stream works as I expect, but my question is why?.



As you can see, now the output is [alex, anna, toby] but if I remove count() method, the result is [], so neither element is added to the list.

Thank you guys, I'm really breaking my head trying to solve it but still nothing.
 
Mike Simmons
Master Rancher
Posts: 3399
33
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To understand this, you need to know about terminal operations and intermediate operations on streams.  Hopefully, there's a section in Jeanne and Scott's book that covers this, that you can review.  Basically, when you don't have the count() method, your stream has only intermediate operations, no terminal operations.  Which means that it's basically a stream that is ready to loop through your list, and sort it, and peek at the contents.  But it hasn't actually been given the "do it!" command to make it happen.  That's what a terminal operation is - a command that requires the stream to actually process all the elements in the stream., with whatever processes have been defined in the stream.  

The count() method is a terminal operation.  So when you add that, it actually kicks off all the processing.

Note that nothing actually captures the count returned by the count() method.  So that result is ignored.  But still, count() and an important "side effect", because it's the thing that actually kicks off all the processing on the strem.
 
Pablo Napoli
Ranch Hand
Posts: 77
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great!!!. For people that never saw all this stuff before I think it's a bit hard, because it's like a change of paradigm, where you should focus more on what to do so rather than how it do it, but the book explains it very clear. Just I was stuck because I haven't had in mind the theory that I read at all.


Stream.generate( () -> "Thank you!").forEach(System.out::println);
 
Junilu Lacar
Marshal
Posts: 14386
240
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Pablo Napoli wrote:Stream.generate( () -> "Thank you!").forEach(System.out::println);


Very clever. But you might want to .limit() that to a few hundred or so maybe
 
Mike Simmons
Master Rancher
Posts: 3399
33
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're just jealous because you didn't get an infinite stream of thank-yous ;)
 
Junilu Lacar
Marshal
Posts: 14386
240
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mike Simmons wrote:You're just jealous because you didn't get an infinite stream of thank-yous ;)


I'm jealous because I didn't come up with that expression  
 
The overall mission is to change the world. When you've done that, then you can read this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!