O SlideShare utiliza cookies para otimizar a funcionalidade e o desempenho do site, assim como para apresentar publicidade mais relevante aos nossos usuários. Se você continuar a navegar o site, você aceita o uso de cookies. Leia nosso Contrato do Usuário e nossa Política de Privacidade.
O SlideShare utiliza cookies para otimizar a funcionalidade e o desempenho do site, assim como para apresentar publicidade mais relevante aos nossos usuários. Se você continuar a utilizar o site, você aceita o uso de cookies. Leia nossa Política de Privacidade e nosso Contrato do Usuário para obter mais detalhes.
Filtering tracks over a minute and collecting to a List
Mapping tracks to names and collecting to a Set
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
•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.
•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.
•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.
•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.
•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)
•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.
•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.
–Sub interface: UnaryOperator<T>
•Fail fast examples if an Optional instance contains no value.
•Avoiding performance overhead.
•To avoid performance overheads, the streams library has primitive versions. They have a clear- cut naming convention.
•Streams for primitives.
•Functional interfaces for primitives.
•Functional interfaces for mapping objects and primitives.
•Several functional interfaces starts with Bi- prefix. Take BiConsumer<T, U> for an example.
•How long of all long tracks?
•Reduce? Sometimes it's called as fold(left).
•The max length?
–Terminal operations that return one value by reducing the contents of a stream.
–(Reduce, fold or combine, whatever you call it.)
•How to reduce the elements of a stream to a more complex object, such as a collection?
•Using collect methods…
•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 …
•Grouping and reducing …
•Grouping and averaging …
•The stream library picks an appropriate implementation for you.
•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
•Implementing your own Collector, a simple example …
–Two tasks are making progress at overlapping time periods.
–Two tasks are happening at literally the same time.
Task 1 Task 2
•Changing a single method call…
•Making an operation execute in parallel…
•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