simplifying java with lambdas (short)

54
Simplifying with Lambdas Richard Warburton

Upload: richardwarburton

Post on 10-May-2015

711 views

Category:

Technology


5 download

DESCRIPTION

There's a revolution calling! Lambda expressions are coming in Java 8 but how can developers benefit? We'll go through a series of code examples, that show how to: Use the new lambda expressions feature Write more readable and faster collections processing code using the Streams API Build complex data processing systems with the new collector abstraction Use lambda expressions in your own code

TRANSCRIPT

Page 1: Simplifying java with lambdas (short)

Simplifying with LambdasRichard Warburton

Page 2: Simplifying java with lambdas (short)
Page 3: Simplifying java with lambdas (short)

What do you mean simple?

Streams

Collectors

Conclusion

Page 4: Simplifying java with lambdas (short)

Lambda Expressions are here in Java 8!

Page 5: Simplifying java with lambdas (short)
Page 6: Simplifying java with lambdas (short)
Page 7: Simplifying java with lambdas (short)

What can we simplify?

Page 8: Simplifying java with lambdas (short)

Writing

Page 9: Simplifying java with lambdas (short)

Reading

Page 10: Simplifying java with lambdas (short)

Change

Page 11: Simplifying java with lambdas (short)

Better Libraries through Code as Data

Page 12: Simplifying java with lambdas (short)

button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { System.out.println("button clicked"); }});

Page 13: Simplifying java with lambdas (short)

button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { System.out.println("button clicked"); }});

Page 14: Simplifying java with lambdas (short)

button.addActionListener(?);

Page 15: Simplifying java with lambdas (short)

button.addActionListener(event -> ?);

Page 16: Simplifying java with lambdas (short)

button.addActionListener(event ->System.out.println("button clicked")

);

Page 17: Simplifying java with lambdas (short)

String name = getName();Runnable runnable =

() -> System.out.println(“Hello “ + name);

Page 18: Simplifying java with lambdas (short)

FUNCTIONAL INTERFACES

Everything in Java has a type

Problem: Need a type to represent a method

Solution: Use interfaces with a single method

Page 19: Simplifying java with lambdas (short)
Page 20: Simplifying java with lambdas (short)

public interface ActionListener { public void actionPerformed(ActionEvent event);}

Page 21: Simplifying java with lambdas (short)

INFERENCE

button.addActionListener(event ->System.out.println("button clicked")

);

public interface ActionListener { public void actionPerformed(ActionEvent event);}

Page 22: Simplifying java with lambdas (short)

What do you mean simple?

Streams

Collectors

Conclusion

Page 23: Simplifying java with lambdas (short)

STREAMS

Support automated data parallelism

Abstraction to build computation pipelines

Iterator with inversion of control

Page 24: Simplifying java with lambdas (short)

int count = 0;for (Artist artist : artists) {

if (artist.isFrom("London")) {count++;

}}

EXTERNAL ITERATION

Page 25: Simplifying java with lambdas (short)

artists.stream()

.filter(artist -> artist.isFrom("London"))

.count();

INTERNAL ITERATION

Page 26: Simplifying java with lambdas (short)
Page 27: Simplifying java with lambdas (short)

List<String> collected = Stream.of("a", "b", "hello") .map(item -> item.toUpperCase()) .collect(toList());

assertEquals(asList("A", "B", "HELLO"),collected);

Page 28: Simplifying java with lambdas (short)
Page 29: Simplifying java with lambdas (short)

List<String> beginningWithNumbers =

Stream.of("a", "1abc", "abc1")

.filter(value -> isDigit(value.charAt(0)))

.collect(toList());

assertEquals(

asList("1abc"),

beginningWithNumbers);

Page 30: Simplifying java with lambdas (short)
Page 31: Simplifying java with lambdas (short)

int sum = Stream.of(1, 2, 3, 4) .reduce(0, (acc, x) -> acc + x);

assertEquals(10, sum);

Page 32: Simplifying java with lambdas (short)

Putting it Together

for a given an album, find the nationality of every band playing on that album

Page 33: Simplifying java with lambdas (short)

Putting it Together

1. get all the artists for an album,

2. figure out which artists are bands,

3. find the nationalities of each band

4. put together a list of these values.

Page 34: Simplifying java with lambdas (short)

Putting it Together

Set<String> origins =

album.getMusicians()

.filter(artist -> artist.getName().startsWith("The"))

.map(artist -> artist.getNationality())

.collect(toSet());

Page 35: Simplifying java with lambdas (short)

Eager vs Lazy

Set<String> origins =

album.getMusicians()

.filter(artist -> artist.getName().startsWith("The"))

.map(artist -> artist.getNationality())

// What’s happened at this point?

.collect(toSet());

Page 36: Simplifying java with lambdas (short)

Parallelism

Parallelism no longer means a rewrite of your code

Streams support parallelism out of the box

call .parallelStream() instead of .stream()

Performs well in the right circumstances, but not a panacea

Page 37: Simplifying java with lambdas (short)

What do you mean simple?

Streams

Collectors

Conclusion

Page 38: Simplifying java with lambdas (short)

Enter the Collector

Collection is mutable reduction

Generic API for building up final values

Already seen collect(toList())

Page 39: Simplifying java with lambdas (short)

import static java.util.stream.Collectors.*;

collect(toList());

collect(toSet());

collect(toCollection(

() -> new TreeSet<>()));

Page 40: Simplifying java with lambdas (short)
Page 41: Simplifying java with lambdas (short)

Map<Boolean, List<Artist>> bandsAndSolo =

artists.collect(partitioningBy(a -> a.isSolo()));

Page 42: Simplifying java with lambdas (short)
Page 43: Simplifying java with lambdas (short)

Map<Artist, List<Album>> albumsByArtist =

albums.collect(

groupingBy(alb -> alb.getMainMusician()));

Page 44: Simplifying java with lambdas (short)

Map<Artist, Long> albumsByArtist =

albums.collect(

groupingBy(alb -> alb.getMainMusician(),

counting()));

Page 45: Simplifying java with lambdas (short)

Recap of Collectors

Mutable Reduction

A custom collector is just implementing an interface

Can collect other valuesmin, maxaverage, sumsummary statisticsjoin together strings

Page 46: Simplifying java with lambdas (short)

What do you mean simple?

Streams

Collectors

Design Patterns

Conclusion

Page 47: Simplifying java with lambdas (short)

How did we simplify?

Page 48: Simplifying java with lambdas (short)

Behavioural abstraction

A method/class which is parameterized by different behaviours.

Page 49: Simplifying java with lambdas (short)

High Order Functions

Functions which take other functions as arguments or return functions.

Page 50: Simplifying java with lambdas (short)

Writing

Avoided repeating basic looping and collections constructs.

Page 51: Simplifying java with lambdas (short)

Reading

Words like map or reduce provide immediate understanding.

Page 52: Simplifying java with lambdas (short)

Changing

Applying the same techniques in your own code reaps huge rewards.

Page 53: Simplifying java with lambdas (short)
Page 54: Simplifying java with lambdas (short)

Q & [email protected]/java8lambdas