This week's book giveaway is in the NodeJS forum.
We're giving away four copies of Serverless Applications with Node.js and have Slobodan Stojanovic & Aleksandar Simovic on-line!
See this thread for details.
Win a copy of Serverless Applications with Node.js this week in the NodeJS 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
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Stream API methods updating backing source of stream  RSS feed

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

I have a question regarding Stream API method updating the backing source of the stream. In the following code the .sorted() method does not update the backing source aka slist.stream().forEach(Student::debug) on line 14 prints the original list. However, slist.stream().forEach(increaseMarks) does update the backing source aka slist.stream().forEach(Student::debug) on line 19 prints the updated list. Is this because .sorted() is a intermediate stream operation where as .forEach() is a terminal operation? Or is there another reason why some methods update the backing source and others dont?

Regards,

Bart

 
Bart Boersma
Ranch Hand
Posts: 77
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I found out that this is called 'non-interfering' which makes it a lot easier searching for it!
 
Saloon Keeper
Posts: 9996
208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This has nothing to do with Stream or its methods. In neither case are you changing the backing collection. It still holds the same Student objects.

When you call forEach(increaseMarks), you modify the students in the list, not the list itself. With sorted(), you don't modify anything, sorted() just returns a new Stream but with the elements sorted.
 
Bart Boersma
Ranch Hand
Posts: 77
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So basically this question boils down to: which stream operations are interferent and which are non-interferent? Is it correct to say all terminal operations are interferent and all intermediate operations are non-interferent?
 
Stephan van Hulst
Saloon Keeper
Posts: 9996
208
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stream operations aren't inherently interfering. Functions that YOU pass can be interfering. You can easily pass interfering functions to non-terminal operations:

I passed peek() an interfering function. It modifies the source list as you iterate over the elements. You must never do this, unless the stream you're operating on has the CONCURRENT characteristic, and even then it's questionable practice.

Increasing a student's marks is NOT interfering, because it does not affect the source list. Remember that a list holds references to students, and you haven't changed the list or any of the references it holds. You just changed one of the students that it points to.

The sorted() method doesn't take functional arguments, so you can't pass an interfering function.
 
rubbery bacon. rubbery tiny ad:
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!