Great Deals

Java8 Part2 Streams

23:20 Unknown 0 Comments

Java8 Streams
The Java conventional style to deal with collection is external iteration.
We deal with collection by
  • Internal iteration
  • External iteration
The term external iteration implies you need to explicitly take care of iteration of elements in collection then validate each element and add it to the other list.

Where as internal iteration implies.
Java8 itself gives capability to iterate each and every element you just need to supply filter criteria.
 
For internal iteration it comes with three important methods to deal with collection of objects.
 
1.Map
2.Filter
3.Reduce


1.Map:
  • Map method allows you to process one by one element of collection.
  • Takes argument as lambda expression
  • apply lambda expression on every element
  • Return results.

Map generates each processed element in the seperate streams
FlatMap: whereas flatmap each processed element flattened into the seperate stream.

2.Filter
Produces a new Stream that contains only the elements of the original Stream that pass a given test.

3.Reduce
  • Reductions operation occurs at the end of the all operations.
  • Hence it is also termed as terminal operation.
Generally the methods used for reductions are
1. Sum
2..Average
3. Count
4. toArray


Different kinds of streams
1.Stream
2. Parellal Stream-

1.Stream
Allows you to convert List into Stream Objects
Method :listObj.strean()
 
2.Parallel Stream
You can do operate on your stream in parallel.
When you call parallelStream() it creates several threads and operates on our collection simultaneously.

0 comments:

Advertising