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?

Enhancements in Java 9 Streams

  • 1.
    Java Streams API Enhancementsto Java 9 Corneil du Plessis
  • 2.
    About Me • Workingas 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)
  • 3.
    Quote Divide each difficultyinto as many parts as is feasible and necessary to resolve it. René Descartes Thank you to Venkat Subramaniam
  • 4.
    Streams – APrimer • 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
  • 5.
    Introduction • What areJava Streams? • Aren’t Streams slow? • Enhancements in Java 9
  • 6.
    Streams – Java8 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>
  • 7.
  • 8.
  • 9.
    Streams – PerformanceComparison 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
  • 10.
    Streams – Java9 enhancements • New • takeWhile • dropWhile • ofNullable • Enhanced • iterate
  • 11.
    Streams – Java9 - takeWhile
  • 12.
    Streams – Java9 - dropWhile
  • 13.
    Streams – Java9 – dropWhile / takeWhile • Not useful for unordered collections. • Can be cheaper than filter
  • 14.
    Streams – Java9 - ofNullable
  • 15.
    Streams – Java9 - ofNullable • Improves handling of null. • Removes boilerplate
  • 16.
    Streams – Java9 - iterate
  • 17.
    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?