Lambdas Streams Bloch
Lambdas Streams Bloch
17-214 1
Administrivia
17-214 2
Today’s topics
17-214 4
I. What is a lambda?
17-214 5
When did Java get lambdas?
17-214 6
Function objects in Java 1.0 (1996)
Arrays.sort(words, StringLengthComparator.INSTANCE);
17-214 7
Function objects in Java 1.1 (1997) – anonymous classes
17-214 8
Function objects in Java 5 (2004)
17-214 9
Function objects in Java 8 (2014)
17-214 10
Lambda syntax
Syntax Example
parameter -> expression x -> x * x
parameter -> block n -> { int result = 1;
for (int i = 1; i <= n; i++) result *= i;
return result; }
(parameters) -> expression (x, y) -> Math.sqrt(x*x + y*y)
(parameters) -> block (n, r) -> { int result = 1;
for (int k = r; k <= n; k++) result *= k;
return result; }
(parameter decls) -> expression (double x, double y) -> Math.sqrt(x*x + y*y)
(parameters decls) -> block (int n, int r) -> { int result = 1;
for (int k = r; k < n; k++) result *= k;
return result; }
17-214 11
Java has no function types, only functional interfaces
17-214 12
Java has 43 standard functional interfaces
Luckily, there is a fair amount of structure
BiConsumer<T,U> IntUnaryOperator
BiFunction<T,U,R> LongBinaryOperator
BinaryOperator<T> LongConsumer
BiPredicate<T,U> LongFunction<R>
BooleanSupplier LongPredicate
Consumer<T> LongSupplier
DoubleBinaryOperator LongToDoubleFunction
DoubleConsumer LongToIntFunction
DoubleFunction<R> LongUnaryOperator
DoublePredicate ObjDoubleConsumer<T>
DoubleSupplier ObjIntConsumer<T>
DoubleToIntFunction ObjLongConsumer<T>
DoubleToLongFunction Predicate<T>
DoubleUnaryOperator Supplier<T>
Function<T,R> ToDoubleBiFunction<T,U>
IntBinaryOperator ToDoubleFunction<T>
IntConsumer ToIntBiFunction<T,U>
IntFunction<R> ToIntFunction<T>
IntPredicate ToLongBiFunction<T,U>
IntSupplier ToLongFunction<T>
IntToDoubleFunction UnaryOperator<T>
IntToLongFunction
17-214 13
The 6 basic standard functional interfaces
17-214 14
A subtle difference between lambdas & anonymous classes
class Enclosing {
Supplier<Object> lambda() {
return () -> this;
}
Supplier<Object> anon() {
return new Supplier<Object>() {
public Object get() { return this; }
};
}
17-214 16
Occasionally, lambdas are more succinct
is preferable to
service.execute(GoshThisClassNameIsHumongous::action);
17-214 17
Know all five kinds of method references
They all have their uses
17-214 18
The 6 basic functional interfaces redux – method refs
17-214 19
Lambdas vs. method references – the bottom line
17-214 20
II. What is a stream?
17-214 21
Streams are processed lazily
17-214 22
Simple stream examples – mapping, filtering, sorting, etc.
17-214 23
Simple stream examples – mapping, filtering, sorting, etc.
17-214 24
Simple stream examples – mapping, filtering, sorting, etc.
17-214 25
Simple stream examples – mapping, filtering, sorting, etc.
17-214 26
Simple stream examples – file input
17-214 27
Simple stream examples – file input
17-214 28
Simple stream examples – bulk predicates
17-214 29
Simple stream examples – bulk predicates
17-214 30
Stream example – the first twenty Mersenne Primes
17-214 31
Iterative program to print large anagram groups in a dictionary
Review: you saw this Collections Framework case study
17-214 32
Helper function to alphabetize a word
Word nerds call the result an alphagram
17-214 33
Streams gone crazy
Just because you can doesn’t mean you should!
17-214 34
A happy medium
Tasteful use of streams enhances clarity and conciseness
17-214 35
A minipuzzler - what does this print?
"Hello world!".chars()
.forEach(System.out::print);
17-214 36
Puzzler solution
"Hello world!".chars()
.forEach(System.out::print);
Prints 721011081081113211911111410810033
17-214 37
Puzzler solution
"Hello world!".chars()
.forEach(System.out::print);
Prints 721011081081113211911111410810033
17-214 38
How do you fix it?
"Hello world!".chars()
.forEach(x -> System.out.print((char) x));
Moral
Streams only for object ref types, int, long, and double
“Minor primitive types” (byte, short, char, float, boolean) absent
String’s chars method is horribly named!
Avoid using streams for char processing
17-214 39
Streams – the bottom line
17-214 40
Use caution making streams parallel
Remember our Mersenne primes program?
17-214 41
How fast do you think this program runs?
17-214 42
How fast do you think this program runs?
17-214 43
Why did the program run so slowly?
17-214 44
What does parallelize well?
17-214 45
Example – number of primes ≤ n, π(n)
17-214 46
Example – number of primes ≤ n, π(n)
17-214 47
The takeaway – .parallel() is merely an optimization
17-214 48
Summary
17-214 49