Enhancements in Java 9 Streams

17
Java Streams API Enhancements to Java 9 Corneil du Plessis

Transcript of Enhancements in Java 9 Streams

Page 1: Enhancements in Java 9 Streams

Java Streams API

Enhancements to Java 9

Corneil du Plessis

Page 2: Enhancements in Java 9 Streams

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)

Page 3: Enhancements in Java 9 Streams

Quote

Divide each difficulty into as many parts as is feasible and necessary to resolve it.

René Descartes

Thank you to Venkat Subramaniam

Page 4: Enhancements in Java 9 Streams

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

Page 5: Enhancements in Java 9 Streams

Introduction

• What are Java Streams?• Aren’t Streams slow?• Enhancements in Java 9

Page 6: Enhancements in Java 9 Streams

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>

Page 7: Enhancements in Java 9 Streams

Streams – Java 8

Page 8: Enhancements in Java 9 Streams

Streams – Java 8

Page 9: Enhancements in Java 9 Streams

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

Page 10: Enhancements in Java 9 Streams

Streams – Java 9 enhancements

• New• takeWhile

• dropWhile

• ofNullable

• Enhanced• iterate

Page 11: Enhancements in Java 9 Streams

Streams – Java 9 - takeWhile

Page 12: Enhancements in Java 9 Streams

Streams – Java 9 - dropWhile

Page 13: Enhancements in Java 9 Streams

Streams – Java 9 – dropWhile / takeWhile

• Not useful for unordered collections.

• Can be cheaper than filter

Page 14: Enhancements in Java 9 Streams

Streams – Java 9 - ofNullable

Page 15: Enhancements in Java 9 Streams

Streams – Java 9 - ofNullable

• Improves handling of null.

• Removes boilerplate

Page 16: Enhancements in Java 9 Streams

Streams – Java 9 - iterate

Page 17: Enhancements in Java 9 Streams

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?