Beginning Java 8 Streams
One of the most useful feature in Java 8 is the Stream API
The Streams API extends the Collections API with methods to convert and filter items in a collection. Here is a simple example and explanation
In this example we take a list of words and
1. Convert the list to a Stream using .stream()
2. filter the list looking for words that start with “c”
3. Change all the words to uppercase
4. sort the list using sorted()
5. finish the processing of the stream by collecting the results into a list.
At their simplest Mapping functions convert something into something else. In many programming languages, Map is the name of a higher-order function that applies a given function to each element of a list, returning a list of results. It is often called apply-to-all when considered in functional form.
In mathematics a Map is an operation that takes a function mapping a domain D to a range R, and a list of elements of D. It then produces a list of elements of R.
The Map function in the Streams API produces a list with the same number of elements as the input list.
Here is an example using the Map function
items.stream().map( item -> item.toUpperCase()
Here is a full example from my GitHub account.
The examples I have are draw almost directly for a presentation given by Dr. Venkat Subramaniam and a blog post written by Benjamin Winterberg. I have provided link to both below. Additionally I have provided a link to an excellent pictorial representation of the various stream operation is from Martin Fowler.
Dr. Venkat Subramaniam Java 8 Streams Presentation
Benjamin Winterberg blog
Manning Java 8 in Action Book