Java8 Released: March 18, 2014. Lambda Expressions.

Slides:



Advertisements
Similar presentations
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Parallelism & Locality Optimization.
Advertisements

Programming Languages and Paradigms
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha Control Flow-II: Execution Order.
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
Names and Bindings.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Spark: Cluster Computing with Working Sets
MATHEMATICAL FOUNDATIONS SUPPLEMENTAL MATERIAL – NOT ON EXAM.
DATAFLOW PROCESS NETWORKS Edward A. Lee Thomas M. Parks.
Comp 205: Comparative Programming Languages Semantics of Imperative Programming Languages denotational semantics operational semantics logical semantics.
Road Map Introduction to object oriented programming. Classes
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes Names Variables The Concept of Binding Type Checking Strong Typing Type Compatibility.
Control Structures. Hierarchical Statement Structure Standard in imperative languages since Algol60. Exceptions: Early FORTRAN, COBOL, early BASIC, APL.
Lecture 2: Design and Implementation of Lambda Expressions in Java 8 Alfred V. Aho CS E6998-1: Advanced Topics in Programming Languages.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Chapter 17 Java SE 8 Lambdas and Streams
Appendix B: Lambda Expressions In the technical keynote address for JavaOne 2013, Mark Reinhold, chief architect for the Java Platform Group at Oracle,
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
Functional Programing Referencing material from Programming Language Pragmatics – Third Edition – by Michael L. Scott Andy Balaam (Youtube.com/user/ajbalaam)
Lambdas and Streams. Functional interfaces Functional interfaces are also known as single abstract method (SAM) interfaces. Package java.util.function.
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.
Chapter 1 - Introduction
MapReduce High-Level Languages Spring 2014 WPI, Mohamed Eltabakh 1.
1 Introduction Modules  Most computer programs solve much larger problem than the examples in last sessions.  The problem is more manageable and easy.
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
What does a computer program look like: a general overview.
Introduction A variable can be characterized by a collection of properties, or attributes, the most important of which is type, a fundamental concept in.
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
PROGRAMMING LANGUAGES: PROLOG, CLOJURE, F# Jared Wheeler.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Inside LINQ to Objects How LINQ to Objects work Inside LINQ1.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Lambda Expressions In the technical keynote address for JavaOne 2013, Mark Reinhold, chief architect for the Java Platform Group at Oracle, described lambda.
Functional Programming IN NON-FUNCTIONAL LANGUAGES.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Classes, Interfaces and Packages
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
Names, Scope, and Bindings Programming Languages and Paradigms.
JAVA 8 AND FUNCTIONAL PROGRAMMING. What is Functional Programming?  Focus on Mathematical function computations  Generally don’t think about “state”
Lambdas and Streams. Stream manipulation Class Employee represents an employee with a first name, last name, salary and department and provides methods.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Functional Processing of Collections (Advanced) 6.0.
Lambdas & Streams Laboratory
Polymorphism in Methods
Advanced Java Programming 51037
Java Yingcai Xiao.
Taking Java from purely OOP by adding “functional level Programming”
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
Functional Processing of Collections (Advanced)
Imperative languages Most familiar: computation modifies store
Java Algorithms.
Functional Programming with Java
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.
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
Notable Java 8 New Features – Not including Lambda S Marvasti
Control Flow Chapter 6.
Packages and Interfaces
COS 260 DAY 10 Tony Gauvin.
Functional interface.
A few of the more significant changes and additions. James Brucker
Computer Science 312 Composing Functions Streams in Java 1.
Computer Science 312 What’s New in Java 8? 1.
Lambda Expressions.
Instructor: Alexander Stoytchev
Java’s Stream API.
Threads and concurrency / Safety
Presentation transcript:

Java8 Released: March 18, 2014

Lambda Expressions

Imperative versus Declarative Imperative: is a style of programming where you program the algorithm with control flow and explicit steps. Declarative: is a style of programming where you declare what needs be done without concern for the control flow. Functional programming: is a declarative programming paradigm that treats computation as a series of functions and avoids state and mutable data to facilitate concurrency. “Functional programming has its roots in lambda calculus, a formal system developed in the 1930s to investigate computability, the Entscheidungsproblem, function definition, function application, and recursion. Many functional programming languages can be viewed as elaborations on the lambda calculus”. Wikipedia See BigDecimalMain

How did Java deal with “functional” programming prior to Java8 See imperativeVersusFunctional

Another example of anon class

Our first Lambda expression 1/ start with a “functional interface” - a functional interface has ONE abstract method. 2/ create an anonymous class and implement its method 3/ use the format as seen above 4/ no need for return statements, that's implied

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. Older functional interfaces have been decorated with default methods to preserve backwards compatibility

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

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

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 may also use lambda expressions with Streams See functionalIterfaceTest

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.

Streams

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 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. See StreamMain

Iterator See IteratorDriver

4 categories of Functional Interfaces Functional Interface archetypes Example used in Consumer forEach(Consumer), peek(Consumer) Predicatefilter(Predicate) Functionmap(Function) Supplierreduce(Supplier) collect(Supplier) See java.util.function.* ConsumerMain

4 categories of Functional Interfaces

Method references You can refer to static or non-static method simply by using the double colon (method reference) notation like so: System.out::println stringLenComparator::compare See MethodRefsMain

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

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 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

New Time API in Java8

From pluralsight.com