--- title: Java Programming 2 - Stream API with Lambda Expression & Optional API tags: java, stream, lambda, optional --- # Java Stream API with Lambda Expression & Optional API ## Anonymous Functions ### Syntax ```java= public class LambdaDemo { public static void main(String[] args) { // annoymous class Runnable job1 = new Runnable() { public void run() { /* ...do something ... */ } }; // annoymous function, aka lambda Runnable job2 = () -> { /* ...do something ... */ }; } } } ``` #### References * https://www.oracle.com/technetwork/articles/java/architect-lambdas-part1-2080972.html * https://www.oracle.com/technetwork/articles/java/architect-lambdas-part2-2081439.html * https://stackoverflow.com/questions/27524445/does-a-lambda-expression-create-an-object-on-the-heap-every-time-its-executed * https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.27 * https://www.infoq.com/articles/Java-8-Lambdas-A-Peek-Under-the-Hood/ * https://www.oracle.com/technetwork/java/jvmls2013kuksen-2014088.pdf ## Stream API * A stream pipeline consists of a stream source, followed by zero or more intermediate operations, and a terminal operation. * functional-style #### References * https://shipilev.net/talks/javaday-Nov2013-streamstyle.pdf * https://stackify.com/streams-guide-java-8/ * http://www.angelikalanger.com/Conferences/Slides/LambdasI-Langer.WJAX.pdf * [Processing Data with Java SE 8 Streams](https://www.oracle.com/technetwork/articles/java/ma14-java-se-8-streams-2177646.html) * http://www.cse.chalmers.se/edu/year/2018/course/DAT280_Parallel_Functional_Programming/Material/Java/slides.pdf * https://www.jfokus.se/jfokus16/preso/Lambdas-Streams--Beyond-the-Basics.pdf ### Stream Creation * First of all, Java 8 Streams should not be confused with Java I/O streams (see https://docs.oracle.com/javase/tutorial/essential/io/). * Streams are wrappers around a data source, allowing us to operate with that data source and making bulk processing convenient and fast. * A stream does not store data and, in that sense, is not a data structure. * It also ==never modifies the underlying data source==. * All Stream operations are divided into intermediate and terminal operations and are combined to form **stream pipelines**. * List: * Collections: simply invoking stream() or parallelStream() * Number streams: ``IntStream``, ``LongStream``, ``DoubleStream`` ### Intermediate Operations * Computation on the source data is only performed when the terminal operation is initiated, and source elements are consumed only as needed. * They are **lazy**. * Processing streams lazily allows avoiding examining all the data when that’s not necessary. * This behavior becomes even more important when the input stream is infinite and not just very large. * The operations are as follows: * filter() * map() * flatMap() * distinct() * sorted() * peek() * limit() * skip() ### Ternimal Operations #### API with examples --- #### Why? * The Java API designers are updating the API with a new abstraction called **Stream** that lets you process data in a **declarative** way. * Like SQL. * Furthermore, streams can leverage **multi-core architectures** without you having to write a single line of multithread code. * Also see concurrenct programming. ## Java Optional: alternative to null (a billion-dollar mistake) #### Why? * Null checks are not required (the 3rd way? used to represent a value which is present or absent). * **No more NullPointerException at run-time.** * Can develop clean and neat APIs. * No more Boiler plate code? #### References * https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html * [Java 8 Optional In Depth](https://www.mkyong.com/java8/java-8-optional-in-depth/) * https://www.baeldung.com/java-optional