java-8-stream-api-and-collectors-slides
java-8-stream-api-and-collectors-slides
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
// ...
}
▪
// ...
}
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
@FunctionalInterface
public interface Consumer<T> {
@FunctionalInterface
public interface Consumer<T> {
@FunctionalInterface
public interface Consumer<T> {
@FunctionalInterface
public interface Consumer<T> {
@FunctionalInterface
public interface Consumer<T> {
▪
▪
Consumer<String> c1 = list::add;
Consumer<String> c2 = System.out::println;
▪
Consumer<String> c1 = list::add;
Consumer<String> c2 = System.out::println;
Consumer<String> c3 = c1.andThen(c2);
▪
Consumer<String> c1 = result::add;
Consumer<String> c2 = System.out::println;
persons.stream()
.forEach(c1.andThen(c2));
▪
▪
@FunctionalInterface
public interface Predicate<T> {
@FunctionalInterface
public interface Predicate<T> {
▪
▪
@FunctionalInterface
public interface Predicate<T> {
// default methods
@FunctionalInterface
public interface Predicate<T> {
// default methods
Predicate<String> p = Predicate.isEqual("two") ;
▪
Predicate<String> p = Predicate.isEqual("two") ;
▪
▪
Predicate<String> p = Predicate.isEqual("two") ;
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
persons.stream()
.peek(System.out::println)
.filter(person -> person.getAge() > 20)
.peek(result::add);
▪
persons.stream()
.peek(System.out::println)
.filter(person -> person.getAge() > 20)
.peek(result::add);
▪
▪
persons.stream()
.peek(System.out::println)
.filter(person -> person.getAge() > 20)
.peek(result::add);
▪
▪
▪
▪
▪
▪
▪
▪
@FunctionalInterface
public interface Function<T, R> {
R apply(T t);
}
▪
@FunctionalInterface
public interface Function<T, R> {
R apply(T t);
@FunctionalInterface
public interface Function<T, R> {
R apply(T t);
▪
▪
@FunctionalInterface
public interface Function<T, R> {
R apply(T t);
@FunctionalInterface
public interface Function<T, R> {
R apply(T t);
// default methods
▪
<R> Stream<R> flatMap(Function<T, Stream<R>> flatMapper);
▪
<R> Stream<R> flatMap(Function<T, Stream<R>> flatMapper);
▪
▪
▪
<R> Stream<R> flatMap(Function<T, Stream<R>> flatMapper);
▪
▪
▪
<R> Stream<R> flatMap(Function<T, Stream<R>> flatMapper);
▪
▪
▪
<R> Stream<R> flatMap(Function<T, Stream<R>> flatMapper);
▪
▪
▪
<R> Stream<R> flatMap(Function<T, Stream<R>> flatMapper);
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
@FunctionalInterface
public interface BiFunction<T, U, R> {
R apply(T t, U u);
@FunctionalInterface
public interface BiFunction<T, U, R> {
R apply(T t, U u);
@FunctionalInterface
public interface BinaryOperator<T>
extends BiFunction<T, T, T> {
▪
▪
▪
▪
▪
▪
▪
▪
> 0
▪
> 1
▪
> 10
▪
BinaryOperation<Integer> max =
(i1, i2) ->
i1 > i2 ? i1 : i2;
▪
BinaryOperation<Integer> max =
(i1, i2) ->
i1 > i2 ? i1 : i2;
▪
▪
BinaryOperation<Integer> max =
(i1, i2) ->
i1 > i2 ? i1 : i2;
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
Optional<Integer> minAge =
persons.map(person -> person.getAge()) // Stream<Integer>
.filter(age -> age > 20) // Stream<Integer>
.min(Comparator.naturalOrder()); // terminal operation
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
String result =
persons.stream()
.filter(person -> person.getAge() > 20)
.map(Person::getLastName)
.collect(
Collectors.joining(", ")
);
▪
▪
List<String> result =
persons.stream()
.filter(person -> person.getAge() > 20)
.map(Person::getLastName)
.collect(
Collectors.toList()
);
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪