SlideShare a Scribd company logo
Lambda Masterclass, JavaDay.ro, December 2018
Lambda Programming Laboratory
A Masterclass for Java Developers
JavaDay.ro 2019
Maurice Naftalin
@mauricenaftalin
Lambda Masterclass, JavaDay.ro, December 2018
• Intro + Setup
• Lambdas and Method References
• Comparators
• Default Methods
• Streams I
• Streams II
Agenda
Lambda Masterclass, JavaDay.ro, December 2018
Maurice Naftalin
Lambda Masterclass, JavaDay.ro, December 2018
Maurice Naftalin
Java 5
Lambda Masterclass, JavaDay.ro, December 2018
Maurice Naftalin
Java 5 Java 8
Lambda Masterclass, JavaDay.ro, December 2018
Maurice Naftalin
Java 5 Java 8
Lambda Masterclass, JavaDay.ro, December 2018
Maurice Naftalin
Java 5 Java 8
2013
Lambda Masterclass, JavaDay.ro, December 2018
Maurice Naftalin
Java 5 Java 8
2014
Lambda Masterclass, JavaDay.ro, December 2018
Maurice Naftalin
Java 5 Java 8
2015
Lambda Masterclass, JavaDay.ro, December 2018
Open https://github.com/mauricen/LambdaHOLv2/
Select Branch: Java 8 or Java 9
Click "Clone or download"; choose "Download ZIP" 

– save to local disk, expand
Setup 1
Lambda Masterclass, JavaDay.ro, December 2018
Setup 2
IntelliJ IDEA
Select New | Project from Existing Sources; select the
new directory LambdaHOLv2-Java8/9, click Open,
then Import project from external model, select
Maven; click Next, Next, and Finish
Check that the module SDK (set from File | Project
Structure… | Modules, Dependencies tab) and the
language level (Sources tab) are set correctly.
Labs are in the package exercises.otherides,
under the directory LambdaLab/test. Detailed lab
instructions are in the README.
Eclipse
Select Preferences | Java | Editor | Folding,
ensure Comments checkbox is ticked.
Select File | Import… | Maven | Existing Maven
Projects, click Next, select new directory
LambdaHOL-v2-Java8/9, click Finish
Labs are in the package exercises.eclipse,
under the directory LambdaLab/test. Detailed
lab instructions are in the README.
Lambda Masterclass, JavaDay.ro, December 2018
• Intro + Setup
• Lambdas and Method References
• Comparators
• Default Methods
• Streams I
• Streams II
Agenda
Lambda Masterclass, JavaDay.ro, December 2018
Lambdas–taking values to a higher order
Lambda Masterclass, JavaDay.ro, December 2018
Lambdas–taking values to a higher order
public interface Collection<E> {
...
boolean removeAll(Collection<?> c);
...
}
instead of supplying values to
specific library methods
Lambda Masterclass, JavaDay.ro, December 2018
Lambdas–taking values to a higher order
public interface Collection<E> {
...
boolean removeAll(Collection<?> c);
...
}
public interface Collection<E> {
...
boolean removeIf(Predicate<? super E> p);
...
}
instead of supplying values to
specific library methods
we want to supply behaviour to
general library methods:
Lambda Masterclass, JavaDay.ro, December 2018
Lambdas–taking values to a higher order
Predicate is an interface with a single abstract boolean-valued method test.
The method removeIf executes test for each element, and –
public interface Collection<E> {
...
boolean removeAll(Collection<?> c);
...
}
public interface Collection<E> {
...
boolean removeIf(Predicate<? super E> p);
...
}
instead of supplying values to
specific library methods
we want to supply behaviour to
general library methods:
Lambda Masterclass, JavaDay.ro, December 2018
Lambdas–taking values to a higher order
Predicate is an interface with a single abstract boolean-valued method test.
The method removeIf executes test for each element, and –
• if test returns true, removeIf removes that element
public interface Collection<E> {
...
boolean removeAll(Collection<?> c);
...
}
public interface Collection<E> {
...
boolean removeIf(Predicate<? super E> p);
...
}
instead of supplying values to
specific library methods
we want to supply behaviour to
general library methods:
Lambda Masterclass, JavaDay.ro, December 2018
How can an instance of an interface represent behaviour?
A behaviour is implemented by a single method!
Predicate is an Interface
Lambda Masterclass, JavaDay.ro, December 2018
How can an instance of an interface represent behaviour?
A behaviour is implemented by a single method!
Convention: if an interface has a single abstract method, like Predicate:
then when the behaviour of an instance of the interface is wanted, the
behaviour of that single method is used.
public interface java.util.functions.Predicate<T> {
boolean test(T t);
}
Predicate is an Interface
Lambda Masterclass, JavaDay.ro, December 2018
How can an instance of an interface represent behaviour?
A behaviour is implemented by a single method!
Convention: if an interface has a single abstract method, like Predicate:
then when the behaviour of an instance of the interface is wanted, the
behaviour of that single method is used.
public interface java.util.functions.Predicate<T> {
boolean test(T t);
}
Functional Interface
Predicate is an Interface
Lambda Masterclass, JavaDay.ro, December 2018
How to make an Interface Instance?
Lambda Masterclass, JavaDay.ro, December 2018
How to make an Interface Instance?
The old way was using an anonymous inner class:





// Predicate returns true for odd values of i
new Predicate<Integer>(){
public boolean test(Integer i) {
return i & 1 == 1;
});
Lambda Masterclass, JavaDay.ro, December 2018
How to make an Interface Instance?
The old way was using an anonymous inner class:





and we can still supply that as a method argument:
// Predicate returns true for odd values of i
new Predicate<Integer>(){
public boolean test(Integer i) {
return i & 1 == 1;
});
// remove odd values from integerList
integerList.removeIf(new Predicate<Integer>(){
public boolean test(Integer i) {
return i & 1 == 1;
}));
Lambda Masterclass, JavaDay.ro, December 2018
Stripping Out the Boilerplate
Lambda Masterclass, JavaDay.ro, December 2018
// remove odd numbers from integerList
integerList.removeIf(new Predicate<Integer>(){
public boolean test(Integer i) {
return (i & 1) == 1;
}
});
Stripping Out the Boilerplate
Lambda Masterclass, JavaDay.ro, December 2018
// remove odd numbers from integerList
integerList.removeIf(new Predicate<Integer>(){
public boolean test(Integer i) {
return (i & 1) == 1;
}
});
Stripping Out the Boilerplate
Why do we have to say we’re supplying a Predicate?
Lambda Masterclass, JavaDay.ro, December 2018
Why do we have to say we’re supplying a Predicate?
// remove odd numbers from integerList
integerList.removeIf(new Predicate<Integer>(){
public boolean test(Integer i) {
return (i & 1) == 1;
}
});
Stripping Out the Boilerplate
Lambda Masterclass, JavaDay.ro, December 2018
Why do we have to say we’re supplying a Predicate?
// remove odd numbers from integerList
integerList.removeIf(new Predicate<Integer>(){
public boolean test(Integer i) {
return (i & 1) == 1;
}
});
Stripping Out the Boilerplate
Why do we have to say we’re implementing test, the only abstract method?
Lambda Masterclass, JavaDay.ro, December 2018
// remove odd numbers from integerList
integerList.removeIf(new Predicate<Integer>(){
public boolean test(Integer i) {
return (i & 1) == 1;
}
});
Stripping Out the Boilerplate
Why do we have to say we’re implementing test, the only abstract method?
Why do we have to say the type parameter is Integer?
Why do we have to say we’re supplying a Predicate?
Lambda Masterclass, JavaDay.ro, December 2018
// remove odd numbers from integerList
integerList.removeIf(new Predicate<Integer>(){
public boolean test(Integer i) {
return (i & 1) == 1;
}
});
Stripping Out the Boilerplate
Why do we have to say we’re implementing test, the only abstract method?
Why do we have to say the type parameter is Integer?
Why do we have to say we’re supplying a Predicate?
Lambda Masterclass, JavaDay.ro, December 2018
// remove odd numbers from integerList
integerList.removeIf(i (i & 1) == 1)
Stripping Out the Boilerplate
Why do we have to say we’re implementing test, the only abstract method?
Why do we have to say the type parameter is Integer?
Why do we have to say we’re supplying a Predicate?
Lambda Masterclass, JavaDay.ro, December 2018
// remove odd numbers from integerList
integerList.removeIf(i (i & 1) == 1)
Stripping Out the Boilerplate
Why do we have to say we’re implementing test, the only abstract method?
Why do we have to say the type parameter is Integer?
Why do we have to say we’re supplying a Predicate?
All we need is one extra syntax element!
Lambda Masterclass, JavaDay.ro, December 2018
// remove odd numbers from integerList
integerList.removeIf(i -> (i & 1) == 1)
Stripping Out the Boilerplate
Why do we have to say we’re implementing test, the only abstract method?
Why do we have to say the type parameter is Integer?
All we need is one extra syntax element!
Why do we have to say we’re supplying a Predicate?
Lambda Masterclass, JavaDay.ro, December 2018
// remove odd numbers from integerList
integerList.removeIf(i -> (i & 1) == 1)
Stripping Out the Boilerplate
Why do we have to say we’re implementing test, the only abstract method?
Why do we have to say the type parameter is Integer?
All we need is one extra syntax element!
Why do we have to say we’re supplying a Predicate?
// remove odd numbers from integerList
integerList.removeIf(i -> (i & 1) == 1)
Lambda Masterclass, JavaDay.ro, December 2018
Rules for Writing Lambdas
Lambda Masterclass, JavaDay.ro, December 2018
Rules for Writing Lambdas
() -> 42
Lambda Masterclass, JavaDay.ro, December 2018
Rules for Writing Lambdas
() -> 42
a -> a + 1 // single untyped parameter – can omit parentheses
Lambda Masterclass, JavaDay.ro, December 2018
Rules for Writing Lambdas
() -> 42
a -> a + 1 // single untyped parameter – can omit parentheses
(a,b) -> a + b // but they are needed for multiple parameters
Lambda Masterclass, JavaDay.ro, December 2018
Rules for Writing Lambdas
() -> 42
a -> a + 1 // single untyped parameter – can omit parentheses
(a,b) -> a + b // but they are needed for multiple parameters
(int a, int b) -> a + b;

// type either all parameters, or none
Lambda Masterclass, JavaDay.ro, December 2018
Rules for Writing Lambdas
() -> 42
a -> a + 1 // single untyped parameter – can omit parentheses
(a,b) -> a + b // but they are needed for multiple parameters
(int a, int b) -> a + b;

// type either all parameters, or none
() -> { println(42); return 42; } 

// lambda body can be a statement
Lambda Masterclass, JavaDay.ro, December 2018
Target Typing
Context is needed to define a lambda’s type:
Lambda Masterclass, JavaDay.ro, December 2018
Target Typing
Context is needed to define a lambda’s type:
Runnable r = () -> {} // compiles
Lambda Masterclass, JavaDay.ro, December 2018
Target Typing
Context is needed to define a lambda’s type:
Runnable r = () -> {} // compiles
Thread t = new Thread(() -> {}) // compiles
Lambda Masterclass, JavaDay.ro, December 2018
Target Typing
Context is needed to define a lambda’s type:
Runnable r = () -> {} // compiles
Thread t = new Thread(() -> {}) // compiles
Object o = () -> {} // illegal
Lambda Masterclass, JavaDay.ro, December 2018
Method References
Sometimes it’s more convenient to write lambdas as
method references. There are four kinds:
example lambda equivalent
constructor ArrayList::new () -> new ArrayList()
static Integer::max (x,y) -> Integer.max(x,y)
bound “abc"::concat str -> "abc".concat(str)
unbound String::length str -> str.length()
Lambda Masterclass, JavaDay.ro, December 2018
Lambdas and Method References
Demo and labs
Lambda Masterclass, JavaDay.ro, December 2018
• Intro + Setup
• Lambdas and Method References
• Comparators
• Default Methods
• Streams I
• Streams II
Agenda
Lambda Masterclass, JavaDay.ro, December 2018
Comparator
Comparators were enhanced with static and default
methods in Java 8. Now, instead of writing this:
Comparator<Person> cmpr = new Comparator<>() {
@Override
public int compare(Person p1, Person p2) {
int cmp = p1.getLastName().compareTo(p2.getLastName());
if (cmp == 0) {
return p1.getFirstName().compareTo(p2.getFirstName());
} else {
return cmp;
}
}
};
Lambda Masterclass, JavaDay.ro, December 2018
Comparator
We can instead write
using default and factory methods on Comparator
Comparator<Person> cmp =
Comparator.comparing(Person::getLastName)
.thenComparing(Person::getFirstName)
.thenComparing(Person::getAge);
Lambda Masterclass, JavaDay.ro, December 2018
Comparator Methods returning Comparator<T>
name argument(s)
static comparing Function<T,U>
comparing Function<T,U>, Comparator<U>
comparingXxx ToXxxFunction<T>
naturalOrder
nullsFirst/Last Comparator<T>
reverseOrder Comparator<T>
default reversed
thenComparing Comparator<T>
thenComparingXxx ToXxxFunction<T>
Lambda Masterclass, JavaDay.ro, December 2018
Example Domain
firstName: String
lastName: String
age: int
Person
Lambda Masterclass, JavaDay.ro, December 2018
Comparators
Demo and labs
Lambda Masterclass, JavaDay.ro, December 2018
• Intro + Setup
• Lambdas and Method References
• Comparators
• Default Methods
• Streams 1
• Streams II
Agenda
Lambda Masterclass, JavaDay.ro, December 2018
Iterable<T>
forEach(Consumer<T> c)
supplies each element in turn to c
removeIf(Predicate<T> p)
removes elements satisfying p
compute(K key, BiFunction<K,V,V> remappingFunction)
computes a new mapping from key and its current mapped value (or null)
computeIfAbsent(K key, Function<K,V> mfn)
if there is no current mapping for key, creates one using mfn and enters it
computeIfPresent(K key, BiFunction<K,V,V> remappingFunction)
creates a mapping from key (if present and non-null) and its current mapped value
forEach(BiConsumer<K,V> c)
executes c for each key-value pair in this Map
merge(K key, V v, BiFunction<V,V,V> remappingFunction)
if key present, calculates new value from old value and v, o.w. associates key with v
putIfAbsent(K key, V value)
associates key with value if key is not present or is null
remove(Object k, Object val)
removes mapping for k from this map if it is present and has the value val
replace(K key, V oldValue, V newValue)
replaces the entry for key only if currently mapped to oldvalue
replaceAll(BiFunction<K,V,V> fn)
replaces each entry’s value with the result of applying fn to the entry’s key and value
replaceAll(UnaryOperator<E> op)
replaces each element with the result of applying op to it
Map<K,V>
List<E>
Lambda Masterclass, JavaDay.ro, December 2018
Default Methods on Map
returns name argument(s)
void forEach BiConsumer<K,V>
void replaceAll BiFunction<K,V,V>
V computeIfAbsent K, Function<K,V>
V computeIfPresent K, BiFunction<K,V,V>
V compute K, BiFunction<K,V,V>
V merge K, V, BiFunction<K,V,V>
boolean remove Object, Object
V replace K, V
boolean replace K, V, V
V putIfAbsent K, V
Lambda Masterclass, JavaDay.ro, December 2018
Default Methods
Demo and labs
Lambda Masterclass, JavaDay.ro, December 2018
• Intro + Setup
• Lambdas and Method References
• Comparators
• Default Methods
• Streams I
• Streams II
Agenda
x0 x1 x2 x3
Visualizing Stream Operations
x0x1 x2 x3
Visualizing Stream Operations
y0x1 x2 x3
Visualizing Stream Operations
x1 x2 x3
Visualizing Stream Operations
x1x2 x3
Visualizing Stream Operations
y1x2 x3
Visualizing Stream Operations
x2 x3
Visualizing Stream Operations
x2
Visualizing Stream Operations
x0
x1
x3
33
x0
x1
x2
x3
x2
Visualizing Stream Operations
x0
x1
x3
33
x0
x1
x2
x3
x2
Visualizing Stream Operations
y0
y1
y2
y3
33
x0
x1
x3
x2
Visualizing Stream Operations
33
x0
x1
x3
name returns interface used FI signature
filter Stream<T> Predicate<T> T ➞ boolean
map Stream<U> Function<T,U> T ➞ U
flatMap Stream<R> Function<T,Stream<R>> T ➞ Stream<R>
peek Stream<T> Consumer<T> T ➞ void
Stateless Intermediate Operations
34
name returns interface used FI signature
filter Stream<T> Predicate<T> T ➞ boolean
map Stream<U> Function<T,U> T ➞ U
flatMap Stream<R> Function<T,Stream<R>> T ➞ Stream<R>
peek Stream<T> Consumer<T> T ➞ void
Stateless Intermediate Operations
34
mapToInt IntStream ToIntFunction<T> T ➞ int
mapToLong LongStream ToLongFunction<T> T ➞ long
mapToDouble DoubleStream ToDoubleFunction<T> T ➞ double
filter()
filter(s -> s.length() < 4)
Stream<String>
Predicate<String
Stream<String>
filter()
filter()
filter(s -> s.length() < 4)
Stream<String>
Predicate<String
Stream<String>
filter()
“jim”
✔
filter()
filter(s -> s.length() < 4)
Stream<String>
Predicate<String
Stream<String>
filter()
✔
filter()
✖
filter(s -> s.length() < 4)
Stream<String>
Predicate<String
Stream<String>
filter()
✔
filter()
✖
filter(s -> s.length() < 4)
Stream<String>
Predicate<String
Stream<String>
filter()
✔
“amy”
filter()
✖
filter(s -> s.length() < 4)
Stream<String>
Predicate<String
Stream<String>
filter()
✔
map()
map(Person::getCity)
Stream<Person> Stream<City>
Function<Person,City
map()
map(Person::getCity)
Stream<Person> Stream<City>
Function<Person,City
bill
map()
map(Person::getCity)
Stream<Person> Stream<City>
Function<Person,City
map()
map(Person::getCity)
Stream<Person> Stream<City>
Function<Person,City
amy
map()
map(Person::getCity)
Stream<Person> Stream<City>
Function<Person,City
peek()
peek(System.out::println)
Consumer<T>
System.out::println
T
Stream<T>
flatMapToInt()
Stream<String>
Function<String,IntStream>
IntStream
flatMapToInt(String::chars)
flatMapToInt()
Stream<String>
Function<String,IntStream>
IntStream
'a' 'm' 'y'
flatMapToInt(String::chars)
flatMapToInt()
Stream<String>
Function<String,IntStream>
IntStream
'm' 'y'
flatMapToInt(String::chars)
flatMapToInt()
Stream<String>
Function<String,IntStream>
IntStream
'y'
flatMapToInt(String::chars)
flatMapToInt()
Stream<String>
Function<String,IntStream>
IntStream
flatMapToInt(String::chars)
name returns type used FI signature
limit Stream<T> long
skip Stream<T> long
sorted Stream<T> Comparator<T> (T, T) ➞ int
distinct Stream<T>
takeWhile* Stream<T> Predicate<T>
dropWhile* Stream<T> Predicate<T>
Stateful Intermediate Operations
* added in Java 9
Lambda Masterclass, JavaDay.ro, December 2018
Collectors: Journey’s End for a Stream
The Life of a Stream Element
• Born (at a spliterator)
• Transformed (by intermediate operations)
• Collected (by a terminal operation)
Lambda Masterclass, JavaDay.ro, December 2018
Collectors: Journey’s End for a Stream
The Life of a Stream Element
• Born (at a spliterator)
• Transformed (by intermediate operations)
• Collected (by a terminal operation)
Lambda Masterclass, JavaDay.ro, December 2018
Collectors: Journey’s End for a Stream
The Life of a Stream Element
• Born (at a spliterator)
• Transformed (by intermediate operations)
• Collected (by a terminal operation)
Lambda Masterclass, JavaDay.ro, December 2018
Collectors: Journey’s End for a Stream
?
The Life of a Stream Element
• Born (at a spliterator)
• Transformed (by intermediate operations)
• Collected (by a terminal operation)
Lambda Masterclass, JavaDay.ro, December 2018
Terminal Operations
– Search operations
– Side-effecting operations
– Reductions

Lambda Masterclass, JavaDay.ro, December 2018
Search Operations
Search operations -
• allMatch, anyMatch
• findAny, findFirst
boolean allAdults =
people.stream()

.allMatch(p -> p.getAge() >= 21);

Lambda Masterclass, JavaDay.ro, December 2018
Side-effecting Operations
Side-effecting operations
• forEach, forEachOrdered



people.stream()

.forEach(System.out::println);
• So could we calculate total ages like this?



int sum = 0;

people.stream()

.mapToInt(Person::getAge)

.forEach(a -> { sum += a });
people.stream()

.forEach(System.out::println);

int sum = 0;

people.stream()

.mapToInt(Person::getAge)

.forEach(a -> { sum += a; });

Lambda Masterclass, JavaDay.ro, December 2018
Side-effecting Operations
Side-effecting operations
• forEach, forEachOrdered



people.stream()

.forEach(System.out::println);
• So could we calculate total ages like this?



int sum = 0;

people.stream()

.mapToInt(Person::getAge)

.forEach(a -> { sum += a });
people.stream()

.forEach(System.out::println);

int sum = 0;

people.stream()

.mapToInt(Person::getAge)

.forEach(a -> { sum += a; });

Don’t do this!
Lambda Masterclass, JavaDay.ro, December 2018
• Using an accumulator:
Reduction – Why?
int[] vals = new int[100];
Arrays.setAll(vals, i -> i);
int sum = 0;
for (int i = 0 ; i < vals.length ; i++) {
sum += vals[i];
}

Lambda Masterclass, JavaDay.ro, December 2018
• Using an accumulator:
Reduction – Why?
int[] vals = new int[100];
Arrays.setAll(vals, i -> i);
int sum = 0;
for (int i = 0 ; i < vals.length ; i++) {
sum += vals[i];
}

0 1 2 3
+
+
+
Lambda Masterclass, JavaDay.ro, December 2018
• Avoiding an accumulator:
Reduction – Why?
Lambda Masterclass, JavaDay.ro, December 2018
• Avoiding an accumulator:
Reduction – Why?
Lambda Masterclass, JavaDay.ro, December 2018
• Avoiding an accumulator:
Reduction – Why?
int[] vals = new int[100];
Arrays.setAll(vals, i -> i);
OptionalInt sum = Arrays.stream(vals)
.reduce((a,b) -> a + b);

+ +
+
1 2 30
Lambda Masterclass, JavaDay.ro, December 2018
• Avoiding an accumulator:
Reduction – Why?
int[] vals = new int[100];
Arrays.setAll(vals, i -> i);
OptionalInt sum = Arrays.stream(vals)
.reduce((a,b) -> a + b);

+ +
+
1 2 30
+
+
+
Lambda Masterclass, JavaDay.ro, December 2018
• Avoiding an accumulator:
Reduction – Why?
int[] vals = new int[100];
Arrays.setAll(vals, i -> i);
OptionalInt sum = Arrays.stream(vals)
.reduce((a,b) -> a + b);

BinaryOperator must be associative!
+ +
+
1 2 30
+
+
+
Lambda Masterclass, JavaDay.ro, December 2018
• Avoiding an accumulator:
Reduction – Why?
int[] vals = new int[100];
Arrays.setAll(vals, i -> i);
OptionalInt sum = Arrays.stream(vals)
.reduce((a,b) -> a + b);

BinaryOperator must be associative!
a + (b + c) = (a + b) + c
+ +
+
1 2 30
+
+
+
Lambda Masterclass, JavaDay.ro, December 2018
0 1 2 3 4 5 6 7
+ +
+
+
+
+
+
Reduction – Why?
Lambda Masterclass, JavaDay.ro, December 2018
0 1 2 3 4 5 6 7
+ +
+
+
+
+
+
Reduction – Why?
Lambda Masterclass, JavaDay.ro, December 2018
0 1 2 3 4 5 6 7
+ +
+
+
+
+
+
Intermediate operations
Reduction – Why?
Lambda Masterclass, JavaDay.ro, December 2018
Reduction on ImmutableValues
Reduction works on immutable values too
BigDecimal[] vals = new BigDecimal[100];
Arrays.setAll(vals, i -> new BigDecimal(i));
Optional<BigDecimal> sum = Arrays.stream(vals)
.reduce(BigDecimal::add);

Lambda Masterclass, JavaDay.ro, December 2018
Streams 1
Demo and labs
Lambda Masterclass, JavaDay.ro, December 2018
• Intro + Setup
• Lambdas and Method References
• Comparators
• Default Methods
• Streams I
• Streams II
Agenda
Lambda Masterclass, JavaDay.ro, December 2018
What about Collections?
Lambda Masterclass, JavaDay.ro, December 2018
What about Collections?
This also works with immutable values.
Lambda Masterclass, JavaDay.ro, December 2018
What about Collections?
This also works with immutable values.
And even collections – sort of … but then
• for each element, client code has to create a new empty
collection and add the single element to it,
• and combine collections using addAll
Lambda Masterclass, JavaDay.ro, December 2018
What about Collections?
This also works with immutable values.
And even collections – sort of … but then
• for each element, client code has to create a new empty
collection and add the single element to it,
• and combine collections using addAll
... it’s hopelessly inefficient!
Lambda Masterclass, JavaDay.ro, December 2018
What about Collections?
Lambda Masterclass, JavaDay.ro, December 2018
What about Collections?
This also works with immutable values.
Lambda Masterclass, JavaDay.ro, December 2018
What about Collections?
This also works with immutable values.
But collections need to
Lambda Masterclass, JavaDay.ro, December 2018
What about Collections?
This also works with immutable values.
But collections need to
• be initialised
Lambda Masterclass, JavaDay.ro, December 2018
What about Collections?
This also works with immutable values.
But collections need to
• be initialised
• have individual elements added
Lambda Masterclass, JavaDay.ro, December 2018
What about Collections?
This also works with immutable values.
But collections need to
• be initialised
• have individual elements added
• be merged
Lambda Masterclass, JavaDay.ro, December 2018
A better idea 

would be
Lambda Masterclass, JavaDay.ro, December 2018
A better idea 

would be
Reduction over an Identity
Lambda Masterclass, JavaDay.ro, December 2018
Reduction over an Identity
Lambda Masterclass, JavaDay.ro, December 2018
Reduction over an Identity
BigDecimal[] vals = new BigDecimal[100];
Arrays.setAll(vals, i -> new BigDecimal(i));
BigDecimal sum = Arrays.stream(vals)
.reduce(BigDecimal.ZERO, BigDecimal::add);

Works for primitives (of course) and also immutable objects:
Lambda Masterclass, JavaDay.ro, December 2018
Reduction over an Identity
+
+
+
0 1 2 3
+
+
+
0
4 5 6 7
0
++
+
Lambda Masterclass, JavaDay.ro, December 2018
So, Reduction to Collections?
Lambda Masterclass, JavaDay.ro, December 2018
So, Reduction to Collections?
Reduction over an identity doesn’t work with
collections at all
Lambda Masterclass, JavaDay.ro, December 2018
So, Reduction to Collections?
Reduction over an identity doesn’t work with
collections at all
• reduction reuses the identity element
Lambda Masterclass, JavaDay.ro, December 2018
So, Reduction to Collections?
Reduction over an identity doesn’t work with
collections at all
• reduction reuses the identity element
We need something better!
Lambda Masterclass, JavaDay.ro, December 2018
The Answer – Collectors
a
a
a
e0 e1 e2 e3
a
a
a
e4 e5 e6 e7
aa
c
Lambda Masterclass, JavaDay.ro, December 2018
The Answer – Collectors
a
a
a
e0 e1 e2 e3
a
a
a
() -> []
e4 e5 e6 e7
aa
c
() -> []
Lambda Masterclass, JavaDay.ro, December 2018
The Answer – Collectors
a
a
a
e0 e1 e2 e3
a
a
a
() -> []
e4 e5 e6 e7
aa
c
a: accumulator
c: combiner
() -> []
Lambda Masterclass, JavaDay.ro, December 2018
Collectors
Lambda Masterclass, JavaDay.ro, December 2018
So to define a collector, you need to provide
• Supplier
• Accumulator
• Combiner
Collectors
Lambda Masterclass, JavaDay.ro, December 2018
So to define a collector, you need to provide
• Supplier
• Accumulator
• Combiner
That sounds really hard…
Collectors
Lambda Masterclass, JavaDay.ro, December 2018
So to define a collector, you need to provide
• Supplier
• Accumulator
• Combiner
That sounds really hard…
Good then that we don’t have to do it!
Collectors
Lambda Masterclass, JavaDay.ro, December 2018
Example Domain
city: City
name: String
age: int
Person
Person amy = new Person(Athens, "Amy", 21);

...
List<Person> people = Arrays.asList(jon, amy, bill);

Lambda Masterclass, JavaDay.ro, December 2018
• Why collectors?
• Using the predefined collectors
• Worked problems

Journey’s End – Agenda
Lambda Masterclass, JavaDay.ro, December 2018
Using the Predefined Collectors
Predefined Standalone Collectors – from factory
methods in Collectors class
• toList(), toSet(), toMap(), joining()
• toMap(), toCollection()
• groupingBy(), partitioningBy(),
groupingByConcurrent()
Lambda Masterclass, JavaDay.ro, December 2018
Using the Predefined Collectors
Predefined Standalone Collectors – from factory
methods in Collectors class
• toList(), toSet(), toMap(), joining()
• toMap(), toCollection()
• groupingBy(), partitioningBy(),
groupingByConcurrent()
framework provides
the Supplier
Lambda Masterclass, JavaDay.ro, December 2018
Using the Predefined Collectors
Predefined Standalone Collectors – from factory
methods in Collectors class
• toList(), toSet(), toMap(), joining()
• toMap(), toCollection()
• groupingBy(), partitioningBy(),
groupingByConcurrent()
user provides
the Supplier
framework provides
the Supplier
Lambda Masterclass, JavaDay.ro, December 2018
Using the Predefined Collectors
Predefined Standalone Collectors – from factory
methods in Collectors class
• toList(), toSet(), toMap(), joining()
• toMap(), toCollection()
• groupingBy(), partitioningBy(),
groupingByConcurrent()
user provides
the Supplier
produce a 

classification map
framework provides
the Supplier
Lambda Masterclass, JavaDay.ro, December 2018
Simple Collector – toSet()
Lambda Masterclass, JavaDay.ro, December 2018
Simple Collector – toSet()
people.stream().collect(Collectors.toSet())
Lambda Masterclass, JavaDay.ro, December 2018
Simple Collector – toSet()
Collectors.toSet()
people.stream().collect(Collectors.toSet())
Lambda Masterclass, JavaDay.ro, December 2018
Simple Collector – toSet()
Stream<Person>
Collectors.toSet()
Set<Person>
people.stream().collect(Collectors.toSet())
Collector<Person,?,Set<Person>>
Lambda Masterclass, JavaDay.ro, December 2018
Simple Collector – toSet()
Stream<Person>
Collectors.toSet()
Set<Person>
people.stream().collect(Collectors.toSet())
bill
Collector<Person,?,Set<Person>>
Lambda Masterclass, JavaDay.ro, December 2018
Simple Collector – toSet()
Stream<Person>
Collectors.toSet()
Set<Person>
people.stream().collect(Collectors.toSet())
bill
Collector<Person,?,Set<Person>>
Lambda Masterclass, JavaDay.ro, December 2018
Simple Collector – toSet()
Stream<Person>
Collectors.toSet()
Set<Person>
people.stream().collect(Collectors.toSet())
bill
Collector<Person,?,Set<Person>>
Lambda Masterclass, JavaDay.ro, December 2018
Simple Collector – toSet()
Stream<Person>
Collectors.toSet()
Set<Person>
people.stream().collect(Collectors.toSet())
bill
jon
Collector<Person,?,Set<Person>>
Lambda Masterclass, JavaDay.ro, December 2018
Simple Collector – toSet()
Stream<Person>
Collectors.toSet()
Set<Person>
people.stream().collect(Collectors.toSet())
bill
jon
Collector<Person,?,Set<Person>>
Lambda Masterclass, JavaDay.ro, December 2018
Simple Collector – toSet()
Stream<Person>
Collectors.toSet()
Set<Person>
people.stream().collect(Collectors.toSet())
amy
bill
jon
Collector<Person,?,Set<Person>>
Lambda Masterclass, JavaDay.ro, December 2018
Simple Collector – toSet()
Stream<Person>
Collectors.toSet()
Set<Person>
people.stream().collect(Collectors.toSet())
amy
bill
jon
Collector<Person,?,Set<Person>>
Lambda Masterclass, JavaDay.ro, December 2018
Simple Collector – toSet()
Collectors.toSet()
Set<Person>
people.stream().collect(Collectors.toSet())
amy
bill
jon
Collector<Person,?,Set<Person>>
Lambda Masterclass, JavaDay.ro, December 2018
Simple Collector – toSet()
Collectors.toSet()
Set<Person>
{ , , }
people.stream().collect(Collectors.toSet())
amybilljon
Collector<Person,?,Set<Person>>
Lambda Masterclass, JavaDay.ro, December 2018
toMap(Function<T,K> keyMapper,
Function<T,U> valueMapper)
people.stream().collect(
Collectors.toMap(
Person::getCity,

Person::getName))
Lambda Masterclass, JavaDay.ro, December 2018
toMap(Function<T,K> keyMapper,
Function<T,U> valueMapper)
Stream<Person>
toMap() Map<City,String>
Collector<Person,?,Map<City,String>
people.stream().collect(toMap(Person::getCity,Person::getName))
Lambda Masterclass, JavaDay.ro, December 2018
toMap(Function<T,K> keyMapper,
Function<T,U> valueMapper)
Stream<Person>
toMap() Map<City,String>
Collector<Person,?,Map<City,String>
Lambda Masterclass, JavaDay.ro, December 2018
toMap(Function<T,K> keyMapper,
Function<T,U> valueMapper)
Stream<Person>
toMap() Map<City,String>
bill
Lambda Masterclass, JavaDay.ro, December 2018
toMap(Function<T,K> keyMapper,
Function<T,U> valueMapper)
Stream<Person>
toMap() Map<City,String>
bill
Person::getCity
Lambda Masterclass, JavaDay.ro, December 2018
toMap(Function<T,K> keyMapper,
Function<T,U> valueMapper)
Stream<Person>
toMap() Map<City,String>
London
bill
Lambda Masterclass, JavaDay.ro, December 2018
toMap(Function<T,K> keyMapper,
Function<T,U> valueMapper)
Stream<Person>
toMap() Map<City,String>
London
bill Person::getName
Lambda Masterclass, JavaDay.ro, December 2018
toMap(Function<T,K> keyMapper,
Function<T,U> valueMapper)
Stream<Person>
toMap() Map<City,String>
London “Bill”
bill Person::getName
Lambda Masterclass, JavaDay.ro, December 2018
toMap(Function<T,K> keyMapper,
Function<T,U> valueMapper)
Stream<Person>
toMap() Map<City,String>
London “Bill”
Lambda Masterclass, JavaDay.ro, December 2018
toMap(Function<T,K> keyMapper,
Function<T,U> valueMapper)
Stream<Person>
toMap() Map<City,String>
amy
London “Bill”
Lambda Masterclass, JavaDay.ro, December 2018
toMap(Function<T,K> keyMapper,
Function<T,U> valueMapper)
Stream<Person>
toMap() Map<City,String>
Athens
amy
London “Bill”
Lambda Masterclass, JavaDay.ro, December 2018
toMap(Function<T,K> keyMapper,
Function<T,U> valueMapper)
Stream<Person>
toMap() Map<City,String>
“Amy”Athens
London “Bill”
Lambda Masterclass, JavaDay.ro, December 2018
toMap(Function<T,K> keyMapper,
Function<T,U> valueMapper)
Stream<Person>
toMap() Map<City,String>
“Amy”Athens
jon
London “Bill”
Lambda Masterclass, JavaDay.ro, December 2018
toMap(Function<T,K> keyMapper,
Function<T,U> valueMapper)
Stream<Person>
toMap() Map<City,String>
“Amy”Athens
London “Bill”
Tulsa “Jon”
Lambda Masterclass, JavaDay.ro, December 2018
toMap(Function<T,K> keyMapper,
Function<T,U> valueMapper)
toMap() Map<City,String>
“Amy”Athens
London “Bill”
Tulsa “Jon”
Lambda Masterclass, JavaDay.ro, December 2018
toMap(Function<T,K> keyMapper,
Function<T,U> valueMapper)
toMap() Map<City,String>
“Amy”Athens
London “Bill”
Tulsa “Jon”
Lambda Masterclass, JavaDay.ro, December 2018
toMap(Function<T,K> keyMapper,
Function<T,U> valueMapper)
Stream<Person>
toMap() Map<City,String>
Lambda Masterclass, JavaDay.ro, December 2018
toMap(Function<T,K> keyMapper,
Function<T,U> valueMapper)
Stream<Person>
toMap() Map<City,String>
“Amy”Athens
jon
London “Bill”
Lambda Masterclass, JavaDay.ro, December 2018
toMap(Function<T,K> keyMapper,
Function<T,U> valueMapper)
Stream<Person>
toMap() Map<City,String>
“Amy”Athens
jon
London “Bill”
Athens
Lambda Masterclass, JavaDay.ro, December 2018
toMap(Function<T,K> keyMapper,
Function<T,U> valueMapper)
Stream<Person>
toMap() Map<City,String>
“Amy”Athens
jon
London
IllegalStateException
“Bill”
Athens
Lambda Masterclass, JavaDay.ro, December 2018
toMap(Function<T,K> keyMapper,
Function<T,U> valueMapper
BinaryOperator<U> mergeFunction)
people.stream().collect(
Collectors.toMap(
Person::getCity,

Person::getName,
(a,b) -> a.concat(b)))
Lambda Masterclass, JavaDay.ro, December 2018
toMap(Function<T,K> keyMapper,
Function<T,U> valueMapper
BinaryOperator<U> mergeFunction)
Stream<Person>
toMap() Map<City,String>
Lambda Masterclass, JavaDay.ro, December 2018
toMap(Function<T,K> keyMapper,
Function<T,U> valueMapper
BinaryOperator<U> mergeFunction)
Stream<Person>
toMap() Map<City,String>
“Amy”Athens
London “Bill”
“Jon”
Athens
Lambda Masterclass, JavaDay.ro, December 2018
toMap(Function<T,K> keyMapper,
Function<T,U> valueMapper
BinaryOperator<U> mergeFunction)
Stream<Person>
toMap() Map<City,String>
“Amy”Athens
London “Bill”
“Jon”
(a,b) -> a.concat(b)
Athens
Lambda Masterclass, JavaDay.ro, December 2018
toMap(Function<T,K> keyMapper,
Function<T,U> valueMapper
BinaryOperator<U> mergeFunction)
Stream<Person>
toMap() Map<City,String>
Athens
London “Bill”
(a,b) -> a.concat(b)
“AmyJon”
Athens
Lambda Masterclass, JavaDay.ro, December 2018
Collectors API
Factory methods in the Collectors class.They
produce standalone collectors, accumulating to:
• framework-supplied containers
• custom collections
• classification maps
Lambda Masterclass, JavaDay.ro, December 2018
toMap(Function<T,K> keyMapper,
Function<T,U> valueMapper
BinaryOperator<U> mergeFunction)
toMap(Function<T,K> keyMapper,
Function<T,U> valueMapper
BinaryOperator<U> mergeFunction,
Supplier<M> mapFactory)
people.stream().collect(
Collectors.toMap(
Person::getCity,

Person::getName,
(a,b) -> a.concat(b),

TreeMap::new))
Lambda Masterclass, JavaDay.ro, December 2018
toMap(Function<T,K> keyMapper,
Function<T,U> valueMapper
BinaryOperator<U> mergeFunction,
Supplier<M> mapFactory)
Stream<Person>
toMap() Map<City,String>
Lambda Masterclass, JavaDay.ro, December 2018
toMap(Function<T,K> keyMapper,
Function<T,U> valueMapper
BinaryOperator<U> mergeFunction,
Supplier<M> mapFactory)
Stream<Person>
toMap() Map<City,String>
Lambda Masterclass, JavaDay.ro, December 2018
Collecting to Custom Collections
Lambda Masterclass, JavaDay.ro, December 2018
Collecting to Custom Collections
toCollection(Supplier<C> collectionFactory)
Lambda Masterclass, JavaDay.ro, December 2018
Collecting to Custom Collections
<C extends Collection<T>>
toCollection(Supplier<C> collectionFactory)
Lambda Masterclass, JavaDay.ro, December 2018
Collecting to Custom Collections
returns
Collector<T,?,C>
<C extends Collection<T>>
toCollection(Supplier<C> collectionFactory)
Lambda Masterclass, JavaDay.ro, December 2018
Collecting to Custom Collections
returns
Collector<T,?,C>
<C extends Collection<T>>
NavigableSet<String> sortedNames = people.stream()
.map(Person::getName)

.collect(toCollection(TreeSet::new));
toCollection(Supplier<C> collectionFactory)
Lambda Masterclass, JavaDay.ro, December 2018
Collectors API
Factory methods in the Collectors class.They
produce standalone collectors, accumulating to:
• framework-supplied containers
• custom collections
• classification maps
Lambda Masterclass, JavaDay.ro, December 2018
Collecting to Classification Maps
groupingBy
• simple
• with downstream
• with downstream and map factory
partitioningBy
Lambda Masterclass, JavaDay.ro, December 2018
groupingBy(Function<T,K> classifier)
Uses the classifier function to make a classification mapping
Like toMap(), except that the values placed in the map are lists of
the elements, one List corresponding to each classification key:
For example, use Person.getCity() to make a Map<City,List<Person>>
Map<City,List<Person>> peopleByCity = people.stream()
.collect(Collectors.groupingBy(Person::getCity));
Lambda Masterclass, JavaDay.ro, December 2018
Stream<Person>
groupingBy() Map<City,List<Person>>
bill
jon
amy Athens
London
Collector<Person,?,Map<City,List<Person>
groupingBy(Function<Person,City>)
Classifier
Person→City
Lambda Masterclass, JavaDay.ro, December 2018
Stream<Person>
groupingBy() Map<City,List<Person>>
bill
jon
amy Athens
London
groupingBy(Function<Person,City>)
Lambda Masterclass, JavaDay.ro, December 2018
Stream<Person>
groupingBy() Map<City,List<Person>>
bill
jon
amy Athens
bill London
groupingBy(Function<Person,City>)
Lambda Masterclass, JavaDay.ro, December 2018
Stream<Person>
groupingBy() Map<City,List<Person>>
bill
jon
amy Athens
billLondon
groupingBy(Function<Person,City>)
[ ]
Lambda Masterclass, JavaDay.ro, December 2018
Stream<Person>
groupingBy() Map<City,List<Person>>
bill
jon
amy Athens [ ]
bill
amy
London
groupingBy(Function<Person,City>)
[ ]
Lambda Masterclass, JavaDay.ro, December 2018
Stream<Person>
groupingBy() Map<City,List<Person>>
bill
jon
amy Athens [ ]
[ , ]bill
amy
jonLondon
groupingBy(Function<Person,City>)
Lambda Masterclass, JavaDay.ro, December 2018
groupingBy() Map<City,List<Person>>
bill
jon
amy Athens [ ]
[ , ]bill
amy
jonLondon
groupingBy(Function<Person,City>)
Lambda Masterclass, JavaDay.ro, December 2018
groupingBy() Map<City,List<Person>>
bill
jon
amy
Athens [ ]
[ , ]bill
amy
jonLondon
groupingBy(Function<Person,City>)
Lambda Masterclass, JavaDay.ro, December 2018
groupingBy(Function<T,K> classifier)
Uses the classifier function to make a classification mapping
Like toMap(), except that the values placed in the map are lists of
the elements, one List corresponding to each classification key:
For example, use Person.getCity() to make a Map<City,List<Person>>
Map<City,List<Person>> peopleByCity = people.stream()
.collect(Collectors.groupingBy(Person::getCity));
Lambda Masterclass, JavaDay.ro, December 2018
groupingBy(classifier, downstream)
Uses the classifier function to make a classification mapping into a
container defined by a downstream Collector
Like toMap(), except that the values placed in the map are containers
of the elements, one container corresponding to each classification key:
For example, use Person.getCity() to make a Map<City,Set<Person>>
Map<City,Set<Person>> peopleByCity = people.stream()
.collect(Collectors.groupingBy(Person::getCity,toSet()));
Lambda Masterclass, JavaDay.ro, December 2018
groupingBy(Function classifier,Collector downstream))
Stream<Person>
groupingBy()
Map<City,Set<Person>
bill
jon
amy
London
Athens
Downstream
Collector
—
toSet()
Stream<Person
>
Classifier
Person→City
Lambda Masterclass, JavaDay.ro, December 2018
groupingBy(Function classifier,Collector downstream))
Stream<Person>
groupingBy()
Map<City,Set<Person>
bill
jon
amy
London
Athens
Lambda Masterclass, JavaDay.ro, December 2018
groupingBy(Function classifier,Collector downstream))
Stream<Person>
groupingBy()
Map<City,Set<Person>
bill
jon
amy
London
Athens
bill
Lambda Masterclass, JavaDay.ro, December 2018
groupingBy(Function classifier,Collector downstream))
Stream<Person>
groupingBy()
Map<City,Set<Person>
bill
jon
amy
London
Athens
bill
Lambda Masterclass, JavaDay.ro, December 2018
groupingBy(Function classifier,Collector downstream))
Stream<Person>
groupingBy()
Map<City,Set<Person>
bill
jon
amy
London
Athens
bill
Lambda Masterclass, JavaDay.ro, December 2018
groupingBy(Function classifier,Collector downstream))
Stream<Person>
groupingBy()
Map<City,Set<Person>
bill
jon
amy
London
Athensamy
bill
Lambda Masterclass, JavaDay.ro, December 2018
groupingBy(Function classifier,Collector downstream))
Stream<Person>
groupingBy()
Map<City,Set<Person>
bill
jon
amy
London
Athens amy
bill
Lambda Masterclass, JavaDay.ro, December 2018
groupingBy(Function classifier,Collector downstream))
Stream<Person>
groupingBy()
Map<City,Set<Person>
bill
jon
amy
London
Athens amy
bill
Lambda Masterclass, JavaDay.ro, December 2018
groupingBy(Function classifier,Collector downstream))
Stream<Person>
groupingBy()
Map<City,Set<Person>
bill
jon
amy
London
Athens amy
jon bill
Lambda Masterclass, JavaDay.ro, December 2018
groupingBy(Function classifier,Collector downstream))
Stream<Person>
groupingBy()
Map<City,Set<Person>
bill
jon
amy
London
Athens amy
jon
bill
Lambda Masterclass, JavaDay.ro, December 2018
groupingBy(Function classifier,Collector downstream))
groupingBy()
Map<City,Set<Person>
bill
jon
amy
London
Athens amy
jon
bill
Lambda Masterclass, JavaDay.ro, December 2018
groupingBy(Function classifier,Collector downstream))
groupingBy()
Map<City,Set<Person>
bill
jon
amy
London
Athens amy
jon
bill
Lambda Masterclass, JavaDay.ro, December 2018
groupingBy(Function classifier,Collector downstream))
groupingBy()
Map<City,Set<Person>
bill
jon
amy
London
Athens { }amy
{ , }jonbill
Lambda Masterclass, JavaDay.ro, December 2018
groupingBy(Function classifier,Collector downstream))
groupingBy()
Map<City,Set<Person>
bill
jon
amy
London
Athens { }amy
{ , }jonbill
Lambda Masterclass, JavaDay.ro, December 2018
mapping(Function mapper,Collector downstream))
Applies a mapping function to each input element before passing it
to the downstream collector.
Lambda Masterclass, JavaDay.ro, December 2018
mapping(Function mapper,Collector downstream))
Applies a mapping function to each input element before passing it
to the downstream collector.
Set<City> inhabited = people.stream()

.collect(mapping(Person::getCity,toSet()));
Animation example
Journey’s End, JavaOne, September 2016
mapping(Function mapper,Collector downstream))
LondonStream<Person>
mapping()
bill
jon
amy Athens
Mapper
Person→City
Downstream
Collector
—
toSet()
Stream<City>
Set<City>
Journey’s End, JavaOne, September 2016
mapping(Function mapper,Collector downstream))
LondonStream<Person>
mapping()
bill
jon
amy Athens
Set<City>
Journey’s End, JavaOne, September 2016
mapping(Function mapper,Collector downstream))
LondonStream<Person>
mapping()
bill
jon
amy Athens
London
Set<City>
Journey’s End, JavaOne, September 2016
mapping(Function mapper,Collector downstream))
LondonStream<Person>
mapping()
bill
jon
amy Athens
Athens
London
Set<City>
Journey’s End, JavaOne, September 2016
mapping(Function mapper,Collector downstream))
LondonStream<Person>
mapping()
bill
jon
amy Athens
Athens
LondonLondon
Set<City>
Journey’s End, JavaOne, September 2016
mapping(Function mapper,Collector downstream))
London
mapping()
bill
jon
amy Athens
Athens
LondonLondon
Set<City>
Journey’s End, JavaOne, September 2016
mapping(Function mapper,Collector downstream))
London
mapping()
bill
jon
amy
{ ,
}
Athens
Athens
London
Set<City>
Journey’s End, JavaOne, September 2016
mapping(Function mapper,Collector downstream))
London
mapping()
bill
jon
amy
{ ,
}
Athens
Athens
London
Set<City>
Lambda Masterclass, JavaDay.ro, December 2018
mapping(Function mapper,Collector downstream))
Applies a mapping function to each input element before passing it
to the downstream collector.
Set<City> inhabited = people.stream()

.collect(mapping(Person::getCity,toSet()));
Animation example
Lambda Masterclass, JavaDay.ro, December 2018
mapping(Function mapper,Collector downstream))
Applies a mapping function to each input element before passing it
to the downstream collector.
Set<City> inhabited = people.stream()

.collect(mapping(Person::getCity,toSet()));
Map<City,String> namesByCity = people.stream()

.collect(groupingBy(Person::getCity,
mapping(Person::getName,joining()));
Animation example
Sensible example
Collectors’ Fair, JavaOne, October 2016
groupingBy()
bill
jon
amy Athens
London
Collector<Person,?,String>
Classifier
Person→City
mapping()
Mapper
Person→String
Stream<String>
joining()
Collector<CharSequence,?,String>
Nesting Collectors
Journey’s End, JavaOne, September 2016
Dualling Convenience Reductions
Terminal operation Collectors factory method
count() counting()
max()
min()
maxBy()
minBy()
sum()
average()
summingXxx()
averagingXxx()
summaryStatistics() summarizingXxx()
reduce(accumulator)
reduce(identity, accumulator)
reduce(identity, accumulator, combiner)
reducing(accumulator)
reducing(identity, accumulator)
reducing(identity, accumulator, combiner)
Lambda Masterclass, JavaDay.ro, December 2018
Streams 1I
Demo and labs
Lambda Masterclass, JavaDay.ro, December 2018
What?You Want More??
Lambda Masterclass, JavaDay.ro, December 2018
What?You Want More??

More Related Content

What's hot

Python Jump Start
Python Jump StartPython Jump Start
Python Jump Start
Haim Michael
 
Java 8 lambda
Java 8 lambdaJava 8 lambda
Java 8 lambda
Manav Prasad
 
Java SE 8 library design
Java SE 8 library designJava SE 8 library design
Java SE 8 library design
Stephen Colebourne
 
Cs30 New
Cs30 NewCs30 New
Major Java 8 features
Major Java 8 featuresMajor Java 8 features
Major Java 8 features
Sanjoy Kumar Roy
 
Eclipse and Java 8 - Eclipse Day India 2013
Eclipse and Java 8 - Eclipse Day India 2013Eclipse and Java 8 - Eclipse Day India 2013
Eclipse and Java 8 - Eclipse Day India 2013
Noopur Gupta
 
Preparing for Scala 3
Preparing for Scala 3Preparing for Scala 3
Preparing for Scala 3
Martin Odersky
 
Eclipse Day India 2015 - Java 8 Overview
Eclipse Day India 2015 - Java 8 OverviewEclipse Day India 2015 - Java 8 Overview
Eclipse Day India 2015 - Java 8 Overview
Eclipse Day India
 
Java 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & StreamsJava 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & Streams
NewCircle Training
 
Programming with Lambda Expressions in Java
Programming with Lambda Expressions in Java Programming with Lambda Expressions in Java
Programming with Lambda Expressions in Java
langer4711
 
Lambda Expressions in Java 8
Lambda Expressions in Java 8Lambda Expressions in Java 8
Lambda Expressions in Java 8
icarter09
 
Functional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singhFunctional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singh
Harmeet Singh(Taara)
 
What To Leave Implicit
What To Leave ImplicitWhat To Leave Implicit
What To Leave Implicit
Martin Odersky
 
Generics On The JVM (What you don't know will hurt you)
Generics On The JVM (What you don't know will hurt you)Generics On The JVM (What you don't know will hurt you)
Generics On The JVM (What you don't know will hurt you)
Garth Gilmour
 
What To Leave Implicit
What To Leave ImplicitWhat To Leave Implicit
What To Leave Implicit
Martin Odersky
 
Java 8 Features
Java 8 FeaturesJava 8 Features
Java 8 Features
Trung Nguyen
 
A (too) Short Introduction to Scala
A (too) Short Introduction to ScalaA (too) Short Introduction to Scala
A (too) Short Introduction to Scala
Riccardo Cardin
 

What's hot (17)

Python Jump Start
Python Jump StartPython Jump Start
Python Jump Start
 
Java 8 lambda
Java 8 lambdaJava 8 lambda
Java 8 lambda
 
Java SE 8 library design
Java SE 8 library designJava SE 8 library design
Java SE 8 library design
 
Cs30 New
Cs30 NewCs30 New
Cs30 New
 
Major Java 8 features
Major Java 8 featuresMajor Java 8 features
Major Java 8 features
 
Eclipse and Java 8 - Eclipse Day India 2013
Eclipse and Java 8 - Eclipse Day India 2013Eclipse and Java 8 - Eclipse Day India 2013
Eclipse and Java 8 - Eclipse Day India 2013
 
Preparing for Scala 3
Preparing for Scala 3Preparing for Scala 3
Preparing for Scala 3
 
Eclipse Day India 2015 - Java 8 Overview
Eclipse Day India 2015 - Java 8 OverviewEclipse Day India 2015 - Java 8 Overview
Eclipse Day India 2015 - Java 8 Overview
 
Java 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & StreamsJava 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & Streams
 
Programming with Lambda Expressions in Java
Programming with Lambda Expressions in Java Programming with Lambda Expressions in Java
Programming with Lambda Expressions in Java
 
Lambda Expressions in Java 8
Lambda Expressions in Java 8Lambda Expressions in Java 8
Lambda Expressions in Java 8
 
Functional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singhFunctional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singh
 
What To Leave Implicit
What To Leave ImplicitWhat To Leave Implicit
What To Leave Implicit
 
Generics On The JVM (What you don't know will hurt you)
Generics On The JVM (What you don't know will hurt you)Generics On The JVM (What you don't know will hurt you)
Generics On The JVM (What you don't know will hurt you)
 
What To Leave Implicit
What To Leave ImplicitWhat To Leave Implicit
What To Leave Implicit
 
Java 8 Features
Java 8 FeaturesJava 8 Features
Java 8 Features
 
A (too) Short Introduction to Scala
A (too) Short Introduction to ScalaA (too) Short Introduction to Scala
A (too) Short Introduction to Scala
 

Similar to Lambda/Streams Hands-On Lab

Java 8 - An Overview
Java 8 - An OverviewJava 8 - An Overview
Java 8 - An Overview
Indrajit Das
 
Software Uni Conf October 2014
Software Uni Conf October 2014Software Uni Conf October 2014
Software Uni Conf October 2014Nayden Gochev
 
Functional Programming With Lambdas and Streams in JDK8
 Functional Programming With Lambdas and Streams in JDK8 Functional Programming With Lambdas and Streams in JDK8
Functional Programming With Lambdas and Streams in JDK8
IndicThreads
 
What's New in Java 8
What's New in Java 8What's New in Java 8
What's New in Java 8
javafxpert
 
Java 8 - Project Lambda
Java 8 - Project LambdaJava 8 - Project Lambda
Java 8 - Project Lambda
Rahman USTA
 
Hanoi JUG: Java 8 & lambdas
Hanoi JUG: Java 8 & lambdasHanoi JUG: Java 8 & lambdas
Hanoi JUG: Java 8 & lambdas
Benoît de CHATEAUVIEUX
 
Xebicon2013 scala vsjava_final
Xebicon2013 scala vsjava_finalXebicon2013 scala vsjava_final
Xebicon2013 scala vsjava_final
Urs Peter
 
Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...
Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...
Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...
Raffi Khatchadourian
 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentation
Van Huong
 
Lambdas and-streams-s ritter-v3
Lambdas and-streams-s ritter-v3Lambdas and-streams-s ritter-v3
Lambdas and-streams-s ritter-v3
Simon Ritter
 
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and StreamsIntroduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
Emiel Paasschens
 
Java 8
Java 8Java 8
Java 8
vilniusjug
 
How to code to code less
How to code to code lessHow to code to code less
How to code to code less
Anton Novikau
 
Colloquium Report
Colloquium ReportColloquium Report
Colloquium Report
Mohammad Faizan
 
Bring the fun back to java
Bring the fun back to javaBring the fun back to java
Bring the fun back to java
ciklum_ods
 
Lambda expressions
Lambda expressionsLambda expressions
Lambda expressionsDoron Gold
 
Functional programming with_jdk8-s_ritter
Functional programming with_jdk8-s_ritterFunctional programming with_jdk8-s_ritter
Functional programming with_jdk8-s_ritter
Simon Ritter
 
java150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptxjava150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptx
BruceLee275640
 
PHP 5.3 Part 2 - Lambda Functions & Closures
PHP 5.3 Part 2 - Lambda Functions & ClosuresPHP 5.3 Part 2 - Lambda Functions & Closures
PHP 5.3 Part 2 - Lambda Functions & Closures
melechi
 

Similar to Lambda/Streams Hands-On Lab (20)

Java 8 - An Overview
Java 8 - An OverviewJava 8 - An Overview
Java 8 - An Overview
 
Software Uni Conf October 2014
Software Uni Conf October 2014Software Uni Conf October 2014
Software Uni Conf October 2014
 
Functional Programming With Lambdas and Streams in JDK8
 Functional Programming With Lambdas and Streams in JDK8 Functional Programming With Lambdas and Streams in JDK8
Functional Programming With Lambdas and Streams in JDK8
 
Java 8 Lambda
Java 8 LambdaJava 8 Lambda
Java 8 Lambda
 
What's New in Java 8
What's New in Java 8What's New in Java 8
What's New in Java 8
 
Java 8 - Project Lambda
Java 8 - Project LambdaJava 8 - Project Lambda
Java 8 - Project Lambda
 
Hanoi JUG: Java 8 & lambdas
Hanoi JUG: Java 8 & lambdasHanoi JUG: Java 8 & lambdas
Hanoi JUG: Java 8 & lambdas
 
Xebicon2013 scala vsjava_final
Xebicon2013 scala vsjava_finalXebicon2013 scala vsjava_final
Xebicon2013 scala vsjava_final
 
Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...
Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...
Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...
 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentation
 
Lambdas and-streams-s ritter-v3
Lambdas and-streams-s ritter-v3Lambdas and-streams-s ritter-v3
Lambdas and-streams-s ritter-v3
 
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and StreamsIntroduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
 
Java 8
Java 8Java 8
Java 8
 
How to code to code less
How to code to code lessHow to code to code less
How to code to code less
 
Colloquium Report
Colloquium ReportColloquium Report
Colloquium Report
 
Bring the fun back to java
Bring the fun back to javaBring the fun back to java
Bring the fun back to java
 
Lambda expressions
Lambda expressionsLambda expressions
Lambda expressions
 
Functional programming with_jdk8-s_ritter
Functional programming with_jdk8-s_ritterFunctional programming with_jdk8-s_ritter
Functional programming with_jdk8-s_ritter
 
java150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptxjava150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptx
 
PHP 5.3 Part 2 - Lambda Functions & Closures
PHP 5.3 Part 2 - Lambda Functions & ClosuresPHP 5.3 Part 2 - Lambda Functions & Closures
PHP 5.3 Part 2 - Lambda Functions & Closures
 

More from Maurice Naftalin

What Lies Beneath
What Lies BeneathWhat Lies Beneath
What Lies Beneath
Maurice Naftalin
 
Complicating Complexity: Performance in a New Machine Age
Complicating Complexity: Performance in a New Machine AgeComplicating Complexity: Performance in a New Machine Age
Complicating Complexity: Performance in a New Machine Age
Maurice Naftalin
 
Journey's end
Journey's endJourney's end
Journey's end
Maurice Naftalin
 
Shooting the Rapids
Shooting the RapidsShooting the Rapids
Shooting the Rapids
Maurice Naftalin
 
Good and Wicked Fairies, and the Tragedy of the Commons: Understanding the Pe...
Good and Wicked Fairies, and the Tragedy of the Commons: Understanding the Pe...Good and Wicked Fairies, and the Tragedy of the Commons: Understanding the Pe...
Good and Wicked Fairies, and the Tragedy of the Commons: Understanding the Pe...
Maurice Naftalin
 
Parallel-Ready Java Code: Managing Mutation in an Imperative Language
Parallel-Ready Java Code: Managing Mutation in an Imperative LanguageParallel-Ready Java Code: Managing Mutation in an Imperative Language
Parallel-Ready Java Code: Managing Mutation in an Imperative Language
Maurice Naftalin
 
Let's Get to the Rapids
Let's Get to the RapidsLet's Get to the Rapids
Let's Get to the Rapids
Maurice Naftalin
 
Shooting the Rapids: Getting the Best from Java 8 Streams
Shooting the Rapids: Getting the Best from Java 8 StreamsShooting the Rapids: Getting the Best from Java 8 Streams
Shooting the Rapids: Getting the Best from Java 8 Streams
Maurice Naftalin
 
Journey's End – Collection and Reduction in the Stream API
Journey's End – Collection and Reduction in the Stream APIJourney's End – Collection and Reduction in the Stream API
Journey's End – Collection and Reduction in the Stream API
Maurice Naftalin
 

More from Maurice Naftalin (9)

What Lies Beneath
What Lies BeneathWhat Lies Beneath
What Lies Beneath
 
Complicating Complexity: Performance in a New Machine Age
Complicating Complexity: Performance in a New Machine AgeComplicating Complexity: Performance in a New Machine Age
Complicating Complexity: Performance in a New Machine Age
 
Journey's end
Journey's endJourney's end
Journey's end
 
Shooting the Rapids
Shooting the RapidsShooting the Rapids
Shooting the Rapids
 
Good and Wicked Fairies, and the Tragedy of the Commons: Understanding the Pe...
Good and Wicked Fairies, and the Tragedy of the Commons: Understanding the Pe...Good and Wicked Fairies, and the Tragedy of the Commons: Understanding the Pe...
Good and Wicked Fairies, and the Tragedy of the Commons: Understanding the Pe...
 
Parallel-Ready Java Code: Managing Mutation in an Imperative Language
Parallel-Ready Java Code: Managing Mutation in an Imperative LanguageParallel-Ready Java Code: Managing Mutation in an Imperative Language
Parallel-Ready Java Code: Managing Mutation in an Imperative Language
 
Let's Get to the Rapids
Let's Get to the RapidsLet's Get to the Rapids
Let's Get to the Rapids
 
Shooting the Rapids: Getting the Best from Java 8 Streams
Shooting the Rapids: Getting the Best from Java 8 StreamsShooting the Rapids: Getting the Best from Java 8 Streams
Shooting the Rapids: Getting the Best from Java 8 Streams
 
Journey's End – Collection and Reduction in the Stream API
Journey's End – Collection and Reduction in the Stream APIJourney's End – Collection and Reduction in the Stream API
Journey's End – Collection and Reduction in the Stream API
 

Recently uploaded

Empowering Growth with Best Software Development Company in Noida - Deuglo
Empowering Growth with Best Software  Development Company in Noida - DeugloEmpowering Growth with Best Software  Development Company in Noida - Deuglo
Empowering Growth with Best Software Development Company in Noida - Deuglo
Deuglo Infosystem Pvt Ltd
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Neo4j
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
Shane Coughlan
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
Aftab Hussain
 
Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)
abdulrafaychaudhry
 
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Łukasz Chruściel
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
Max Andersen
 
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Crescat
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOMLORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
lorraineandreiamcidl
 
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
Alina Yurenko
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
Ortus Solutions, Corp
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
Globus
 
Nidhi Software Price. Fact , Costs, Tips
Nidhi Software Price. Fact , Costs, TipsNidhi Software Price. Fact , Costs, Tips
Nidhi Software Price. Fact , Costs, Tips
vrstrong314
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
Neo4j
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Mind IT Systems
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 

Recently uploaded (20)

Empowering Growth with Best Software Development Company in Noida - Deuglo
Empowering Growth with Best Software  Development Company in Noida - DeugloEmpowering Growth with Best Software  Development Company in Noida - Deuglo
Empowering Growth with Best Software Development Company in Noida - Deuglo
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
 
Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)
 
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
 
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOMLORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
 
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
 
Nidhi Software Price. Fact , Costs, Tips
Nidhi Software Price. Fact , Costs, TipsNidhi Software Price. Fact , Costs, Tips
Nidhi Software Price. Fact , Costs, Tips
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 

Lambda/Streams Hands-On Lab

  • 1. Lambda Masterclass, JavaDay.ro, December 2018 Lambda Programming Laboratory A Masterclass for Java Developers JavaDay.ro 2019 Maurice Naftalin @mauricenaftalin
  • 2. Lambda Masterclass, JavaDay.ro, December 2018 • Intro + Setup • Lambdas and Method References • Comparators • Default Methods • Streams I • Streams II Agenda
  • 3. Lambda Masterclass, JavaDay.ro, December 2018 Maurice Naftalin
  • 4. Lambda Masterclass, JavaDay.ro, December 2018 Maurice Naftalin Java 5
  • 5. Lambda Masterclass, JavaDay.ro, December 2018 Maurice Naftalin Java 5 Java 8
  • 6. Lambda Masterclass, JavaDay.ro, December 2018 Maurice Naftalin Java 5 Java 8
  • 7. Lambda Masterclass, JavaDay.ro, December 2018 Maurice Naftalin Java 5 Java 8 2013
  • 8. Lambda Masterclass, JavaDay.ro, December 2018 Maurice Naftalin Java 5 Java 8 2014
  • 9. Lambda Masterclass, JavaDay.ro, December 2018 Maurice Naftalin Java 5 Java 8 2015
  • 10. Lambda Masterclass, JavaDay.ro, December 2018 Open https://github.com/mauricen/LambdaHOLv2/ Select Branch: Java 8 or Java 9 Click "Clone or download"; choose "Download ZIP" 
 – save to local disk, expand Setup 1
  • 11. Lambda Masterclass, JavaDay.ro, December 2018 Setup 2 IntelliJ IDEA Select New | Project from Existing Sources; select the new directory LambdaHOLv2-Java8/9, click Open, then Import project from external model, select Maven; click Next, Next, and Finish Check that the module SDK (set from File | Project Structure… | Modules, Dependencies tab) and the language level (Sources tab) are set correctly. Labs are in the package exercises.otherides, under the directory LambdaLab/test. Detailed lab instructions are in the README. Eclipse Select Preferences | Java | Editor | Folding, ensure Comments checkbox is ticked. Select File | Import… | Maven | Existing Maven Projects, click Next, select new directory LambdaHOL-v2-Java8/9, click Finish Labs are in the package exercises.eclipse, under the directory LambdaLab/test. Detailed lab instructions are in the README.
  • 12. Lambda Masterclass, JavaDay.ro, December 2018 • Intro + Setup • Lambdas and Method References • Comparators • Default Methods • Streams I • Streams II Agenda
  • 13. Lambda Masterclass, JavaDay.ro, December 2018 Lambdas–taking values to a higher order
  • 14. Lambda Masterclass, JavaDay.ro, December 2018 Lambdas–taking values to a higher order public interface Collection<E> { ... boolean removeAll(Collection<?> c); ... } instead of supplying values to specific library methods
  • 15. Lambda Masterclass, JavaDay.ro, December 2018 Lambdas–taking values to a higher order public interface Collection<E> { ... boolean removeAll(Collection<?> c); ... } public interface Collection<E> { ... boolean removeIf(Predicate<? super E> p); ... } instead of supplying values to specific library methods we want to supply behaviour to general library methods:
  • 16. Lambda Masterclass, JavaDay.ro, December 2018 Lambdas–taking values to a higher order Predicate is an interface with a single abstract boolean-valued method test. The method removeIf executes test for each element, and – public interface Collection<E> { ... boolean removeAll(Collection<?> c); ... } public interface Collection<E> { ... boolean removeIf(Predicate<? super E> p); ... } instead of supplying values to specific library methods we want to supply behaviour to general library methods:
  • 17. Lambda Masterclass, JavaDay.ro, December 2018 Lambdas–taking values to a higher order Predicate is an interface with a single abstract boolean-valued method test. The method removeIf executes test for each element, and – • if test returns true, removeIf removes that element public interface Collection<E> { ... boolean removeAll(Collection<?> c); ... } public interface Collection<E> { ... boolean removeIf(Predicate<? super E> p); ... } instead of supplying values to specific library methods we want to supply behaviour to general library methods:
  • 18. Lambda Masterclass, JavaDay.ro, December 2018 How can an instance of an interface represent behaviour? A behaviour is implemented by a single method! Predicate is an Interface
  • 19. Lambda Masterclass, JavaDay.ro, December 2018 How can an instance of an interface represent behaviour? A behaviour is implemented by a single method! Convention: if an interface has a single abstract method, like Predicate: then when the behaviour of an instance of the interface is wanted, the behaviour of that single method is used. public interface java.util.functions.Predicate<T> { boolean test(T t); } Predicate is an Interface
  • 20. Lambda Masterclass, JavaDay.ro, December 2018 How can an instance of an interface represent behaviour? A behaviour is implemented by a single method! Convention: if an interface has a single abstract method, like Predicate: then when the behaviour of an instance of the interface is wanted, the behaviour of that single method is used. public interface java.util.functions.Predicate<T> { boolean test(T t); } Functional Interface Predicate is an Interface
  • 21. Lambda Masterclass, JavaDay.ro, December 2018 How to make an Interface Instance?
  • 22. Lambda Masterclass, JavaDay.ro, December 2018 How to make an Interface Instance? The old way was using an anonymous inner class:
 
 
 // Predicate returns true for odd values of i new Predicate<Integer>(){ public boolean test(Integer i) { return i & 1 == 1; });
  • 23. Lambda Masterclass, JavaDay.ro, December 2018 How to make an Interface Instance? The old way was using an anonymous inner class:
 
 
 and we can still supply that as a method argument: // Predicate returns true for odd values of i new Predicate<Integer>(){ public boolean test(Integer i) { return i & 1 == 1; }); // remove odd values from integerList integerList.removeIf(new Predicate<Integer>(){ public boolean test(Integer i) { return i & 1 == 1; }));
  • 24. Lambda Masterclass, JavaDay.ro, December 2018 Stripping Out the Boilerplate
  • 25. Lambda Masterclass, JavaDay.ro, December 2018 // remove odd numbers from integerList integerList.removeIf(new Predicate<Integer>(){ public boolean test(Integer i) { return (i & 1) == 1; } }); Stripping Out the Boilerplate
  • 26. Lambda Masterclass, JavaDay.ro, December 2018 // remove odd numbers from integerList integerList.removeIf(new Predicate<Integer>(){ public boolean test(Integer i) { return (i & 1) == 1; } }); Stripping Out the Boilerplate Why do we have to say we’re supplying a Predicate?
  • 27. Lambda Masterclass, JavaDay.ro, December 2018 Why do we have to say we’re supplying a Predicate? // remove odd numbers from integerList integerList.removeIf(new Predicate<Integer>(){ public boolean test(Integer i) { return (i & 1) == 1; } }); Stripping Out the Boilerplate
  • 28. Lambda Masterclass, JavaDay.ro, December 2018 Why do we have to say we’re supplying a Predicate? // remove odd numbers from integerList integerList.removeIf(new Predicate<Integer>(){ public boolean test(Integer i) { return (i & 1) == 1; } }); Stripping Out the Boilerplate Why do we have to say we’re implementing test, the only abstract method?
  • 29. Lambda Masterclass, JavaDay.ro, December 2018 // remove odd numbers from integerList integerList.removeIf(new Predicate<Integer>(){ public boolean test(Integer i) { return (i & 1) == 1; } }); Stripping Out the Boilerplate Why do we have to say we’re implementing test, the only abstract method? Why do we have to say the type parameter is Integer? Why do we have to say we’re supplying a Predicate?
  • 30. Lambda Masterclass, JavaDay.ro, December 2018 // remove odd numbers from integerList integerList.removeIf(new Predicate<Integer>(){ public boolean test(Integer i) { return (i & 1) == 1; } }); Stripping Out the Boilerplate Why do we have to say we’re implementing test, the only abstract method? Why do we have to say the type parameter is Integer? Why do we have to say we’re supplying a Predicate?
  • 31. Lambda Masterclass, JavaDay.ro, December 2018 // remove odd numbers from integerList integerList.removeIf(i (i & 1) == 1) Stripping Out the Boilerplate Why do we have to say we’re implementing test, the only abstract method? Why do we have to say the type parameter is Integer? Why do we have to say we’re supplying a Predicate?
  • 32. Lambda Masterclass, JavaDay.ro, December 2018 // remove odd numbers from integerList integerList.removeIf(i (i & 1) == 1) Stripping Out the Boilerplate Why do we have to say we’re implementing test, the only abstract method? Why do we have to say the type parameter is Integer? Why do we have to say we’re supplying a Predicate? All we need is one extra syntax element!
  • 33. Lambda Masterclass, JavaDay.ro, December 2018 // remove odd numbers from integerList integerList.removeIf(i -> (i & 1) == 1) Stripping Out the Boilerplate Why do we have to say we’re implementing test, the only abstract method? Why do we have to say the type parameter is Integer? All we need is one extra syntax element! Why do we have to say we’re supplying a Predicate?
  • 34. Lambda Masterclass, JavaDay.ro, December 2018 // remove odd numbers from integerList integerList.removeIf(i -> (i & 1) == 1) Stripping Out the Boilerplate Why do we have to say we’re implementing test, the only abstract method? Why do we have to say the type parameter is Integer? All we need is one extra syntax element! Why do we have to say we’re supplying a Predicate? // remove odd numbers from integerList integerList.removeIf(i -> (i & 1) == 1)
  • 35. Lambda Masterclass, JavaDay.ro, December 2018 Rules for Writing Lambdas
  • 36. Lambda Masterclass, JavaDay.ro, December 2018 Rules for Writing Lambdas () -> 42
  • 37. Lambda Masterclass, JavaDay.ro, December 2018 Rules for Writing Lambdas () -> 42 a -> a + 1 // single untyped parameter – can omit parentheses
  • 38. Lambda Masterclass, JavaDay.ro, December 2018 Rules for Writing Lambdas () -> 42 a -> a + 1 // single untyped parameter – can omit parentheses (a,b) -> a + b // but they are needed for multiple parameters
  • 39. Lambda Masterclass, JavaDay.ro, December 2018 Rules for Writing Lambdas () -> 42 a -> a + 1 // single untyped parameter – can omit parentheses (a,b) -> a + b // but they are needed for multiple parameters (int a, int b) -> a + b;
 // type either all parameters, or none
  • 40. Lambda Masterclass, JavaDay.ro, December 2018 Rules for Writing Lambdas () -> 42 a -> a + 1 // single untyped parameter – can omit parentheses (a,b) -> a + b // but they are needed for multiple parameters (int a, int b) -> a + b;
 // type either all parameters, or none () -> { println(42); return 42; } 
 // lambda body can be a statement
  • 41. Lambda Masterclass, JavaDay.ro, December 2018 Target Typing Context is needed to define a lambda’s type:
  • 42. Lambda Masterclass, JavaDay.ro, December 2018 Target Typing Context is needed to define a lambda’s type: Runnable r = () -> {} // compiles
  • 43. Lambda Masterclass, JavaDay.ro, December 2018 Target Typing Context is needed to define a lambda’s type: Runnable r = () -> {} // compiles Thread t = new Thread(() -> {}) // compiles
  • 44. Lambda Masterclass, JavaDay.ro, December 2018 Target Typing Context is needed to define a lambda’s type: Runnable r = () -> {} // compiles Thread t = new Thread(() -> {}) // compiles Object o = () -> {} // illegal
  • 45. Lambda Masterclass, JavaDay.ro, December 2018 Method References Sometimes it’s more convenient to write lambdas as method references. There are four kinds: example lambda equivalent constructor ArrayList::new () -> new ArrayList() static Integer::max (x,y) -> Integer.max(x,y) bound “abc"::concat str -> "abc".concat(str) unbound String::length str -> str.length()
  • 46. Lambda Masterclass, JavaDay.ro, December 2018 Lambdas and Method References Demo and labs
  • 47. Lambda Masterclass, JavaDay.ro, December 2018 • Intro + Setup • Lambdas and Method References • Comparators • Default Methods • Streams I • Streams II Agenda
  • 48. Lambda Masterclass, JavaDay.ro, December 2018 Comparator Comparators were enhanced with static and default methods in Java 8. Now, instead of writing this: Comparator<Person> cmpr = new Comparator<>() { @Override public int compare(Person p1, Person p2) { int cmp = p1.getLastName().compareTo(p2.getLastName()); if (cmp == 0) { return p1.getFirstName().compareTo(p2.getFirstName()); } else { return cmp; } } };
  • 49. Lambda Masterclass, JavaDay.ro, December 2018 Comparator We can instead write using default and factory methods on Comparator Comparator<Person> cmp = Comparator.comparing(Person::getLastName) .thenComparing(Person::getFirstName) .thenComparing(Person::getAge);
  • 50. Lambda Masterclass, JavaDay.ro, December 2018 Comparator Methods returning Comparator<T> name argument(s) static comparing Function<T,U> comparing Function<T,U>, Comparator<U> comparingXxx ToXxxFunction<T> naturalOrder nullsFirst/Last Comparator<T> reverseOrder Comparator<T> default reversed thenComparing Comparator<T> thenComparingXxx ToXxxFunction<T>
  • 51. Lambda Masterclass, JavaDay.ro, December 2018 Example Domain firstName: String lastName: String age: int Person
  • 52. Lambda Masterclass, JavaDay.ro, December 2018 Comparators Demo and labs
  • 53. Lambda Masterclass, JavaDay.ro, December 2018 • Intro + Setup • Lambdas and Method References • Comparators • Default Methods • Streams 1 • Streams II Agenda
  • 54. Lambda Masterclass, JavaDay.ro, December 2018 Iterable<T> forEach(Consumer<T> c) supplies each element in turn to c removeIf(Predicate<T> p) removes elements satisfying p compute(K key, BiFunction<K,V,V> remappingFunction) computes a new mapping from key and its current mapped value (or null) computeIfAbsent(K key, Function<K,V> mfn) if there is no current mapping for key, creates one using mfn and enters it computeIfPresent(K key, BiFunction<K,V,V> remappingFunction) creates a mapping from key (if present and non-null) and its current mapped value forEach(BiConsumer<K,V> c) executes c for each key-value pair in this Map merge(K key, V v, BiFunction<V,V,V> remappingFunction) if key present, calculates new value from old value and v, o.w. associates key with v putIfAbsent(K key, V value) associates key with value if key is not present or is null remove(Object k, Object val) removes mapping for k from this map if it is present and has the value val replace(K key, V oldValue, V newValue) replaces the entry for key only if currently mapped to oldvalue replaceAll(BiFunction<K,V,V> fn) replaces each entry’s value with the result of applying fn to the entry’s key and value replaceAll(UnaryOperator<E> op) replaces each element with the result of applying op to it Map<K,V> List<E>
  • 55. Lambda Masterclass, JavaDay.ro, December 2018 Default Methods on Map returns name argument(s) void forEach BiConsumer<K,V> void replaceAll BiFunction<K,V,V> V computeIfAbsent K, Function<K,V> V computeIfPresent K, BiFunction<K,V,V> V compute K, BiFunction<K,V,V> V merge K, V, BiFunction<K,V,V> boolean remove Object, Object V replace K, V boolean replace K, V, V V putIfAbsent K, V
  • 56. Lambda Masterclass, JavaDay.ro, December 2018 Default Methods Demo and labs
  • 57. Lambda Masterclass, JavaDay.ro, December 2018 • Intro + Setup • Lambdas and Method References • Comparators • Default Methods • Streams I • Streams II Agenda
  • 58. x0 x1 x2 x3 Visualizing Stream Operations
  • 59. x0x1 x2 x3 Visualizing Stream Operations
  • 60. y0x1 x2 x3 Visualizing Stream Operations
  • 61. x1 x2 x3 Visualizing Stream Operations
  • 69. name returns interface used FI signature filter Stream<T> Predicate<T> T ➞ boolean map Stream<U> Function<T,U> T ➞ U flatMap Stream<R> Function<T,Stream<R>> T ➞ Stream<R> peek Stream<T> Consumer<T> T ➞ void Stateless Intermediate Operations 34
  • 70. name returns interface used FI signature filter Stream<T> Predicate<T> T ➞ boolean map Stream<U> Function<T,U> T ➞ U flatMap Stream<R> Function<T,Stream<R>> T ➞ Stream<R> peek Stream<T> Consumer<T> T ➞ void Stateless Intermediate Operations 34 mapToInt IntStream ToIntFunction<T> T ➞ int mapToLong LongStream ToLongFunction<T> T ➞ long mapToDouble DoubleStream ToDoubleFunction<T> T ➞ double
  • 71. filter() filter(s -> s.length() < 4) Stream<String> Predicate<String Stream<String> filter()
  • 72. filter() filter(s -> s.length() < 4) Stream<String> Predicate<String Stream<String> filter() “jim” ✔
  • 73. filter() filter(s -> s.length() < 4) Stream<String> Predicate<String Stream<String> filter() ✔
  • 74. filter() ✖ filter(s -> s.length() < 4) Stream<String> Predicate<String Stream<String> filter() ✔
  • 75. filter() ✖ filter(s -> s.length() < 4) Stream<String> Predicate<String Stream<String> filter() ✔ “amy”
  • 76. filter() ✖ filter(s -> s.length() < 4) Stream<String> Predicate<String Stream<String> filter() ✔
  • 88. name returns type used FI signature limit Stream<T> long skip Stream<T> long sorted Stream<T> Comparator<T> (T, T) ➞ int distinct Stream<T> takeWhile* Stream<T> Predicate<T> dropWhile* Stream<T> Predicate<T> Stateful Intermediate Operations * added in Java 9
  • 89. Lambda Masterclass, JavaDay.ro, December 2018 Collectors: Journey’s End for a Stream The Life of a Stream Element • Born (at a spliterator) • Transformed (by intermediate operations) • Collected (by a terminal operation)
  • 90. Lambda Masterclass, JavaDay.ro, December 2018 Collectors: Journey’s End for a Stream The Life of a Stream Element • Born (at a spliterator) • Transformed (by intermediate operations) • Collected (by a terminal operation)
  • 91. Lambda Masterclass, JavaDay.ro, December 2018 Collectors: Journey’s End for a Stream The Life of a Stream Element • Born (at a spliterator) • Transformed (by intermediate operations) • Collected (by a terminal operation)
  • 92. Lambda Masterclass, JavaDay.ro, December 2018 Collectors: Journey’s End for a Stream ? The Life of a Stream Element • Born (at a spliterator) • Transformed (by intermediate operations) • Collected (by a terminal operation)
  • 93. Lambda Masterclass, JavaDay.ro, December 2018 Terminal Operations – Search operations – Side-effecting operations – Reductions

  • 94. Lambda Masterclass, JavaDay.ro, December 2018 Search Operations Search operations - • allMatch, anyMatch • findAny, findFirst boolean allAdults = people.stream()
 .allMatch(p -> p.getAge() >= 21);

  • 95. Lambda Masterclass, JavaDay.ro, December 2018 Side-effecting Operations Side-effecting operations • forEach, forEachOrdered
 
 people.stream()
 .forEach(System.out::println); • So could we calculate total ages like this?
 
 int sum = 0;
 people.stream()
 .mapToInt(Person::getAge)
 .forEach(a -> { sum += a }); people.stream()
 .forEach(System.out::println);
 int sum = 0;
 people.stream()
 .mapToInt(Person::getAge)
 .forEach(a -> { sum += a; });

  • 96. Lambda Masterclass, JavaDay.ro, December 2018 Side-effecting Operations Side-effecting operations • forEach, forEachOrdered
 
 people.stream()
 .forEach(System.out::println); • So could we calculate total ages like this?
 
 int sum = 0;
 people.stream()
 .mapToInt(Person::getAge)
 .forEach(a -> { sum += a }); people.stream()
 .forEach(System.out::println);
 int sum = 0;
 people.stream()
 .mapToInt(Person::getAge)
 .forEach(a -> { sum += a; });
 Don’t do this!
  • 97. Lambda Masterclass, JavaDay.ro, December 2018 • Using an accumulator: Reduction – Why? int[] vals = new int[100]; Arrays.setAll(vals, i -> i); int sum = 0; for (int i = 0 ; i < vals.length ; i++) { sum += vals[i]; }

  • 98. Lambda Masterclass, JavaDay.ro, December 2018 • Using an accumulator: Reduction – Why? int[] vals = new int[100]; Arrays.setAll(vals, i -> i); int sum = 0; for (int i = 0 ; i < vals.length ; i++) { sum += vals[i]; }
 0 1 2 3 + + +
  • 99. Lambda Masterclass, JavaDay.ro, December 2018 • Avoiding an accumulator: Reduction – Why?
  • 100. Lambda Masterclass, JavaDay.ro, December 2018 • Avoiding an accumulator: Reduction – Why?
  • 101. Lambda Masterclass, JavaDay.ro, December 2018 • Avoiding an accumulator: Reduction – Why? int[] vals = new int[100]; Arrays.setAll(vals, i -> i); OptionalInt sum = Arrays.stream(vals) .reduce((a,b) -> a + b);
 + + + 1 2 30
  • 102. Lambda Masterclass, JavaDay.ro, December 2018 • Avoiding an accumulator: Reduction – Why? int[] vals = new int[100]; Arrays.setAll(vals, i -> i); OptionalInt sum = Arrays.stream(vals) .reduce((a,b) -> a + b);
 + + + 1 2 30 + + +
  • 103. Lambda Masterclass, JavaDay.ro, December 2018 • Avoiding an accumulator: Reduction – Why? int[] vals = new int[100]; Arrays.setAll(vals, i -> i); OptionalInt sum = Arrays.stream(vals) .reduce((a,b) -> a + b);
 BinaryOperator must be associative! + + + 1 2 30 + + +
  • 104. Lambda Masterclass, JavaDay.ro, December 2018 • Avoiding an accumulator: Reduction – Why? int[] vals = new int[100]; Arrays.setAll(vals, i -> i); OptionalInt sum = Arrays.stream(vals) .reduce((a,b) -> a + b);
 BinaryOperator must be associative! a + (b + c) = (a + b) + c + + + 1 2 30 + + +
  • 105. Lambda Masterclass, JavaDay.ro, December 2018 0 1 2 3 4 5 6 7 + + + + + + + Reduction – Why?
  • 106. Lambda Masterclass, JavaDay.ro, December 2018 0 1 2 3 4 5 6 7 + + + + + + + Reduction – Why?
  • 107. Lambda Masterclass, JavaDay.ro, December 2018 0 1 2 3 4 5 6 7 + + + + + + + Intermediate operations Reduction – Why?
  • 108. Lambda Masterclass, JavaDay.ro, December 2018 Reduction on ImmutableValues Reduction works on immutable values too BigDecimal[] vals = new BigDecimal[100]; Arrays.setAll(vals, i -> new BigDecimal(i)); Optional<BigDecimal> sum = Arrays.stream(vals) .reduce(BigDecimal::add);

  • 109. Lambda Masterclass, JavaDay.ro, December 2018 Streams 1 Demo and labs
  • 110. Lambda Masterclass, JavaDay.ro, December 2018 • Intro + Setup • Lambdas and Method References • Comparators • Default Methods • Streams I • Streams II Agenda
  • 111. Lambda Masterclass, JavaDay.ro, December 2018 What about Collections?
  • 112. Lambda Masterclass, JavaDay.ro, December 2018 What about Collections? This also works with immutable values.
  • 113. Lambda Masterclass, JavaDay.ro, December 2018 What about Collections? This also works with immutable values. And even collections – sort of … but then • for each element, client code has to create a new empty collection and add the single element to it, • and combine collections using addAll
  • 114. Lambda Masterclass, JavaDay.ro, December 2018 What about Collections? This also works with immutable values. And even collections – sort of … but then • for each element, client code has to create a new empty collection and add the single element to it, • and combine collections using addAll ... it’s hopelessly inefficient!
  • 115. Lambda Masterclass, JavaDay.ro, December 2018 What about Collections?
  • 116. Lambda Masterclass, JavaDay.ro, December 2018 What about Collections? This also works with immutable values.
  • 117. Lambda Masterclass, JavaDay.ro, December 2018 What about Collections? This also works with immutable values. But collections need to
  • 118. Lambda Masterclass, JavaDay.ro, December 2018 What about Collections? This also works with immutable values. But collections need to • be initialised
  • 119. Lambda Masterclass, JavaDay.ro, December 2018 What about Collections? This also works with immutable values. But collections need to • be initialised • have individual elements added
  • 120. Lambda Masterclass, JavaDay.ro, December 2018 What about Collections? This also works with immutable values. But collections need to • be initialised • have individual elements added • be merged
  • 121. Lambda Masterclass, JavaDay.ro, December 2018 A better idea 
 would be
  • 122. Lambda Masterclass, JavaDay.ro, December 2018 A better idea 
 would be Reduction over an Identity
  • 123. Lambda Masterclass, JavaDay.ro, December 2018 Reduction over an Identity
  • 124. Lambda Masterclass, JavaDay.ro, December 2018 Reduction over an Identity BigDecimal[] vals = new BigDecimal[100]; Arrays.setAll(vals, i -> new BigDecimal(i)); BigDecimal sum = Arrays.stream(vals) .reduce(BigDecimal.ZERO, BigDecimal::add);
 Works for primitives (of course) and also immutable objects:
  • 125. Lambda Masterclass, JavaDay.ro, December 2018 Reduction over an Identity + + + 0 1 2 3 + + + 0 4 5 6 7 0 ++ +
  • 126. Lambda Masterclass, JavaDay.ro, December 2018 So, Reduction to Collections?
  • 127. Lambda Masterclass, JavaDay.ro, December 2018 So, Reduction to Collections? Reduction over an identity doesn’t work with collections at all
  • 128. Lambda Masterclass, JavaDay.ro, December 2018 So, Reduction to Collections? Reduction over an identity doesn’t work with collections at all • reduction reuses the identity element
  • 129. Lambda Masterclass, JavaDay.ro, December 2018 So, Reduction to Collections? Reduction over an identity doesn’t work with collections at all • reduction reuses the identity element We need something better!
  • 130. Lambda Masterclass, JavaDay.ro, December 2018 The Answer – Collectors a a a e0 e1 e2 e3 a a a e4 e5 e6 e7 aa c
  • 131. Lambda Masterclass, JavaDay.ro, December 2018 The Answer – Collectors a a a e0 e1 e2 e3 a a a () -> [] e4 e5 e6 e7 aa c () -> []
  • 132. Lambda Masterclass, JavaDay.ro, December 2018 The Answer – Collectors a a a e0 e1 e2 e3 a a a () -> [] e4 e5 e6 e7 aa c a: accumulator c: combiner () -> []
  • 133. Lambda Masterclass, JavaDay.ro, December 2018 Collectors
  • 134. Lambda Masterclass, JavaDay.ro, December 2018 So to define a collector, you need to provide • Supplier • Accumulator • Combiner Collectors
  • 135. Lambda Masterclass, JavaDay.ro, December 2018 So to define a collector, you need to provide • Supplier • Accumulator • Combiner That sounds really hard… Collectors
  • 136. Lambda Masterclass, JavaDay.ro, December 2018 So to define a collector, you need to provide • Supplier • Accumulator • Combiner That sounds really hard… Good then that we don’t have to do it! Collectors
  • 137. Lambda Masterclass, JavaDay.ro, December 2018 Example Domain city: City name: String age: int Person Person amy = new Person(Athens, "Amy", 21);
 ... List<Person> people = Arrays.asList(jon, amy, bill);

  • 138. Lambda Masterclass, JavaDay.ro, December 2018 • Why collectors? • Using the predefined collectors • Worked problems
 Journey’s End – Agenda
  • 139. Lambda Masterclass, JavaDay.ro, December 2018 Using the Predefined Collectors Predefined Standalone Collectors – from factory methods in Collectors class • toList(), toSet(), toMap(), joining() • toMap(), toCollection() • groupingBy(), partitioningBy(), groupingByConcurrent()
  • 140. Lambda Masterclass, JavaDay.ro, December 2018 Using the Predefined Collectors Predefined Standalone Collectors – from factory methods in Collectors class • toList(), toSet(), toMap(), joining() • toMap(), toCollection() • groupingBy(), partitioningBy(), groupingByConcurrent() framework provides the Supplier
  • 141. Lambda Masterclass, JavaDay.ro, December 2018 Using the Predefined Collectors Predefined Standalone Collectors – from factory methods in Collectors class • toList(), toSet(), toMap(), joining() • toMap(), toCollection() • groupingBy(), partitioningBy(), groupingByConcurrent() user provides the Supplier framework provides the Supplier
  • 142. Lambda Masterclass, JavaDay.ro, December 2018 Using the Predefined Collectors Predefined Standalone Collectors – from factory methods in Collectors class • toList(), toSet(), toMap(), joining() • toMap(), toCollection() • groupingBy(), partitioningBy(), groupingByConcurrent() user provides the Supplier produce a 
 classification map framework provides the Supplier
  • 143. Lambda Masterclass, JavaDay.ro, December 2018 Simple Collector – toSet()
  • 144. Lambda Masterclass, JavaDay.ro, December 2018 Simple Collector – toSet() people.stream().collect(Collectors.toSet())
  • 145. Lambda Masterclass, JavaDay.ro, December 2018 Simple Collector – toSet() Collectors.toSet() people.stream().collect(Collectors.toSet())
  • 146. Lambda Masterclass, JavaDay.ro, December 2018 Simple Collector – toSet() Stream<Person> Collectors.toSet() Set<Person> people.stream().collect(Collectors.toSet()) Collector<Person,?,Set<Person>>
  • 147. Lambda Masterclass, JavaDay.ro, December 2018 Simple Collector – toSet() Stream<Person> Collectors.toSet() Set<Person> people.stream().collect(Collectors.toSet()) bill Collector<Person,?,Set<Person>>
  • 148. Lambda Masterclass, JavaDay.ro, December 2018 Simple Collector – toSet() Stream<Person> Collectors.toSet() Set<Person> people.stream().collect(Collectors.toSet()) bill Collector<Person,?,Set<Person>>
  • 149. Lambda Masterclass, JavaDay.ro, December 2018 Simple Collector – toSet() Stream<Person> Collectors.toSet() Set<Person> people.stream().collect(Collectors.toSet()) bill Collector<Person,?,Set<Person>>
  • 150. Lambda Masterclass, JavaDay.ro, December 2018 Simple Collector – toSet() Stream<Person> Collectors.toSet() Set<Person> people.stream().collect(Collectors.toSet()) bill jon Collector<Person,?,Set<Person>>
  • 151. Lambda Masterclass, JavaDay.ro, December 2018 Simple Collector – toSet() Stream<Person> Collectors.toSet() Set<Person> people.stream().collect(Collectors.toSet()) bill jon Collector<Person,?,Set<Person>>
  • 152. Lambda Masterclass, JavaDay.ro, December 2018 Simple Collector – toSet() Stream<Person> Collectors.toSet() Set<Person> people.stream().collect(Collectors.toSet()) amy bill jon Collector<Person,?,Set<Person>>
  • 153. Lambda Masterclass, JavaDay.ro, December 2018 Simple Collector – toSet() Stream<Person> Collectors.toSet() Set<Person> people.stream().collect(Collectors.toSet()) amy bill jon Collector<Person,?,Set<Person>>
  • 154. Lambda Masterclass, JavaDay.ro, December 2018 Simple Collector – toSet() Collectors.toSet() Set<Person> people.stream().collect(Collectors.toSet()) amy bill jon Collector<Person,?,Set<Person>>
  • 155. Lambda Masterclass, JavaDay.ro, December 2018 Simple Collector – toSet() Collectors.toSet() Set<Person> { , , } people.stream().collect(Collectors.toSet()) amybilljon Collector<Person,?,Set<Person>>
  • 156. Lambda Masterclass, JavaDay.ro, December 2018 toMap(Function<T,K> keyMapper, Function<T,U> valueMapper) people.stream().collect( Collectors.toMap( Person::getCity,
 Person::getName))
  • 157. Lambda Masterclass, JavaDay.ro, December 2018 toMap(Function<T,K> keyMapper, Function<T,U> valueMapper) Stream<Person> toMap() Map<City,String> Collector<Person,?,Map<City,String> people.stream().collect(toMap(Person::getCity,Person::getName))
  • 158. Lambda Masterclass, JavaDay.ro, December 2018 toMap(Function<T,K> keyMapper, Function<T,U> valueMapper) Stream<Person> toMap() Map<City,String> Collector<Person,?,Map<City,String>
  • 159. Lambda Masterclass, JavaDay.ro, December 2018 toMap(Function<T,K> keyMapper, Function<T,U> valueMapper) Stream<Person> toMap() Map<City,String> bill
  • 160. Lambda Masterclass, JavaDay.ro, December 2018 toMap(Function<T,K> keyMapper, Function<T,U> valueMapper) Stream<Person> toMap() Map<City,String> bill Person::getCity
  • 161. Lambda Masterclass, JavaDay.ro, December 2018 toMap(Function<T,K> keyMapper, Function<T,U> valueMapper) Stream<Person> toMap() Map<City,String> London bill
  • 162. Lambda Masterclass, JavaDay.ro, December 2018 toMap(Function<T,K> keyMapper, Function<T,U> valueMapper) Stream<Person> toMap() Map<City,String> London bill Person::getName
  • 163. Lambda Masterclass, JavaDay.ro, December 2018 toMap(Function<T,K> keyMapper, Function<T,U> valueMapper) Stream<Person> toMap() Map<City,String> London “Bill” bill Person::getName
  • 164. Lambda Masterclass, JavaDay.ro, December 2018 toMap(Function<T,K> keyMapper, Function<T,U> valueMapper) Stream<Person> toMap() Map<City,String> London “Bill”
  • 165. Lambda Masterclass, JavaDay.ro, December 2018 toMap(Function<T,K> keyMapper, Function<T,U> valueMapper) Stream<Person> toMap() Map<City,String> amy London “Bill”
  • 166. Lambda Masterclass, JavaDay.ro, December 2018 toMap(Function<T,K> keyMapper, Function<T,U> valueMapper) Stream<Person> toMap() Map<City,String> Athens amy London “Bill”
  • 167. Lambda Masterclass, JavaDay.ro, December 2018 toMap(Function<T,K> keyMapper, Function<T,U> valueMapper) Stream<Person> toMap() Map<City,String> “Amy”Athens London “Bill”
  • 168. Lambda Masterclass, JavaDay.ro, December 2018 toMap(Function<T,K> keyMapper, Function<T,U> valueMapper) Stream<Person> toMap() Map<City,String> “Amy”Athens jon London “Bill”
  • 169. Lambda Masterclass, JavaDay.ro, December 2018 toMap(Function<T,K> keyMapper, Function<T,U> valueMapper) Stream<Person> toMap() Map<City,String> “Amy”Athens London “Bill” Tulsa “Jon”
  • 170. Lambda Masterclass, JavaDay.ro, December 2018 toMap(Function<T,K> keyMapper, Function<T,U> valueMapper) toMap() Map<City,String> “Amy”Athens London “Bill” Tulsa “Jon”
  • 171. Lambda Masterclass, JavaDay.ro, December 2018 toMap(Function<T,K> keyMapper, Function<T,U> valueMapper) toMap() Map<City,String> “Amy”Athens London “Bill” Tulsa “Jon”
  • 172. Lambda Masterclass, JavaDay.ro, December 2018 toMap(Function<T,K> keyMapper, Function<T,U> valueMapper) Stream<Person> toMap() Map<City,String>
  • 173. Lambda Masterclass, JavaDay.ro, December 2018 toMap(Function<T,K> keyMapper, Function<T,U> valueMapper) Stream<Person> toMap() Map<City,String> “Amy”Athens jon London “Bill”
  • 174. Lambda Masterclass, JavaDay.ro, December 2018 toMap(Function<T,K> keyMapper, Function<T,U> valueMapper) Stream<Person> toMap() Map<City,String> “Amy”Athens jon London “Bill” Athens
  • 175. Lambda Masterclass, JavaDay.ro, December 2018 toMap(Function<T,K> keyMapper, Function<T,U> valueMapper) Stream<Person> toMap() Map<City,String> “Amy”Athens jon London IllegalStateException “Bill” Athens
  • 176. Lambda Masterclass, JavaDay.ro, December 2018 toMap(Function<T,K> keyMapper, Function<T,U> valueMapper BinaryOperator<U> mergeFunction) people.stream().collect( Collectors.toMap( Person::getCity,
 Person::getName, (a,b) -> a.concat(b)))
  • 177. Lambda Masterclass, JavaDay.ro, December 2018 toMap(Function<T,K> keyMapper, Function<T,U> valueMapper BinaryOperator<U> mergeFunction) Stream<Person> toMap() Map<City,String>
  • 178. Lambda Masterclass, JavaDay.ro, December 2018 toMap(Function<T,K> keyMapper, Function<T,U> valueMapper BinaryOperator<U> mergeFunction) Stream<Person> toMap() Map<City,String> “Amy”Athens London “Bill” “Jon” Athens
  • 179. Lambda Masterclass, JavaDay.ro, December 2018 toMap(Function<T,K> keyMapper, Function<T,U> valueMapper BinaryOperator<U> mergeFunction) Stream<Person> toMap() Map<City,String> “Amy”Athens London “Bill” “Jon” (a,b) -> a.concat(b) Athens
  • 180. Lambda Masterclass, JavaDay.ro, December 2018 toMap(Function<T,K> keyMapper, Function<T,U> valueMapper BinaryOperator<U> mergeFunction) Stream<Person> toMap() Map<City,String> Athens London “Bill” (a,b) -> a.concat(b) “AmyJon” Athens
  • 181. Lambda Masterclass, JavaDay.ro, December 2018 Collectors API Factory methods in the Collectors class.They produce standalone collectors, accumulating to: • framework-supplied containers • custom collections • classification maps
  • 182. Lambda Masterclass, JavaDay.ro, December 2018 toMap(Function<T,K> keyMapper, Function<T,U> valueMapper BinaryOperator<U> mergeFunction) toMap(Function<T,K> keyMapper, Function<T,U> valueMapper BinaryOperator<U> mergeFunction, Supplier<M> mapFactory) people.stream().collect( Collectors.toMap( Person::getCity,
 Person::getName, (a,b) -> a.concat(b),
 TreeMap::new))
  • 183. Lambda Masterclass, JavaDay.ro, December 2018 toMap(Function<T,K> keyMapper, Function<T,U> valueMapper BinaryOperator<U> mergeFunction, Supplier<M> mapFactory) Stream<Person> toMap() Map<City,String>
  • 184. Lambda Masterclass, JavaDay.ro, December 2018 toMap(Function<T,K> keyMapper, Function<T,U> valueMapper BinaryOperator<U> mergeFunction, Supplier<M> mapFactory) Stream<Person> toMap() Map<City,String>
  • 185. Lambda Masterclass, JavaDay.ro, December 2018 Collecting to Custom Collections
  • 186. Lambda Masterclass, JavaDay.ro, December 2018 Collecting to Custom Collections toCollection(Supplier<C> collectionFactory)
  • 187. Lambda Masterclass, JavaDay.ro, December 2018 Collecting to Custom Collections <C extends Collection<T>> toCollection(Supplier<C> collectionFactory)
  • 188. Lambda Masterclass, JavaDay.ro, December 2018 Collecting to Custom Collections returns Collector<T,?,C> <C extends Collection<T>> toCollection(Supplier<C> collectionFactory)
  • 189. Lambda Masterclass, JavaDay.ro, December 2018 Collecting to Custom Collections returns Collector<T,?,C> <C extends Collection<T>> NavigableSet<String> sortedNames = people.stream() .map(Person::getName)
 .collect(toCollection(TreeSet::new)); toCollection(Supplier<C> collectionFactory)
  • 190. Lambda Masterclass, JavaDay.ro, December 2018 Collectors API Factory methods in the Collectors class.They produce standalone collectors, accumulating to: • framework-supplied containers • custom collections • classification maps
  • 191. Lambda Masterclass, JavaDay.ro, December 2018 Collecting to Classification Maps groupingBy • simple • with downstream • with downstream and map factory partitioningBy
  • 192. Lambda Masterclass, JavaDay.ro, December 2018 groupingBy(Function<T,K> classifier) Uses the classifier function to make a classification mapping Like toMap(), except that the values placed in the map are lists of the elements, one List corresponding to each classification key: For example, use Person.getCity() to make a Map<City,List<Person>> Map<City,List<Person>> peopleByCity = people.stream() .collect(Collectors.groupingBy(Person::getCity));
  • 193. Lambda Masterclass, JavaDay.ro, December 2018 Stream<Person> groupingBy() Map<City,List<Person>> bill jon amy Athens London Collector<Person,?,Map<City,List<Person> groupingBy(Function<Person,City>) Classifier Person→City
  • 194. Lambda Masterclass, JavaDay.ro, December 2018 Stream<Person> groupingBy() Map<City,List<Person>> bill jon amy Athens London groupingBy(Function<Person,City>)
  • 195. Lambda Masterclass, JavaDay.ro, December 2018 Stream<Person> groupingBy() Map<City,List<Person>> bill jon amy Athens bill London groupingBy(Function<Person,City>)
  • 196. Lambda Masterclass, JavaDay.ro, December 2018 Stream<Person> groupingBy() Map<City,List<Person>> bill jon amy Athens billLondon groupingBy(Function<Person,City>) [ ]
  • 197. Lambda Masterclass, JavaDay.ro, December 2018 Stream<Person> groupingBy() Map<City,List<Person>> bill jon amy Athens [ ] bill amy London groupingBy(Function<Person,City>) [ ]
  • 198. Lambda Masterclass, JavaDay.ro, December 2018 Stream<Person> groupingBy() Map<City,List<Person>> bill jon amy Athens [ ] [ , ]bill amy jonLondon groupingBy(Function<Person,City>)
  • 199. Lambda Masterclass, JavaDay.ro, December 2018 groupingBy() Map<City,List<Person>> bill jon amy Athens [ ] [ , ]bill amy jonLondon groupingBy(Function<Person,City>)
  • 200. Lambda Masterclass, JavaDay.ro, December 2018 groupingBy() Map<City,List<Person>> bill jon amy Athens [ ] [ , ]bill amy jonLondon groupingBy(Function<Person,City>)
  • 201. Lambda Masterclass, JavaDay.ro, December 2018 groupingBy(Function<T,K> classifier) Uses the classifier function to make a classification mapping Like toMap(), except that the values placed in the map are lists of the elements, one List corresponding to each classification key: For example, use Person.getCity() to make a Map<City,List<Person>> Map<City,List<Person>> peopleByCity = people.stream() .collect(Collectors.groupingBy(Person::getCity));
  • 202. Lambda Masterclass, JavaDay.ro, December 2018 groupingBy(classifier, downstream) Uses the classifier function to make a classification mapping into a container defined by a downstream Collector Like toMap(), except that the values placed in the map are containers of the elements, one container corresponding to each classification key: For example, use Person.getCity() to make a Map<City,Set<Person>> Map<City,Set<Person>> peopleByCity = people.stream() .collect(Collectors.groupingBy(Person::getCity,toSet()));
  • 203. Lambda Masterclass, JavaDay.ro, December 2018 groupingBy(Function classifier,Collector downstream)) Stream<Person> groupingBy() Map<City,Set<Person> bill jon amy London Athens Downstream Collector — toSet() Stream<Person > Classifier Person→City
  • 204. Lambda Masterclass, JavaDay.ro, December 2018 groupingBy(Function classifier,Collector downstream)) Stream<Person> groupingBy() Map<City,Set<Person> bill jon amy London Athens
  • 205. Lambda Masterclass, JavaDay.ro, December 2018 groupingBy(Function classifier,Collector downstream)) Stream<Person> groupingBy() Map<City,Set<Person> bill jon amy London Athens bill
  • 206. Lambda Masterclass, JavaDay.ro, December 2018 groupingBy(Function classifier,Collector downstream)) Stream<Person> groupingBy() Map<City,Set<Person> bill jon amy London Athens bill
  • 207. Lambda Masterclass, JavaDay.ro, December 2018 groupingBy(Function classifier,Collector downstream)) Stream<Person> groupingBy() Map<City,Set<Person> bill jon amy London Athens bill
  • 208. Lambda Masterclass, JavaDay.ro, December 2018 groupingBy(Function classifier,Collector downstream)) Stream<Person> groupingBy() Map<City,Set<Person> bill jon amy London Athensamy bill
  • 209. Lambda Masterclass, JavaDay.ro, December 2018 groupingBy(Function classifier,Collector downstream)) Stream<Person> groupingBy() Map<City,Set<Person> bill jon amy London Athens amy bill
  • 210. Lambda Masterclass, JavaDay.ro, December 2018 groupingBy(Function classifier,Collector downstream)) Stream<Person> groupingBy() Map<City,Set<Person> bill jon amy London Athens amy bill
  • 211. Lambda Masterclass, JavaDay.ro, December 2018 groupingBy(Function classifier,Collector downstream)) Stream<Person> groupingBy() Map<City,Set<Person> bill jon amy London Athens amy jon bill
  • 212. Lambda Masterclass, JavaDay.ro, December 2018 groupingBy(Function classifier,Collector downstream)) Stream<Person> groupingBy() Map<City,Set<Person> bill jon amy London Athens amy jon bill
  • 213. Lambda Masterclass, JavaDay.ro, December 2018 groupingBy(Function classifier,Collector downstream)) groupingBy() Map<City,Set<Person> bill jon amy London Athens amy jon bill
  • 214. Lambda Masterclass, JavaDay.ro, December 2018 groupingBy(Function classifier,Collector downstream)) groupingBy() Map<City,Set<Person> bill jon amy London Athens amy jon bill
  • 215. Lambda Masterclass, JavaDay.ro, December 2018 groupingBy(Function classifier,Collector downstream)) groupingBy() Map<City,Set<Person> bill jon amy London Athens { }amy { , }jonbill
  • 216. Lambda Masterclass, JavaDay.ro, December 2018 groupingBy(Function classifier,Collector downstream)) groupingBy() Map<City,Set<Person> bill jon amy London Athens { }amy { , }jonbill
  • 217. Lambda Masterclass, JavaDay.ro, December 2018 mapping(Function mapper,Collector downstream)) Applies a mapping function to each input element before passing it to the downstream collector.
  • 218. Lambda Masterclass, JavaDay.ro, December 2018 mapping(Function mapper,Collector downstream)) Applies a mapping function to each input element before passing it to the downstream collector. Set<City> inhabited = people.stream()
 .collect(mapping(Person::getCity,toSet())); Animation example
  • 219. Journey’s End, JavaOne, September 2016 mapping(Function mapper,Collector downstream)) LondonStream<Person> mapping() bill jon amy Athens Mapper Person→City Downstream Collector — toSet() Stream<City> Set<City>
  • 220. Journey’s End, JavaOne, September 2016 mapping(Function mapper,Collector downstream)) LondonStream<Person> mapping() bill jon amy Athens Set<City>
  • 221. Journey’s End, JavaOne, September 2016 mapping(Function mapper,Collector downstream)) LondonStream<Person> mapping() bill jon amy Athens London Set<City>
  • 222. Journey’s End, JavaOne, September 2016 mapping(Function mapper,Collector downstream)) LondonStream<Person> mapping() bill jon amy Athens Athens London Set<City>
  • 223. Journey’s End, JavaOne, September 2016 mapping(Function mapper,Collector downstream)) LondonStream<Person> mapping() bill jon amy Athens Athens LondonLondon Set<City>
  • 224. Journey’s End, JavaOne, September 2016 mapping(Function mapper,Collector downstream)) London mapping() bill jon amy Athens Athens LondonLondon Set<City>
  • 225. Journey’s End, JavaOne, September 2016 mapping(Function mapper,Collector downstream)) London mapping() bill jon amy { , } Athens Athens London Set<City>
  • 226. Journey’s End, JavaOne, September 2016 mapping(Function mapper,Collector downstream)) London mapping() bill jon amy { , } Athens Athens London Set<City>
  • 227. Lambda Masterclass, JavaDay.ro, December 2018 mapping(Function mapper,Collector downstream)) Applies a mapping function to each input element before passing it to the downstream collector. Set<City> inhabited = people.stream()
 .collect(mapping(Person::getCity,toSet())); Animation example
  • 228. Lambda Masterclass, JavaDay.ro, December 2018 mapping(Function mapper,Collector downstream)) Applies a mapping function to each input element before passing it to the downstream collector. Set<City> inhabited = people.stream()
 .collect(mapping(Person::getCity,toSet())); Map<City,String> namesByCity = people.stream()
 .collect(groupingBy(Person::getCity, mapping(Person::getName,joining())); Animation example Sensible example
  • 229. Collectors’ Fair, JavaOne, October 2016 groupingBy() bill jon amy Athens London Collector<Person,?,String> Classifier Person→City mapping() Mapper Person→String Stream<String> joining() Collector<CharSequence,?,String> Nesting Collectors
  • 230. Journey’s End, JavaOne, September 2016 Dualling Convenience Reductions Terminal operation Collectors factory method count() counting() max() min() maxBy() minBy() sum() average() summingXxx() averagingXxx() summaryStatistics() summarizingXxx() reduce(accumulator) reduce(identity, accumulator) reduce(identity, accumulator, combiner) reducing(accumulator) reducing(identity, accumulator) reducing(identity, accumulator, combiner)
  • 231. Lambda Masterclass, JavaDay.ro, December 2018 Streams 1I Demo and labs
  • 232. Lambda Masterclass, JavaDay.ro, December 2018 What?You Want More??
  • 233. Lambda Masterclass, JavaDay.ro, December 2018 What?You Want More??