Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

About stream limit function

 
Ramy Nady
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All ,

Any one explain why limit function on Stream interface considered as stateful intermediate operation ?

I can understand why distinct or sorted be stateful but I think limit() should be stateless intermediate operation.

Thanks in advance.
 
Stephan van Hulst
Bartender
Posts: 6587
86
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stateless operations retain no information about previously seen elements. The limit() method necessarily has to count the amount of elements it's seen so far, meaning it retains information about previously seen elements, and hence is a stateful operation.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35752
412
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Stephan. Another way to think of it is how where you could run the operation in parallel in isolation. Suppose I have a thousand items in my stream. If I want to call filter(), I can give a hundred items to each of ten workers each. As long as they put the items that made it through the filter, I'm good. Picture 10 factory works doing this. No problem. They filter their own section without talking to each other.

How think about how you would instruct the ten factory works to split up the work for limit(13).  Uh oh. You can't. Because they need to know how many each of the other factory workers has seen. You could keep track of this key piece of information. But then you'd have state!
 
Campbell Ritchie
Marshal
Posts: 52621
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would presumably have your ten workers running to a board to chalk up the numbers. When you get to |||| |||| ||, you will have to watch and the next person to draw a | will shout, “Stop.”

I presume you would have one board and one piece of chalk so as to synchronise the whole process.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!