Enhancements in Java 9 Streams
-
Upload
corneil-du-plessis -
Category
Software
-
view
45 -
download
1
Transcript of Enhancements in Java 9 Streams
Java Streams API
Enhancements to Java 9
Corneil du Plessis
About Me
• Working as Programmer since 1985
• Smallest to very large systems.
• Cobol, Fortran, Pascal, Algol, C/C++, Java, JavaScript, Groovy, Python, Scala, Kotlin and more.
• Scientific instrumentation, Sports event management, Mining, Banking, Treasury and Insurance.
• Software Architect (with a preference for coding)
Quote
Divide each difficulty into as many parts as is feasible and necessary to resolve it.
René Descartes
Thank you to Venkat Subramaniam
Streams – A Primer
• Streams are Monads
• In functional programming, a monad is a structure that represents computations defined as sequences of steps. A type with a monad structure defines what it means to chain operations, or nest functions of that type together.
• Simplistic: Pipeline of Iterators where you apply functions to create new Collections
Introduction
• What are Java Streams?• Aren’t Streams slow?• Enhancements in Java 9
Streams – Java 8
Name Description Result
filter Returns a stream with elements matching predicate Stream<T>
map Returns a stream Of type and values return by mapper Stream<R>
limit Truncate the stream Stream<T>
skip Returns a stream after first N elements Stream<T>
distinct Returns a stream with only unique elements using equals Stream<T>
sorted Returns a stream with elements sorted using natural order or comparator
Stream<T>
collect Use collector to create new object like List C
reduce Applies reduction function like concatenation or summation R
forEach Applies function to elements void
iterate Produces stream using seed and function Stream<T>
Streams – Java 8
Streams – Java 8
Streams – Performance Comparison
Method Throughput Error
m1 (Imperative) 1653681.197 ops/s 24178.799 ops/s
m2 (Streams) Java 8 1705339.328 ops/s 22440.688 ops/s
m2 (Streams) Java 9 1858807,026 ops/s 10793,995 ops/s
Streams – Java 9 enhancements
• New• takeWhile
• dropWhile
• ofNullable
• Enhanced• iterate
Streams – Java 9 - takeWhile
Streams – Java 9 - dropWhile
Streams – Java 9 – dropWhile / takeWhile
• Not useful for unordered collections.
• Can be cheaper than filter
Streams – Java 9 - ofNullable
Streams – Java 9 - ofNullable
• Improves handling of null.
• Removes boilerplate
Streams – Java 9 - iterate
Finally
• Slides - https://www.slideshare.net/CorneilduPlessis/enhancements-in-java-9-streams
• Source Code - https://github.com/corneil/streams-samples
• Resources
• http://www.oracle.com/technetwork/articles/java/ma14-java-se-8-streams-2177646.html
• http://www.baeldung.com/java-9-stream-api
Questions?