Recitation 11 Lambdas added to Java 8.

Slides:



Advertisements
Similar presentations
1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.
Advertisements

Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same.
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
Access to Names Namespaces, Scopes, Access privileges.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
1 Chapter 7 Recursion. 2 What Is Recursion? l Recursive call A method call in which the method being called is the same as the one making the call l Direct.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
1 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert Moyer, Montgomery County Community.
1 Chapter 13 Recursion. 2 Chapter 13 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing Recursive.
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
Textbook: Data Structures and the Java Collections Framework 3rd Edition by William Collins William Collins.
Chapter 3 Object Interaction.  To construct interesting applications it is not enough to build individual objects  Objects must be combined so they.
ICT Introduction to Programming Chapter 4 – Control Structures I.
1 Recursion. 2 Chapter 15 Topics  Meaning of Recursion  Base Case and General Case in Recursive Function Definitions  Writing Recursive Functions with.
Sets and Maps Sets Maps The Comparator Interface Sets and Maps in Java Collections API – TreeSet – TreeMap Review for Exam Reading:
1 Introduction  Algorithms  Data structures  Abstract data types  Programming with lists and sets © 2008 David A Watt, University of Glasgow Algorithms.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
1 Java linked list. Java linked list - definition ▪ Often in programming we are required to systematically store some type of information. A prime example.
Chapter 7 Programming with Recursion. What Is Recursion? Recursive call A method call in which the method being called is the same as the one.
Iteration Abstraction SWE Software Construction Fall 2009.
Tutorial 9 Iteration. Reminder Assignment 8 is due Wednesday.
1 Examples of class: Recursive data structures Instructor: Mainak Chaudhuri
A Introduction to Computing II Lecture 1: Java Review Fall Session 2000.
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
Click to edit Master text styles Stacks Data Structure.
The Methods and What You Need to Know for the AP Exam
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
Repetition Statements
Programming with Recursion
Information and Computer Sciences University of Hawaii, Manoa
CS314 – Section 5 Recitation 10
CSC 222: Object-Oriented Programming Fall 2017
Ordered Structures Wellesley College CS230 Lecture 11
Stacks (and Queues).
Object-Oriented Concepts
Building Java Programs
Stacks and Queues.
Building Java Programs
Java Generics Lecture 14 CS2110 – Fall 2016
Chapter 3 Branching Statements
Mid Term Review Advanced Programming Ananda Gunawardena
Lambdas ---anonymous functions---
Stacks and Queues.
Programming with Recursion
Functional Programming with Java
Iteration Abstraction
Hash table another data structure for implementing a map or a set
Building Java Programs
CSE 1030: Implementing Non-Static Features
Building Java Programs
Building Java Programs
Lecture 7: Linked List Basics reading: 16.2
Building Java Programs
Iteration Abstraction
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
Functional interface.
Recap Week 2 and 3.
Lecture 13: Two-Dimensional Arrays
Effective Java, 3rd Edition Chapter 7: Lambdas and Streams
Namespaces, Scopes, Access privileges
A few of the more significant changes and additions. James Brucker
topics mutable data structures
Computer Science 312 Lambdas in Java 1.
CSC 142 Arrays [Reading: chapter 12].
Chapter 18 Recursion.
Object-Oriented Design Part 2
Just Enough Java 17-May-19.
Lecture 7: Linked List Basics reading: 16.2
Class code for pythonroom.com cchsp2cs
First Semester Review.
Presentation transcript:

Recitation 11 Lambdas added to Java 8

Customizing Comparison new TreeSet<E>() - uses compareTo built into the elements But what it you want to use a different order? - reverse order - case insensitive TreeSet’s constructor can take a Comparator: - new TreeSet<K>(Collections.reverseOrder())

Can access variables that are assigned to exactly once Anonymous Inner Class Goal: sort non-negative integers modulo n int n = …; // > 0 … = new TreeSet<Integer>( new Comparator<Integer>() { public int compare(Integer x, Integer y) { return x % n – y % n; } }); Can access variables that are assigned to exactly once This is clunky! Old Java

Lambdas Only one abstract method to implement, so we can use a lambda! int n = …; // > 0 … = new TreeSet<Integer>( ( x, y) -> x % n – y % n ); Can still access variables that are assigned to exactly once Integer Integer Parameter types are optional, but often they should be included for readability. Parentheses are optional when there’s only one parameter. parameters arrow expression Java takes care of turning this lambda into a Comparator

Try it Out /** Print out the lower-cased versions of the * non-empty strings in strs in order of length. */ public void practice(List<String> strs) { // no loops! strs.removeIf( ); strs.replaceAll( ); strs.sort( ); strs.forEach( ); } Do the first one together. Have them figure out the rest. Show them the javadoc for these methods and the associated interfaces. Show java.util.function.

Answer /** Print out the lower-cased versions of the * non-empty strings in strs in order of length. */ public void practice(List<String> strs) { // no loops! strs.removeIf(s -> s.isEmpty()); strs.replaceAll(s -> s.toLowerCase()); strs.sort((s1, s2) -> s1.length() – s2.length()); strs.forEach(s -> System.out.println(s)); }

More Complex Lambdas /** Maps [a, b, c] to [a, a, b, b, c, c] */ public <T> List<T> doubleList(List<T> list) { List<T> d = new ArrayList<T>(); list.forEach(t -> { d.add(t); }); return d; } block braces

The lambda’s block can return values! More Complex Lambdas List<List<Integer>> lists = …; // no nulls // sort so that [1, 3] is before [2, 4, 5] lists.sort((List<Integer> left, List<Integer> right) -> { if (left.size() > right.size()) return left.size() – right.size(); for (int i = 0; i; < left.size(); i++) if (left.get(i) > right.get(i)) return left.get(i) – right.get(i); }); The lambda’s block can return values!

Try it Out /** Remove any non-increasing elements. */ public void filter(List<List<Integer>> lists) { } Have them figure this out.

Answer /** Remove any non-increasing elements. */ public void filter(List<List<Integer>> lists) { lists.removeIf((List<Integer> list) -> { int prev = Integer.MIN_VALUE; for (int i : list) // Ignoring nulls  if (prev > i) return true; else prev = i; return false; }); }

Difference: Fields Anonymous classes can have fields /** Maps [3, 4, 5] to [3, 7, 12] */ public void sumPrev(List<Integer> ints) { ints.replaceAll(new UnaryOperator<Integer>() { int sum = 0; public Integer apply(Integer i) { sum += i; return sum; } });

Watch out!!! Difference: this class Foo { void bar() { baz(new Anon() { Object func() { return this; } }); class Foo { void bar() { baz(() -> this); } Watch out!!!