Main sponsor. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 2 Project Lambda: Functional Programming Constructs & Simpler Concurrency.

Slides:



Advertisements
Similar presentations
DATA STRUCTURES Lecture: Interfaces Slides adapted from Prof. Steven Roehrig.
Advertisements

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | JSON Pointer and JSON Patch Updates to Java API for JSON Processing Kin-man Chung.
Guide To UNIX Using Linux Third Edition
Lecture 2: Design and Implementation of Lambda Expressions in Java 8 Alfred V. Aho CS E6998-1: Advanced Topics in Programming Languages.
Interfaces and Inner Classes. What is an Interface?  What is “presented to the user”?  The public part of a class?  What is the substance of an interface?
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
Chapter 17 Java SE 8 Lambdas and Streams
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Writing Classes (Chapter 4)
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Lambdas and Streams. Functional interfaces Functional interfaces are also known as single abstract method (SAM) interfaces. Package java.util.function.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
The Java Programming Language
Chapter 06 (Part I) Functions and an Introduction to Recursion.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Mohsen Zainalpour What`s New in Java 8 JUG.
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Introduction to Generics
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Salman Marvasti Sharif University of Technology Winter 2015.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
RESTful Microservices In Java With Jersey Jakub Podlešák Software Engineer Oracle, Application Server Group September 29, 2014 Copyright © 2014, Oracle.
Final Review. From ArrayLists to Arrays The ArrayList : used to organize a list of objects –It is a class in the Java API –the ArrayList class uses an.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Java8 Released: March 18, Lambda Expressions.
Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Mid-Year Review. Coding Problems In general, solve the coding problems by doing it piece by piece. Makes it easier to think about Break parts of code.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
Part 1: Composition, Aggregation, and Delegation Part 2: Iterator COMP 401 Fall 2014 Lecture 10 9/18/2014.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Lambdas & Streams Laboratory
Object Oriented Programming in
Taking Java from purely OOP by adding “functional level Programming”
Inheritance and Polymorphism
Java 9 New features 8/11/2017 Iason Dimitrios Rodis.
Functional Programming with Java
Packages and Interfaces
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Functional interface.
A few of the more significant changes and additions. James Brucker
In-Class Exercise: Lambda Expressions
CIS 199 Final Review.
Classes, Objects and Methods
„Lambda expressions, Optional”
Threads and concurrency / Safety
Presentation transcript:

Main sponsor

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 2 Project Lambda: Functional Programming Constructs & Simpler Concurrency In Java SE 8 Simon Ritter Head of Java Evangelism Oracle Corporation

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 3 The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 4 Lambda (JSR 335) Date/Time API (JSR 310) Type Annotations (JSR 308) Compact Profiles Lambda-Form Representation for Method Handles Remove the Permanent Generation Improve Contended Locking Generalized Target-Type Inference DocTree API Parallel Array Sorting Bulk Data Operations Unicode 6.2 Base64 Prepare for Modularization Parameter Names TLS Server Name Indication Configurable Secure-Random Number Generation Java 8 Nashorn Enhanced Verification Errors Fence Intrinsics Repeating Annotations HTTP URL Permissions Limited doPrivileged

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 5 Project Lambda: Some Background

Copyright © 2012, Oracle and/or its affiliates. All rights reserved /40’s 1950/60’s 1970/80’s Images – wikipedia / bio pages

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 7 Computing Today  Multicore is now the default – Moore’s law means more cores, not faster clockspeed  We need to make writing parallel code easier  All components of the Java SE platform are adapting – Language, libraries, VM 360 Cores 2.8 TB RAM 960 GB Flash InfiniBand … Herb Sutter

Copyright © 2012, Oracle and/or its affiliates. All rights reserved java.lang.Thread java.util.concurrent (jsr166) Fork/Join Framework (jsr166y) Project Lambda Concurrency in Java Phasers, etc (jsr166)

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 9 Goals For Better Parallelism In Java  Easy-to-use parallel libraries – Libraries can hide a host of complex concerns  task scheduling, thread management, load balancing, etc  Reduce conceptual and syntactic gap between serial and parallel expressions of the same computation – Currently serial code and parallel code for a given computation are very different  Fork-join is a good start, but not enough  Sometimes we need language changes to support better libraries – Lambda expressions

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 10 Bringing Lambdas To Java

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 11 The Problem: External Iteration List students =... double highestScore = 0.0; for (Student s : students) { if (s.gradYear == 2011) { if (s.score > highestScore) { highestScore = s.score; } Client controls iteration Inherently serial: iterate from beginning to end Not thread-safe because business logic is stateful (mutable accumulator variable)

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 12 Internal Iteration With Inner Classes  Iteraction, filtering and accumulation are handled by the library  Not inherently serial – traversal may be done in parallel  Traversal may be done lazily – so one pass, rather than three  Thread safe – client logic is stateless  High barrier to use – Syntactically ugly More Functional, Fluent and Monad Like List students =... double highestScore = students.filter(new Predicate () { public boolean op(Student s) { return s.getGradYear() == 2011; } }).map(new Mapper () { public Double extract(Student s) { return s.getScore(); } }).max();

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 13 Internal Iteration With Lambdas SomeList students =... double highestScore = students.stream().filter(Student s -> s.getGradYear() == 2011).map(Student s -> s.getScore()).max(); More readable More abstract Less error-prone No reliance on mutable state Easier to make parallel

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 14 Lambda Expressions  Lambda expressions represent anonymous functions – Like a method, has a typed argument list, a return type, a set of thrown exceptions, and a body – Not associated with a class Some Details double highestScore = students.stream().filter(Student s -> s.getGradYear() == 2011).map(Student s -> s.getScore()).max();

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 15 Lambda Expression Types Single-method interfaces used extensively to represent functions and callbacks – Definition: a functional interface is an interface with one method (SAM) – Functional interfaces are identified structurally – The type of a lambda expression will be a functional interface  This is very important interface Comparator { boolean compare(T x, T y); } interface FileFilter { boolean accept(File x); } interface Runnable { void run(); } interface ActionListener { void actionPerformed(…); } interface Callable { T call(); }

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 16 Target Typing  A lambda expression is a way to create an instance of a functional interface – Which functional interface is inferred from the context – Works both in assignment and method invocation contexts Comparator c = new Comparator () { public int compare(String x, String y) { return x.length() - y.length(); } }; Comparator c = (String x, String y) -> x.length() - y.length();

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 17 Local Variable Capture Lambda expressions can refer to effectively final local variables from the enclosing scope Effectively final means that the variable meets the requirements for final variables (e.g., assigned once), even if not explicitly declared final This is a form of type inference void expire(File root, long before) {... root.listFiles(File p -> p.lastModified() <= before);... }

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 18 Lexical Scoping The meaning of names are the same inside the lambda as outside A ‘this’ reference – refers to the enclosing object, not the lambda itself Think of ‘this’ as a final predefined local Remember the type of a Lambda is a functional interface class SessionManager { long before =...; void expire(File root) {... // refers to ‘this.before’, just like outside the lambda root.listFiles(File p -> checkExpiry(p.lastModified(), this.before)); } boolean checkExpiry(long time, long expiry) {... } }

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 19 Type Inferrence  Compiler can often infer parameter types in lambda expression  Inferrence based on the target functional interface’s method signature  Fully statically typed (no dynamic typing sneaking in) – More typing with less typing Collections.sort(ls, (String x, String y) -> x.length() - y.length()); Collections.sort(ls, (x, y) -> x.length() - y.length());

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 20 Method References Method references let us reuse a method as a lambda expression FileFilter x = (File f) -> f.canRead(); FileFilter x = File::canRead; FileFilter x = new FileFilter() { public boolean accept(File f) { return f.canRead(); } };

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 21 Constructor References  When f.make() is invoked it will return a new ArrayList interface Factory { T make(); } Factory > f = ArrayList ::new; Factory > f = () -> return new ArrayList (); Equivalent to

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 22 Library Evolution

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 23 Library Evolution Adding lambda expressions is a big language change If Java had them from day one, the APIs would definitely look different  Most important APIs (Collections) are based on interfaces How to extend an interface without breaking backwards compatability? Adding lambda expressions to Java, but not upgrading the APIs to use them, would be silly Therefore we also need better mechanisms for library evolution The Real Challenge

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 24 Library Evolution Goal  Requirement: aggregate operations on collections – New methods required on Collections to facilitate this – forEach, stream, parallelStream  This is problematic – Can’t add new methods to interfaces without modifying all implementations – Can’t necessarily find or control all implementations int heaviestBlueBlock = blocks.stream().filter(b -> b.getColor() == BLUE).map(Block::getWeight).reduce(0, Integer::max);

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 25 Solution: Virtual Extension Methods Specified in the interface From the caller’s perspective, just an ordinary interface method List class provides a default implementation Default is only used when implementation classes do not provide a body for the extension method Implementation classes can provide a better version, or not AKA Defender Methods interface Collection { default Stream stream() { return StreamSupport.stream(spliterator()); }

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 26 Virtual Extension Methods Err, isn’t this implementing multiple inheritance for Java? Yes, but Java already has multiple inheritance of types This adds multiple inheritance of behavior too But not state, which is where most of the trouble is Though can still be a source of complexity due to separate compilation and dynamic linking Stop right there!

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 27 Functional Interface Definitions  Single Abstract Method (SAM) type  A functional interface is an interface that has one abstract method – Represents a single function contract – Doesn’t mean it only has one method  Abstract classes may be considered later annotation – Helps ensure the functional interface contract is honoured – Compiler error if not a SAM

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 28 The Stream Class  Stream – A sequence of elements supporting sequential and parallel operations – Evaluated in lazy form – Collection.stream() – Collection.parallelStream() java.util.stream List names = Arrays.asList(“Bob”, “Alice”, “Charlie”); System.out.println(names.stream(). filter(e -> e.getLength() > 4). findFirst(). get());

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 29 java.util.function Package  Predicate – Determine if the input of type T matches some criteria  Consumer – Accept a single input argumentof type T, and return no result  Function – Apply a function to the input type T, generating a result of type R  Plus several more

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 30 Lambda Expressions in Use

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 31 public class Person { public enum Gender { MALE, FEMALE }; String name; Date birthday; Gender gender; String Address; public String getName() {... } public Gender getGender() {... } public String get Address() {... } public void printPerson() { //... } Simple Java Data Structure List membership;

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 32 Searching For Specific Characteristics (1) Simplistic, Brittle Approach public static void printPeopleOlderThan(List members, int age) { for (Person p : members) { if (p.getAge() > age) p.printPerson(); } } public static void printPeopleYoungerThan(List members, int age) { //... } // And on, and on, and on...

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 33 Searching For Specific Characteristics (2) Separate Search Criteria /* Single abstract method type */ interface PeoplePredicate { public boolean satisfiesCriteria(Person p); } public static void printPeople(List members, PeoplePredicate match) { for (Person p : members) { if (match.satisfiesCriteria(p)) p.printPerson(); } }

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 34 Searching For Specific Characteristics (3) Separate Search Criteria Using Anonymous Inner Class printPeople(membership, new PeoplePredicate() { public boolean satisfiesCriteria(Person p) { if (p.gender == Person.Gender.MALE && p.getAge() >= 65) return true; return false; } });

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 35 Searching For Specific Characteristics (4)  We now have parameterised behaviour, not just values – This is really important – This is why Lambda statements are such a big deal in Java Separate Search Criteria Using Lambda Expression printPeople(membership, p -> p.getGender() == Person.Gender.MALE && p.getAge() >= 65);

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 36 Make Things More Generic (1) interface PeoplePredicate { public boolean satisfiesCriteria(Person p); } /* From java.util.function class library */ interface Predicate { public boolean test(T t); }

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 37 Make Things More Generic (2) public static void printPeopleUsingPredicate( List members, Predicate predicate) { for (Person p : members) { if (predicate.test()) p.printPerson(); } } printPeopleUsingPredicate(membership, p -> p.getGender() == Person.Gender.MALE && p.getAge() >= 65); Interface defines behaviour Call to method executes behaviour Behaviour passed as parameter

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 38 Using A Consumer (1) java.util.function interface Consumer { public void accept(T t); } public void processPeople(List members, Predicate predicate, Consumer consumer) { for (Person p : members) { if (predicate.test(p)) consumer.accept(p); } }

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 39 Using A Consumer (2) processPeople(membership, p -> p.getGender() == Person.Gender.MALE && p.getAge() >= 65, p -> p.printPerson()); processPeople(membership, p -> p.getGender() == Person.Gender.MALE && p.getAge() >= 65, Person::printPerson);

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 40 Using A Return Value (1) java.util.function interface Function { public R apply(T t); } public static void processPeopleWithFunction( List members, Predicate predicate, Function function, Consumer consumer) { for (Person p : members) { if (predicate.test(p)) { String data = function.apply(p); consumer.accept(data); } } }

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 41 Using A Return Value (2) processPeopleWithFunction( membership, p -> p.getGender() == Person.Gender.MALE && p.getAge() >= 65, p -> p.get Address(), -> System.out.println( )); processPeopleWithFunction( membership, p -> p.getGender() == Person.Gender.MALE && p.getAge() >= 65, Person::get Address, System.out::println);

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 42 Conclusions  Java needs lambda statements for multiple reasons – Significant improvements in existing libraries are required – Replacing all the libraries is a non-starter – Compatibly evolving interface-based APIs has historically been a problem  Require a mechanism for interface evolution – Solution: virtual extension methods – Which is both a language and a VM feature – And which is pretty useful for other things too  Java SE 8 evolves the language, libraries, and VM together

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 43