11. Filtering tracks over a minute and collecting to a List
Mapping tracks to names and collecting to a Set
12. An Intermediate operation, why don't we chain them together?
Filtering tracks over a minute, mapping tracks to names and collect to a Set
Well, what's stream?
We may just use
14. •The stream method returns a Stream instance, not a new collection.
•The filter or map method returns a Stream instance, too.
•The collect(toSet()) returns a Set<String> instance as a result.
15. •The Stream instance draw data from the source, could be a collection, an array, a generator function, or an I/O channel.
•An intermediate operation, such as filter, produces a new Stream instance.
•A terminal operation, such as forEach, produces a non-stream result.
16. •Intermediate operations are lazy…
•You don't have to close streams provided by JDK8 except Files.line, list and walk.
•Compared with the eager way.
17. •Why does Stream's findFirst return Optional?
•Maybe there's no the first one … Why not null?
•Which method of Stream returns Optional?
–Optional<T> max(Comparator<? super T> comparator)
–Optional<T> min(Comparator<? super T> comparator)
–Optional<T> reduce(BinaryOperator<T> accumulator)
I call it my billion- dollar mistake.
18. •Whether an Optional instance containing a value or not is optional, so make appropriate checks to avoid bugs.
•Creating an Optional instance…
•Getting the value orXXX?
–T orElseGet(Supplier<? extends T> other)
–T orElseThrow(Supplier<? extends X> expsp)
19. •Checking by yourself?
•Invoking the specified consumer if a value is present.
•Invoking the specified consumer if the value matches the given predicate.
NoSuchElementException if no value.
20. •Wait! I see wired method signatures in previous examples….Supplier, Consumer, Predicate…What're those types?
•Important functional interfaces used time and time again in JDK8.
22. •Function<T, R>
–Sub interface: UnaryOperator<T>
•Fail fast examples if an Optional instance contains no value.
•Avoiding performance overhead.
24. •To avoid performance overheads, the streams library has primitive versions. They have a clear- cut naming convention.
•Streams for primitives.
•Functional interfaces for primitives.
25. •Functional interfaces for mapping objects and primitives.
•Several functional interfaces starts with Bi- prefix. Take BiConsumer<T, U> for an example.
27. •How long of all long tracks?
•Reduce? Sometimes it's called as fold(left).
28. •The max length?
–Terminal operations that return one value by reducing the contents of a stream.
–(Reduce, fold or combine, whatever you call it.)
29. •How to reduce the elements of a stream to a more complex object, such as a collection?
•Using collect methods…
30. •The Collectors.toXxx method, such as toSet, doesn't return a Set but a Collector.
•The Collectors provides factory methods for creating out-of-box Collector …
•Grouping and mapping …
31. •Grouping and reducing …
•Grouping and averaging …
•The stream library picks an appropriate implementation for you.
32. •You might wish to collect your values into a specific Collection.
•Implementing your own Collector…
Creating a new mutable result container
Reducing a value into a mutable result container
Accepting two partial results and merging them
Performing an optional final transform on the container
–Two tasks are making progress at overlapping time periods.
–Two tasks are happening at literally the same time.
Task 1 Task 2
37. •The Java runtime performs a concurrent reduction if …
–The stream is parallel.
–The Collector has the characteristic Collector.Characteristics.CONCURRENT
–Either the stream is unordered, or the Collector has the characteristic Collector.Characteristics.UNORDERED.
•Built-in concurrent-supported collector
–Data size、source data structure、number of cores、cost per element …
–Executing in parallel without worrying about tools doesn't mean there is a free lunch.
Don't do this!!
•Functional? You've done that when you are …
–Refactoring your existing code …
–Applying filter, map, reduce …
–Returning Optional instead of null …
–Considering issues of parallelism when using parallel tools, such as parallelStream …
–Or even using those monadic API of Java 8 …
•These are Java ways of functional style; pragmatic ways of understanding and introducing Lambda/Functional API
–Completing Chapter 1
–Using functional API to refactor Customer again
Lambda 就是 重構(重構(程式碼))
Lambda is a way to refactor refactored code