1. streams, SQL like operations expressed in Java 8
Here too, the package documentation is an interesting read.
A stream can be considered a conveyer belt on which objects experience operations. The belt starts at a source (for instance a Supplier) and can on or more intermediate operations and end with a Terminal operation.
from Lucas Jellema of amis blogs
Here your see a physical stream and a collect operation.
2. sql like functions
select sum(salary) from people where gender = 'FEMALE'
1 2 3 4 5 people. stream() (1) .filter(p -> p.getGender() == Person.Gender.FEMALE) (2) .mapToInt(p -> p.getSalary()) (3) .sum(); (4)
|1||use people collection as source and start stream|
|2||where … where gender = female|
|3||mapping is often retrieving the value of a member, get salary, note that it is a Map ToIntFunction<Person> specialised function|
1 2 3 4 5 people. stream() .filter(p -> p.getGender() == Person.Gender.FEMALE) .mapToInt( Person::getSalary) (1) .sum();
|1||This alternative uses a method reference, which can be substituted for a lambda epression when their shapes match.|