A Guide To Java Stream API
Stream connects to the source of collections data structure and can performs specific operations on it. It was added in Java 8 under the Package name java.util.stream.
Introduction
What does it offers
- It can Efficiently process large amounts of data.
- Supports Parallel operations, to leverage multi core processors.
- Lazy handling of pipeline operations & avoids unnecessary intermediate computations.
- It represents a sequence of elements and supports different kind of operations to perform computations upon those elements.
Code Example
|
|
Code Analysis:
- From the code, we have a list of string data -
"a1", "a2", "b1", "c2", "c1"
. - Filtering string that starts with character “c”
- converting it to uppercase i.e “C1”
- Sorting the list
- Printing out each elements
Its a general overview. Lets understand more on streams creation, initialization, operations and execution.
1. Creating Streams
|
|
2. Stream Operations
forEach:
forEach()
it loops over the stream elements. Lets say we have List<Product> productList = [ .. ]
.
|
|
Map:
map()
produces a new stream after applying a function to each element of the original stream.
The new stream could be of different type.
|
|
Filter:
filter()
produces a new stream of the elements that satisfy the given condition.
|
|
flatMap:
For the complex data structures like-
Stream<List<String>>
flatMap()
helps us to flatten the data structure.
Lets say we have structure like this - [ [1,2,3],[4,5,6],[7,8,9] ]
which has “two levels”.
In simple - Flattening means transforming it as : [ 1,2,3,4,5,6,7,8,9 ]
|
|
Matching:
anyMatch()
, allMatch()
, noneMatch()
.
Lets say we have a collection of a color pencils.
|
|
Specialized Operations:
Operation like sum()
, average()
, range()
. Let’s say we have List<Employee> empList = [ .. ]
|
|
Reduction Operations:
(Identity, Accumulator, Combiner).
|
|
Code Analysis:
-
reduce ( startingValue, binaryOperator/accumulator-fuction)
-
startingValue = 0, is the identity, also the default result if stream is empty and only initialized at first
-
subtotal is the accumulator storing the final value after each operation.
-
1st iteration: since startingValue = 0 than (subtotal/accumulator = 0, element = 1). arguments setted (subtotal, element)
-
operation:
-> subtotal + element
0 + 1 = 1, now returning 1. subtotal = 1 -
2nd iteration: (subtotal = 1, element = 2). Operation: 1 + 2 = 3
-
3nd iteration: (subtotal = 3, element = 3). Operation: 3 + 3 = 6
-
Final result = 5
This was a example using identity and accumulator.
With Identity, Accumulator and Combiner - three arguments is used in parallel processing.
Combiner works with parallel stream only, otherwise there is nothing to combine.
|
|
Collect:
Collect is used to get stuff out of the stream once we are done with all the processing.
|
|
Joining:
Collectors.joining()
will insert the delimiter between the two String elements of the stream.
Lets say we have List<Employee> empList = [ .. ]
|
|
3. Order of Execution in stream operation
|
|
When executing this code snippet, nothing is printed to the console. That is because intermediate operations will only be executed when a terminal operation is present.
|
|
|
|
The order of the execution is very important to understand in stream operations.
|
|
|
|
For more info See: