Professional Documents
Culture Documents
Streams Java8 Notes
Streams Java8 Notes
For example, let’s see how we can use reducing() with groupingBy():
@Test
public void whenStreamGroupingAndReducing_thenGetMap() {
Comparator<Employee> byNameLength =
Comparator.comparing(Employee::getName);
Here we group the employees based on the initial character of their first name. Within each group, we find
the employee with the longest name.
Parallel Streams
Using the support for parallel streams, we can perform stream operations in parallel without having to
write any boilerplate code; we just have to designate the stream as parallel:
@Test
public void whenParallelStream_thenPerformOperationsInParallel() {
Employee[] arrayOfEmps = {
new Employee(1, "Jeff Bezos", 100000.0),
new Employee(2, "Bill Gates", 200000.0),
new Employee(3, "Mark Zuckerberg", 300000.0)
};
assertThat(empList, contains(
hasProperty("salary", equalTo(110000.0)),