Stream
handling in Java8:
The
better way of usage of main components of collections called streams.
This is not
replacement of collections but we can say this is an enhancement over
collections.
In
Stream it is one-time-use object, once it is traversed it cannot be
re-traversed. Stream traverse is of two modes
- Sequential
- Parallel
In
sequential Traversing:
As the name indicates as Sequential each
item in the stream is read processed, then the next item is read till it
reaches end of stream.
Ex:
List
<Library> book = list.getStream.collect(Collectors.toList());
Parallel
Traverse:
On parallel traverse, the array is split
into multiple segments, each of which is processed individually on a different
thread. The results are then put back together for the output.
Ex:
List <Library> book =
list.getStream.parallel().collect(Collectors.toList());
Parallel Traverse by Split model:
Parallel stream flow:
List originalList = someData;
split1
= originalList(0, mid);
split2
= originalList(mid,end);
new
Runnable(split1.process());
new
Runnable(split2.process());
List
revisedList = split1 + split2;
No comments:
Post a Comment