Streams in Java 8. Start programming in a more functional style

Similar documents
Lambdas in Java 8. Start programming in a more functional style

Java Technologies. Lecture V. Valdas Rapševičius

Java8: Stream Style. Sergey

301AA - Advanced Programming

Principles of Programming Languages

Lambdas & Streams In JDK 8: Beyond The Basics

Functional Constructs in Java 8: Lambdas and Streams

Java SE 8: Lambda Expressions And The Stream API

Refactoring to Functional. Hadi Hariri

Perchance to Stream with Java 8

Multicore Programming

Java SE 8 New Features

LAMBDA EXPRESSIONS AND STREAMS API

Java SE 8 Programming

Java SE 8 Programming

Advanced Java Programming

Java SE 8 Programming

Advanced Programming Methods. Lecture 4 - Functional Programming in Java

JVM ByteCode Interpreter

Common mistakes made with Functional Java. Brian Vermeer

Java 8 Stream Performance Angelika Langer & Klaus Kreft

JAVA. java.lang.stringbuffer java.lang.stringbuilder

Lambdas & Streams: Taking the Hard Work Out of Bulk Operations in Java SE 8.

Java 8 Stream Performance Angelika Langer & Klaus Kreft

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17

CS450 - Structure of Higher Level Languages

Op>onal. The Mother of all Bikesheds. Stuart Marks Core Libraries Java PlaGorm Group, Oracle

ΠΙΝΑΚΑΣ ΠΛΑΝΟΥ ΕΚΠΑΙΔΕΥΣΗΣ

These notes are intended exclusively for the personal usage of the students of CS352 at Cal Poly Pomona. Any other usage is prohibited without

Introduction to Functional Programming in Java 8

Introduction. chapter Functions

Java 8 Programming for OO Experienced Developers

New Features in Java 8

Project Lambda: Functional Programming Constructs and Simpler Concurrency in Java SE 8

CO Java SE 8: Fundamentals

Functional Programming in Java Part 2. CSE 219 Department of Computer Science, Stony Brook University

Collections After Eight. Maurice Naftalin Morningside Light


Fast Track to Core Java 8 Programming for OO Developers (TT2101-J8) Day(s): 3. Course Code: GK1965. Overview

Sequentialising a concurrent program using continuation-passing style

Java 8 Lambdas & Streams Angelika Langer

Free your Lambdas Java SE 8

Java 8 new features Juan Hernández

PIC 20A Anonymous classes, Lambda Expressions, and Functional Programming

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

CS 10: Problem solving via Object Oriented Programming Winter 2017

Discussion 4. Data Abstraction and Sequences

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

Java9 - Features abseits von Jigsaw und JShell. BED-Con September 2017

CPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia


JAVA. java.lang.stringbuffer java.lang.stringbuilder

A Third Look At Java. Chapter Seventeen Modern Programming Languages, 2nd ed. 1

Programming Languages and Techniques (CIS120)

Introduction to Prolog Paper Refs. Prolog tutor. Julian Verdurmen. Seminar Softw. tech. for teaching and learning. September 30, 2009

Formal Specification and Verification

Stop coding Pascal. Saturday, April 6, 13

Pieter van den Hombergh Thijs Dorssers Stefan Sobek. June 8, 2017

D Programming Language

Lambda Expressions In JDK8: Going Beyond The Basics

COMP 322: Fundamentals of Parallel Programming. Lecture 22: Parallelism in Java Streams, Parallel Prefix Sums

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Lecture 10 Notes Linked Lists

20 Most Important Java Programming Interview Questions. Powered by

Collections After Eight. Maurice Naftalin Morningside Light

Index COPYRIGHTED MATERIAL

Programming and Data Structure

Starting to Program in C++ (Basics & I/O)

APCS Semester #1 Final Exam Practice Problems

Lesson 3-4: Using Collectors

Lecture 10 Notes Linked Lists

Murach s Beginning Java with Eclipse

Lecture #5: Higher-Order Functions. A Simple Recursion. Avoiding Recalculation. Redundant Calculation. Tail Recursion and Repetition

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

Homework: More Abstraction, Trees, and Lists

Software Engineering using Formal Methods

The Sun s Java Certification and its Possible Role in the Joint Teaching Material

Comp215: Covariance and Contravariance

CS 215 Software Design Homework 3 Due: February 28, 11:30 PM

+ Abstract Data Types

Introduction to Concepts in Functional Programming. CS16: Introduction to Data Structures & Algorithms Spring 2017

Part III Appendices 165

Functional Programming. Big Picture. Design of Programming Languages

Structural polymorphism in Generic Haskell

Java 8 Functional Programming with Lambdas Angelika Langer

CSc 372. Comparative Programming Languages. 2 : Functional Programming. Department of Computer Science University of Arizona

DRAFT. Dependent types. Chapter The power of and

Lab 4: Imperative & Debugging 12:00 PM, Feb 14, 2018

A Sophomoric Introduction to Shared-Memory Parallelism and Concurrency Lecture 5 Programming with Locks and Critical Sections

// Body of shortestlists starts here y flatmap { case Nil => Nil case x :: xs => findshortest(list(x), xs)

Solutions to Quiz 2 (December 3, 2018)

COMP 322: Fundamentals of Parallel Programming. Lecture 13: Parallelism in Java Streams, Parallel Prefix Sums

What is software testing? Software testing is designing, executing and evaluating test cases in order to detect faults.

CSE332: Data Abstractions Lecture 23: Programming with Locks and Critical Sections. Tyler Robison Summer 2010

Course Description. Learn To: : Intro to JAVA SE7 and Programming using JAVA SE7. Course Outline ::

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

1z0-813.exam.28q 1z0-813 Upgrade to Java SE 8 OCP (Java SE 6 and all prior versions)

This course supports the assessment for Scripting and Programming Applications. The course covers 4 competencies and represents 4 competency units.

Peter Sestoft. Java Precisely. Third Edition. The MIT Press Cambridge, Massachusetts London, England

Transcription:

Streams in Java 8 Start programming in a more functional style

Background Who am I? Tobias Coetzee I m a Technical Lead at BBD I present the Java Expert Level Certifications at BBD (EJB, JPA, etc.) I m currently a banker Part of the JoziJug organising committee I like to run This partially presentation is based on a Simon Ritter course @tobiascode https://github.com/jozi-jug/streamtraining

Outcomes What are the take away s? Know the differences between functional and imperative programming Be able to define the elements of a stream Use intermediate and terminal operations Create streams from different data types Avoid NullPointerExceptions with the Optional Class Debug streams

What Did We Do Last Time?

Lambdas Summary CPU s are getting much faster, but we are getting more cores to use. Inject functionality into methods, the same way we inject values into methods. Lambda syntax and rules, (parameters) -> {lambda body} Functional interfaces are interfaces that have only one abstract method. Lambdas can be used where the type is a functional interface. Use lambdas as method parameters or assign them to variables. The java.util.function package already gives us a few functional interfaces out of the box. Method and constructor references as short hands notation, ::. There are a few new methods that can use lambdas in Java 8, e.g. foreach(consumer c).

What Is Functional Programming?

Functional Programming Remember this game?

Functional Programming Imperative Values associated with names can be changed, i.e. mutable state. The order of execution is defined as a contract. Repetition is external and explicit, we are responsible for the how. Functional (Declarative) Values associated with names are set once and cannot be changed, i.e. immutable state. Order of execution is not defined Repetition is through the use of recursion or internal repetition, the how is hidden away. Thinking about a problem in terms of immutable values and functions that translate between them.

Multithreaded programming, requiring synchronised access to shared, mutable state, is the assembly language of concurrency

The Elements of a Stream

Elements of a Stream What are streams? Streams give us functional blocks to better process collections of data with, monads. We can chain these blocks together to process collections of data. Streams aren t another data structure. Streams can process an infinite list of data. Streams use internal iteration meaning we don t have to code external iteration, the how. Streams support functional programming as suppose to imperative programming.

The purpose of streams isn t just to convert from one collection to another; it s to be able to provide a common set of operations over data

Elements of a Stream Structure of a Stream A stream consists of 3 types of things 1. A source 2. Zero or more intermediate operations 3. A terminal operation Source Intermediate Operation Intermediate Operation Terminal Operation Result result = albums.stream().filter(track -> track.getlength() > 300).map(Track::getName).collect(Collectors.toSet());

Demo: Refactor Code

Elements of a Stream How they work The pipeline is only evaluated when the terminal operation is called. The terminal operations pulls the data, the source doesn t push it. Uses the stream characteristics to help identify optimisations. This allows intermediate operations to be merged: Avoiding multiple redundant passes on data Short-circuit operations Laze evaluation The stream takes care of the how.

Intermediate Operations

Intermediate Operations Intermediate A stream provides a sequence of elements. Supports either sequential or parallel aggregate operations. Most operations take a parameter that describes its behaviour, the what. Typically using lambda expressions. Must be non-interfering Stateless Streams can switch between sequential and parallel, but all processing is either done sequential or parallel, last call wins.

Intermediate Operations Filtering and Mapping Interface filter(predicate) Description Returns a stream with only those elements that return true for the Predicate map(function) maptoint, maptodouble, maptolong distinct() Return a stream where the given Function is applied to each element on the input stream Like map(), but producing streams of primitives rather than objects. Returns a stream with no duplicate elements

Intermediate Operations Map() and FlatMap() Map values from a stream either as one-to-one or oneto-many, but still only produce one stream. map() input stream one-to-one output stream flatmap() input stream one-to-many output stream

Intermediate Operations Sizing and Sorting Interface skip(long) limit(long) sorted(comparator) unordered() Description Returns a stream that skips the first n elements of the input stream Return a stream that only contains the first n elements of the input stream Returns a stream that is sorted with the order determined by the Comparator. With no arguments sorts by natural order. Returns a stream that is unordered (used internally). Can improve efficiency of operations like distinct() and groupingby()

Terminal Operations

Terminal Operations Terminal Terminates the pipeline of the operations on the stream. Only at this point is any processing performed. This allows for optimisation of the pipeline: Lazy evaluation Merged/fused operations Elimination of redundant operations Parallel execution Generates an explicit result of a side effect.

Intermediate Operations Matching Elements and Iteration Interface findfirst(predicate) findany(predicate) allmatch(predicate) anymatch(predicate) nonematch(predicate) Description The first element that matches predicate Like findfirst(), but for a parallel stream All elements in the stream match predicate Any element in the stream matches predicate No elements match the predicate * * foreach(consumer) foreachordered(consumer) Performs an action on each element Like above, but ensures order is respected when used for parallel stream * Encourages non-functional programming style

Intermediate Operations Collecting Results and Numerical Results * Interface collect(collector) toarray() count() max(comparator) min(comparator) average() sum() Description Performs a mutable reduction on a stream Returns an array containing the elements of the stream Returns how many elements are in the stream The maximum value element of the stream, returns an Optional The minimum value element of the stream, returns an Optional Return the arithmetic mean of the stream, returns an Optional Returns the sum of the stream elements * There are a lot of built-in Collectors

Terminal Operations Creating a Single Result The collect function isn t the only option. reduce(binaryoperator accumulator) Also called folding in FP. Performs a reduction on the stream using the BinaryOperator. The accumulator takes a partial result and the next element and returns a new partial result as an Optional. Two other versions One that takes an initial value. One that takes an initial value and BiFunction.

Exercise: Refactor Code

Stream Sources

Stream Sources JDK 8 Libraries There are 95 methods in 23 classes that return a stream, most are intermediate operations though. Leaves 71 methods in 15 classes that can be used as practical sources. There are numerous places to get stream sources. Streams from values Empty streams Streams from functions Streams from arrays Streams from collections Streams from files Streams from other sources

Stream Sources Collection Interface stream() Provides a sequential stream of elements in the collection. parallelstream() Provides a parallel stream of elements in the collection. Uses the fork-join framework for implementation. Only Collection can provide a parallel stream directly. Arrays Class stream() Array is a collection of data, so logical to be able to create a stream. Provides a sequential stream. Overloaded methods for different types, int, double, long, Object

Stream Sources Some other Classes Files Class: find, list, lines, walk. Random numbers to produce finite or infinite streams with or without seeds: Random, ThreadLocalRandom, SplittableRandom. BufferReader, JarFile, ZipFile, Pattern, Charsequence, etc.

Stream Sources Primitive Streams IntStream, DoubleStream, LongStream are primitive specialisations of the Stream interface. range(int, int), rangeclosed(int, int) A stream from a start to an end value (exclusive or inclusive) generate(intsupplier), iterate(int, IntUnaryOperator) An infinite stream created by a given Supplier. iterate uses a seed to start the stream.

Optional

Optional Problems with null Certain situations in Java return a result which is a null, i.e. the reference to an object isn t set. It tries to help eliminate NullPointerException s. Terminal operations like min(), max(), may not return a direct result, suppose the input stream is empty? Introducing Optional<T>: It is a container for an object reference (null, or real object). It is a stream with either 0 or 1 elements, but never null. Can be used in powerful ways to provide complex conditional handling. Doesn t stop developers from returning null, but an Optional tells you do maybe rather check. Was added for the Stream API, but you can also use it.

Optional Creating an Optional <T> Optional<T> empty(): Returns an empty Optional. <T> Optional<T> of(t value): Returns an Optional containing the specified value. If the specified value is null, it throws a NullPointerException. <T> Optional<T> ofnullable(t value): Same as above, but if the specified value is null, it returns and empty Optional.

Optional Getting Data from an Optional <T> get(): Returns the value, but will throw NoSuchElementException is value is empty. <T> orelse(t defaultvalue): Returns the default value if value is empty. <T> orelseget(supplier<? Extends T> defaultsupplier): Same as above, but supplier gets the value. <X extends Throwable> T orelsethrow(supplier<? Extends T> exceptionsupplier): If empty throw the exception from the supplier.

Demo: Refactor to Optional

Exercise: Optional

Debugging Streams

Debugging Streams Solution Most IDE s will support debugging Streams and breakpoints on streams. As you can pass in the behaviour for each operation via a lambda you can always add a System.out.println() or logging as part of the behaviour. If you don t want to mess around with your current behaviour you can use the peek operation. The peek(consumer <? Super T> action) methods was added for debugging and should only be used for debugging. Each element will pass through it and the action will be applied to it.

Homework

Homework Exercises How does it work? The exercises for this month are all small exercises that you will complete using the things you have learned during this presentation. There are 21 exercises to complete. For first month focus on using lambda expressions, method references and some of the new methods that have been added to existing classes. There is a template source file that you should use to create the answers to the exercises called Test.java under exercises package the test folder. To simplify things the lists and maps you need for the exercises have already been created. You just need to focus on the code to solve the exercise. The solutions are in the Test.java file in the solutions package, but try all the exercises first before taking a peak.

Books

Books on Lambdas and Streams

Thank you!