Java stream is standard API in Java used to process collection of object.
there are many operation in java stream. I will show you some list of operation with example by the following.
before dive into API, There are two main operation category are
- Intermediate operation
- Terminal operation
Intermediate operation
Intermediate operation is used to do some operation after stream method or can chain with another method.
some intermediate operation are
- map
- filter
- sorted
Terminal operation
Terminal operation is used to do some operation on the end of stream can’t chain function after terminal operation.
some terminal operation are
- collect
- forEach
- reduce
Now, I will show to how to use these method
map
map method isused to transform element in collection to another form for example:
filter
filter method is used to filter some element in collection that equal to the logic for example:
sorted
sorted method is used to sort element in collection for example:
collect
collect method is used to collect element back to some type of list for example:
forEach
forEach method is used to iterate element in collection. (in convention do not use forEach to transform element in collection use map instead). For example:
reduce
reduce method is used to turn collection to single value for example:
Summary
There are a lot of method of Java stream, here is Stream (Java Platform SE 8 ) (oracle.com) you can see more method can use in Java stream. Enjoy coding.
reference