Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Introduction to Functional Programming Meet Streams flatMap

Siu KWAN Yuen
PLUS
Siu KWAN Yuen
Courses Plus Student 2,898 Points

Help!! Stream and Data Structure

Will operation of a stream change its original object's data structures? What is the data structure when
List <String> m. stream(). filter( m-> isMPass(m)) <- what is the data structure of this output?

2 Answers

Hi Siu.

Stream doesn't modify the original data structure.

The output of this:

List <String> m. stream(). filter( m-> isMPass(m))

is a Stream. The filter method returns a Stream.

You can collect the output in a new List, using collect() method and the Collectors class:

List<String> newList = m. stream(). filter( m-> isMPass(m)).collect(Collectors.toList())
Siu KWAN Yuen
PLUS
Siu KWAN Yuen
Courses Plus Student 2,898 Points

Oh, Thanks!!! Is this stream the same as the I/O stream? So the output is not a way to store data or not a data structure and that's why it needs a terminal operation.

Hi! I believe that are not the same thing, but the idea should be. :) Yes, you need a terminal operation that will return a result or a new data structure with the modifications! excuse my english