Computer Science 312 Lambdas in Java 1.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same.
Chapter 7: User-Defined Functions II
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been.
Defining classes and methods Recitation – 09/(25,26)/2008 CS 180 Department of Computer Science, Purdue University.
Overloading methods review When is the return statement required? What do the following method headers tell us? public static int max (int a, int b)
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Introduction to Computers and Programming Strings Professor: Evan Korth New York University.
Lecture From Chapter 6 & /8/10 1 Method of Classes.
Computer Science 209 The Strategy Pattern II: Emulating Higher-Order Functions.
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.
Lambdas and Streams. Functional interfaces Functional interfaces are also known as single abstract method (SAM) interfaces. Package java.util.function.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Computer Science 111 Fundamentals of Programming I Default and Optional Parameters Higher-Order Functions.
Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings.
Chapter 5 : Methods Part 2. Returning a Value from a Method  Data can be passed into a method by way of the parameter variables. Data may also be returned.
Classes - Intermediate
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Methods.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Functional Programming and Stream API
Taking Java from purely OOP by adding “functional level Programming”
Software Development Java Collections
Recitation 11 Lambdas added to Java 8.
Multiple variables can be created in one declaration
Road Map Introduction to object oriented programming. Classes
Lambdas ---anonymous functions---
The Strategy Pattern II: Emulating Higher-Order Functions
Generics, Lambdas, Reflections
Functional Processing of Collections (Advanced)
Functional Programming
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.
Chapter 4 Procedural Methods.
امکانات جدید جاوا در نسخه 8 Java 8 Features
תוכנה 1 בשפת Java שיעור מספר 10: חידושים ב Java 8
Streams.
An Introduction to Java – Part II
תוכנה 1 בשפת Java שיעור מספר 11: חידושים ב Java 8
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Object Oriented Programming in java
Java Lesson 36 Mr. Kalmes.
Chapter 7 The Java Array Object © Rick Mercer.
Functional interface.
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Effective Java, 3rd Edition Chapter 7: Lambdas and Streams
Chapter 7 Procedural Methods.
METHODS, CLASSES, AND OBJECTS A FIRST LOOK
Code Refresher Test #1 Topics:
A few of the more significant changes and additions. James Brucker
In-Class Exercise: Lambda Expressions
Generics, Lambdas, Reflections
Method of Classes Chapter 7, page 155 Lecture /4/6.
Computer Science 312 What’s New in Java 8? 1.
Names of variables, functions, classes
Welcome back! October 11, 2018.
„Lambda expressions, Optional”
Corresponds with Chapter 5
Java’s Stream API.
Presentation transcript:

Computer Science 312 Lambdas in Java 1

Syntax of Java Lambdas (<parameters>) -> <expression> (<parameters>) -> {<statements> ;} (Apple a) -> a.getWeight() > 150 (Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight()) (int x) -> x + 1

Context of Use Can be bound to a variable: Can be passed as an argument to a method or returned as a method’s value: Must conform to a functional interface in either case Predicate<Apple> isGreen = (Apple a) -> "green".equals(a.getColor()); List<Apple> greenApples = filter(inventory, (Apple a) -> "green".equals(a.getColor()));

Functional Interfaces A functional interface specifies exactly one method The signature of the method is similar to the signature of the lambda expression, which omits the name (called a function descriptor) public interface Predicate<T>{ boolean test(T t); } Predicate<Apple> isGreen = (Apple a) -> "green".equals(a.getColor()); List<Apple> greenApples = filter(inventory, isGreen);

Functional Interface for filter public interface Predicate<T>{ boolean test(T t); } public static <T> List<T> filter(List<T> list, Predicate<T> p) { List<T> results = new ArrayList<>(); for(T s: list){ if(p.test(s)){ results.add(s); return results; Predicate<String> nonEmptyStringPredicate = (String s) -> !s.isEmpty(); List<String> nonEmpty = filter(listOfStrings, nonEmptyStringPredicate);

Functional Interface for map public interface Function<T, R>{ R apply(T t); } public static <T, R> List<R> map(List<T> list, Function<T, R> f) { List<R> results = new ArrayList<>(); for(T s: list){ results.add(f.apply(s)); return results; List<Integer> lyst = map(Arrays.asList("I", "love", "functions!"), (String s) -> s.length());

Functional Interface for forEach public interface Consumer<T>{ void accept(T t); } public static <T> void forEach(List<T> list, Consumer<T> c) { for(T s: list){ c.accept(s); return results; forEach(Arrays.asList(1,2,3,4,5), (Integer i) -> System.out.println(i));

Specializations for Primitive Types @FunctionalInterface public interface IntPredicate{ boolean test(int i); } IntPredicate evenNumbers = (int i) -> i % 2 == 0; evenNumbers.test(1000); // Returns true

Bifunction Interface Where would this interface be used? @FunctionalInterface public interface BiFunction<T, U, V>{ V apply(T o1, U o2); } Where would this interface be used?

Introduction to Streams Chapter 4 For next time Introduction to Streams Chapter 4