Toggle speaker view by pressing P
Pressing C clones the slideshow view, which is the view to put on the projector if you're using speaker view.
Press ? to toggle keyboard commands help.
Toggle speaker view by pressing P
Pressing C clones the slideshow view, which is the view to put on the projector if you're using speaker view.
Press ? to toggle keyboard commands help.
Functions can be
Functions can be
Functions can be
Button button = new Button("Run");button.setOnAction(event -> run(event));
Button button = new Button("Run");button.setOnAction(event -> run(event));
run(event)
is a function that is passed to a methodButton button = new Button("Run");button.setOnAction(event -> run(event));
run(event)
is a function that is passed to a methodButton button = new Button("Run");button.setOnAction(event -> run(event));
run(event)
is a function that is passed to a methodButton button = new Button("Run");button.setOnAction(new EventHandler<ActionEvent>() { @Override private void handle(ActionEvent event) { run(event); }});
@FunctionalInterfacepublic interface EventHandler<T> { void handle(T event);}
@FunctionalInterfacepublic interface EventHandler<T> { void handle(T event);}
@FunctionalInterface
@FunctionalInterfacepublic interface EventHandler<T> { void handle(T event);}
@FunctionalInterface
@FunctionalInterfacepublic interface EventHandler<T> { void handle(T event);}
@FunctionalInterface
EventHandler<ActionEvent> handler = event -> run(event);
Collection
and Stream
InterfacesCollection
Collection
and Stream
InterfacesCollection
Stream
Collection
and Stream
InterfacesCollection
Stream
Stream
Methodscount()
- returns the number of elements in the streamdistinct()
- returns a stream with duplicates removedlimit(long maxSize)
- returns a stream with no more than maxSize
elementsskip(long count)
- returns a stream without the first count
elementssorted()
- returns a stream in sorted ordercount()
and distinct()
List<String> words = Arrays.asList("a", "function", "that", "returns", "a", "function");long count = words.stream() .count();long numberUnique = words.stream() .distinct() .count();System.out.println("Count: " + count + " Unique: " + numberUnique);
count()
and distinct()
List<String> words = Arrays.asList("a", "function", "that", "returns", "a", "function");long count = words.stream() .count();long numberUnique = words.stream() .distinct() .count();System.out.println("Count: " + count + " Unique: " + numberUnique);
Count: 6 Unique: 4
setOnAction(EventHandler<ActionEvent> e)
Stream
allMatch(Predicate<? super T> predicate)
anyMatch(Predicate<? super T> predicate)
noneMatch(Predicate<? super T> predicate)
filter(Predicate<? super T> predicate)
Predicate
Interface@FunctionalInterfacepublic interface Predicate<T> { boolean test(T t);}
∗Not showing default
methods
Stream
... againallMatch(Predicate<? super T> predicate)
anyMatch(Predicate<? super T> predicate)
noneMatch(Predicate<? super T> predicate)
filter(Predicate<? super T> predicate)
filter()
List<String> words = Arrays.asList("a", "function", "that", "returns", "a", "function");Predicate<String> isShort = (word -> word!=null && word.length()<5);Object[] shortWords = words.stream() .filter(isShort) .toArray();for(Object word : shortWords) { System.out.print(word + " ");}System.out.println();
filter()
List<String> words = Arrays.asList("a", "function", "that", "returns", "a", "function");Predicate<String> isShort = (word -> word!=null && word.length()<5);Object[] shortWords = words.stream() .filter(isShort) .toArray();for(Object word : shortWords) { System.out.print(word + " ");}System.out.println();
a that a
Stream
map(Function<? super T, ? extends R> mapper)
mapToDouble(Function<? super T> mapper)
mapToInt(Function<? super T> mapper)
Function
Interface@FunctionalInterfacepublic interface Function<T, R> { R apply(T t);}
∗Not showing default
methods
map()
List<String> words = Arrays.asList("a", "function", "that", "returns", "a", "function");List<String> lengths = words.stream() .map(word -> word + ": " + word.length()) .collect(Collectors.toList());System.out.println(lengths);
map()
List<String> words = Arrays.asList("a", "function", "that", "returns", "a", "function");List<String> lengths = words.stream() .map(word -> word + ": " + word.length()) .collect(Collectors.toList());System.out.println(lengths);
Function<String, String> toLength = (word -> word + ": " + word.length());words.stream().map(toLength);
map()
List<String> words = Arrays.asList("a", "function", "that", "returns", "a", "function");List<String> lengths = words.stream() .map(word -> word + ": " + word.length()) .collect(Collectors.toList());System.out.println(lengths);
Function<String, String> toLength = (word -> word + ": " + word.length());words.stream().map(toLength);
[a: 1, function: 8, that: 4, returns: 7, a: 1, function: 8]
IntStream
, DoubleStream
, and LongStream
sum()
max()
min()
average()
summaryStatistics()
reduce()
mapToInt()
int sum = words.stream() .mapToInt(w -> w.length()); .sum();
mapToInt()
int sum = words.stream() .mapToInt(w -> w.length()); .sum();
Sum of lengths of all strings: 29
Consumer
Interface@FunctionalInterfacepublic interface Consumer<T> { void accept(T t);}
∗Not showing default
methods
forEach()
List<String> words = Arrays.asList("a", "function", "that", "returns", "a", "function");words.stream() .forEach(w -> System.out.println(w + ": " + w.length()));
forEach()
List<String> words = Arrays.asList("a", "function", "that", "returns", "a", "function");words.stream() .forEach(w -> System.out.println(w + ": " + w.length()));
a: 1function: 8that: 4returns: 7a: 1function: 8
Note: we can do forEach()
directly on a List
:
words.forEach(w -> System.out.println(w + ": " + w.length()));
Stream<Integer> numbers = Stream.of(3, 8, 7, 2, 1, 9, 8);List<Integer> evens = numbers.filter(i -> i%2==0) .collect(Collectors.toList());System.out.println(evens);
Stream<Integer> numbers = Stream.of(3, 8, 7, 2, 1, 9, 8);List<Integer> evens = numbers.filter(i -> i%2==0) .collect(Collectors.toList());System.out.println(evens);
[8, 2, 8]
System.out.println(Stream.of("happy", "discussion" /* ... */, "locomotion") .filter(word -> word.endsWith("tion")) .count());
System.out.println(Stream.of("happy", "discussion" /* ... */, "locomotion") .filter(word -> word.endsWith("tion")) .count());
1
Stream<Shape> shapes = Stream.of(new Circle(0, 0), new Square(0, 0, 4), new Circle(0, 0), new Triangle(0, 0, 1, 5));List<Circle> circles = shapes.filter(shape -> shape instanceof Circle) .map(shape -> (Circle)shape) .collect(Collectors.toList());
double totalArea = shapes.mapToDouble(shape -> shape.getArea()) .sum();System.out.println("Total area: " + totalArea);
double totalArea = shapes.mapToDouble(shape -> shape.getArea()) .sum();System.out.println("Total area: " + totalArea);
Total area: 38.239208802178716
OptionalDouble averageArea = shapes.mapToDouble(Shape::getArea) .average();if(averageArea.isPresent()) { System.out.println("Average area: " + averageArea.getAsDouble());}
OptionalDouble averageArea = shapes.mapToDouble(Shape::getArea) .average();if(averageArea.isPresent()) { System.out.println("Average area: " + averageArea.getAsDouble());}
Average area: 9.559802200544679
DoubleSummaryStatistics areaStats = shapes.mapToDouble(Shape::getArea) .summaryStatistics();System.out.println("Number of shapes: " + areaStats.getCount());System.out.println("Total area: " + areaStats.getSum());System.out.println("Average area: " + areaStats.getAverage());System.out.println("Maximum area: " + areaStats.getMax());System.out.println("Minimum area: " + areaStats.getMin());
DoubleSummaryStatistics areaStats = shapes.mapToDouble(Shape::getArea) .summaryStatistics();System.out.println("Number of shapes: " + areaStats.getCount());System.out.println("Total area: " + areaStats.getSum());System.out.println("Average area: " + areaStats.getAverage());System.out.println("Maximum area: " + areaStats.getMax());System.out.println("Minimum area: " + areaStats.getMin());
Number of shapes: 4Total area: 38.239208802178716Average area: 9.559802200544679Maximum area: 16.0Minimum area: 2.5
reduce()
double area = shapes.map(Shape::getArea) .reduce(0.0, (a, b) -> a+b);System.out.println("Total area: " + area);
reduce()
double area = shapes.map(Shape::getArea) .reduce(0.0, (a, b) -> a+b);System.out.println("Total area: " + area);
Total area: 38.239208802178716
Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
Esc | Back to slideshow |