Collection<AdressBook> books;Collection<String> addresses;// Collection is initialized// Collects all the addresses from all the address booksaddresses =books.stream().flatMap((b) ->b.getAdresses().stream()).collect(Collectors.toList());
Reduce
Combines all the values.
Collection<Integer> integers;Integer sumation;// Collection is initialized// All the values are added togethersumation =integers.stream().reduce(this::sum);
Some streams include the most common reduction functions:
integers.stream().sum();
Consume (Apply to All)
Apply a function to all the elements.
Collection<Integer> integers;// Collection is initialized// Increases all the valuesintegers.stream().forEach(this::increase);
Concatenate
Combine streams.
Collection<Integer> list1;Collection<Integer> list2;Collection<Integer> allValues;// Collections are initialized// Combines collectionsallValues =Stream.concat(list1.stream(),list2.stream).collect(Collectors.toList());