Lambda Expressions.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

Java 8 Stream API Raj Thavamani Application Developer / Java Group Biomedical Informatics.
FlumeJava Easy, Efficient Data-Parallel Pipelines Mosharaf Chowdhury.
Java 8 Technion – Institute of Technology Software Design (236700) Based on slides by: Sagie Davidovich, Assaf Israel Author: Assaf Israel - Technion 2013.
Spark: Cluster Computing with Working Sets
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Lecture 2: Design and Implementation of Lambda Expressions in Java 8 Alfred V. Aho CS E6998-1: Advanced Topics in Programming Languages.
The different kinds of variables in a Java program.
CS525: Big Data Analytics MapReduce Languages Fall 2013 Elke A. Rundensteiner 1.
Proxy Design Pattern Source: Design Patterns – Elements of Reusable Object- Oriented Software; Gamma, et. al.
Chapter 17 Java SE 8 Lambdas and Streams
Main sponsor. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 2 Project Lambda: Functional Programming Constructs & Simpler Concurrency.
Appendix B: Lambda Expressions In the technical keynote address for JavaOne 2013, Mark Reinhold, chief architect for the Java Platform Group at Oracle,
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
Monads Technion – Institute of Technology Software Design (236700) Author: Gal Lalouche - Technion 2015 © 1.
Java 8 Lambda expressions and you Technion – Institute of Technology Software Design (236700) Based on slides by: Sagie Davidovich, Assaf Israel Author:
Lambdas and Streams. Functional interfaces Functional interfaces are also known as single abstract method (SAM) interfaces. Package java.util.function.
MapReduce High-Level Languages Spring 2014 WPI, Mohamed Eltabakh 1.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
Lambda Expressions In the technical keynote address for JavaOne 2013, Mark Reinhold, chief architect for the Java Platform Group at Oracle, described lambda.
Java8 Released: March 18, Lambda Expressions.
Sets and Maps Part of the Collections Framework. 2 The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
JAVA 8 AND FUNCTIONAL PROGRAMMING. What is Functional Programming?  Focus on Mathematical function computations  Generally don’t think about “state”
Kyung Hee University Class Diagramming Notation OOSD 담당조교 석사과정 이정환.
Lambdas and Streams. Stream manipulation Class Employee represents an employee with a first name, last name, salary and department and provides methods.
Lecture 5 – Function (Part 2) FTMK, UTeM – Sem /2014.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Chapter 5 – Part 3 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/19 Outline The if Statement and Conditions Other Conditional.
Functional Processing of Collections (Advanced) 6.0.
Lambdas & Streams Laboratory
1 © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. for-each PROS easy to use access to ALL items one-by-one ability to change the state.
PySpark Tutorial - Learn to use Apache Spark with Python
for-each PROS CONS easy to use access to ALL items one-by-one
Advanced Java Programming 51037
CSCI5570 Large Scale Data Processing Systems
Exceptions David Rabinowitz.
CarbonData Data Load Design
Taking Java from purely OOP by adding “functional level Programming”
The Singleton Pattern SE-2811 Dr. Mark L. Hornick.
Lambda Expressions By Val Feldsher.
Concurrency, Processes and Threads
Operating System (013022) Dr. H. Iwidat
Functional Processing of Collections (Advanced)
Java Review: Reference Types
Java Algorithms.
Recursion Great fleas have little fleas upon their backs to bite 'em, And little fleas have lesser fleas, and so ad infinitum. And the great fleas themselves,
Functional Programming with Java
Object Oriented Programming
Java 8 Java 8 is the biggest change to Java since the inception of the language Lambdas are the most important new addition Java is playing catch-up: most.
Chapter 13 Control Structures
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Streams.
14 The Stream API.
Conditional Statements
COS 260 DAY 10 Tony Gauvin.
Functional interface.
COS 260 DAY 23 Tony Gauvin.
A few of the more significant changes and additions. James Brucker
Focus of the Course Object-Oriented Software Development
Computer Science 312 Composing Functions Streams in Java 1.
Introduction to Spark.
Objectives You should be able to describe: The while Statement
COS 260 DAY 23 Tony Gauvin.
Lazy Evaluation CSCE 314: Programming Languages Dr. Dylan Shell
Chap 7. Advanced Control Statements in Java
Concurrency, Processes and Threads
„Lambda expressions, Optional”
Chapter 13 Control Structures
Threads and concurrency / Safety
Presentation transcript:

Lambda Expressions

How did Java deal with functional programming prior to Java8

Another example of anon class

Our first Lambda expression

Another example

Another example

Type of Lambda Expression The java ‘Type’ of a lamba expression is a “Functional Interface” Functional Interface is an interface with only ONE abstract method The concept of “Functional Interface” is new, but many of the interfaces in Java7 and before are “functional interfaces” such as Runnable, Comparable, etc. Java8 defines many more functional interfaces in the java.util.function.* pacakge.

How to create a Functional Interface Methods overwritten from Object don’t count, so this is a @FunctionalInterface

Can I store a Lamba Expression in a variable in Java? Yes! Wherever a method requires an anonymous inner class (with one method), you may put a lamba expression. For example: Collections.sort(list, compL); You also use lambda expressions with Streams

Is a Lambda expression an Object? Not really. It is an ‘object without identity’. It does not inherit from Object and so you can’t call the .equals(), .hashcode(), etc. There is no ‘new’ keyword in lamba, so there is far less overhead both at compile- and run-time.

Location of Functional Interfaces You may define your own “Functional Interfaces” Java8 defines many in: java.util.function.* 43 interfaces in 4 categories

What is a Stream A stream is a limitless iterator that allows you to process collections. You can chain operations. This chain is called a pipeline. You must have at least one terminal operation and zero or more intermediary operations. The pipeline usually takes the form of map-filter-reduce pattern. Any stream operation that returns a Stream<T> is an intermediate operation, and any object that returns void is terminal. Intermediate operations are lazy, whereas terminal operations are eager. A terminal operation will force the stream to be “spent” and you can NOT re-use that reference, though you can always get a new stream. May be parallelized and optimized across cores.

4 categories of Functional Interfaces Functional Interface archetypes Example used in Consumer forEach(Consumer), peek(Consumer) Predicate filter(Predicate) Function map(Function) Supplier reduce(Supplier) collect(Supplier)

4 categories of Functional Interfaces

You may omit the type here because Java infers the type

Method references

Map is a transform The .map() method is not an associative data structure, rather it is a transformative operation. It takes a Stream<T> and it returns either a Stream<T> or Stream<U>. Example: Stream<String> to Stream<String> Example: Stream<String> to Stream<Date>

Intermediary operation Intermediary operation: A method that takes a Stream and returns a Stream. They are lazily loaded and will only be executed if you include a terminal operation at the end. The peek() method The map() method The filter() method

Terminal operation Terminal operation: A method that takes a Stream<T> and returns void. They are eagerly loaded and will cause the entire pipeline to be executed. A terminal operation will “spend” the stream. The forEach() method The count() method The max() method The collect() method The reduce() method