0% found this document useful (0 votes)
11 views

java-8-stream-api-and-collectors-slides

Uploaded by

Chinni Krishna
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

java-8-stream-api-and-collectors-slides

Uploaded by

Chinni Krishna
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 127


List<Person> list = new ArrayList<>() ;


List<Person> list = new ArrayList<>() ;






public interface Stream<T> extends BaseStream<T, Stream<T>> {

// ...
}

public interface Stream<T> extends BaseStream<T, Stream<T>> {

// ...
}
















List<Person> persons = ... ;

Stream<Person> stream = persons.stream();


List<Person> persons = ... ;

Stream<Person> stream = persons.stream();


stream.forEach(p -> System.out.println(p));

List<Person> persons = ... ;

Stream<Person> stream = persons.stream();


stream.forEach(p -> System.out.println(p));


List<Person> persons = ... ;

Stream<Person> stream = persons.stream();


stream.forEach(p -> System.out.println(p));


@FunctionalInterface
public interface Consumer<T> {

void accept(T t);


}

@FunctionalInterface
public interface Consumer<T> {

void accept(T t);


}

Consumer<T> c = p -> System.out.println(p);


@FunctionalInterface
public interface Consumer<T> {

void accept(T t);


}

Consumer<T> c = p -> System.out.println(p);

Consumer<T> c = System.out::println; // Method reference


@FunctionalInterface
public interface Consumer<T> {

void accept(T t);

default Consumer<T> andThen(Consumer<? super T> after) {


Objects.requireNonNull(after);
return (T t) -> { accept(t); after.accept(t); };
}
}

@FunctionalInterface
public interface Consumer<T> {

void accept(T t);

default Consumer<T> andThen(Consumer<? super T> after) {


Objects.requireNonNull(after);
return (T t) -> { accept(t); after.accept(t); };
}
}


List<String> list = new ArrayList<>();

Consumer<String> c1 = s -> list.add(s);


Consumer<String> c2 = s -> System.out.println(s);

List<String> list = new ArrayList<>();

Consumer<String> c1 = list::add;
Consumer<String> c2 = System.out::println;

List<String> list = new ArrayList<>();

Consumer<String> c1 = list::add;
Consumer<String> c2 = System.out::println;

Consumer<String> c3 = c1.andThen(c2);

List<String> result = new ArrayList<>();


List<Person> persons = ...;

Consumer<String> c1 = result::add;
Consumer<String> c2 = System.out::println;

persons.stream()
.forEach(c1.andThen(c2));


List<Person> list = ...;


Stream<Person> stream = list.stream();
Stream<Person> filtered =
stream.filter(person -> person.getAge() > 20);

List<Person> list = ...;


Stream<Person> stream = list.stream();
Stream<Person> filtered =
stream.filter(person -> person.getAge() > 20);

Predicate<Person> p = person -> person.getAge() > 20;


@FunctionalInterface
public interface Predicate<T> {

boolean test(T t);


}

@FunctionalInterface
public interface Predicate<T> {

boolean test(T t);

default Predicate<T> and(Predicate<? super T> other) { ... }

default Predicate<T> or(Predicate<? super T> other) { ... }

default Predicate<T> negate() { ... }


}

Predicate<Integer> p1 = i -> i > 20;


Predicate<Integer> p2 = i -> i < 30;
Predicate<Integer> p3 = i -> i == 0;

Predicate<Integer> p = p1.and(p2).or(p3); // (p1 AND p2) OR p3


Predicate<Integer> p = p3.or(p1).and(p2); // (p3 OR p1) AND p2

Predicate<Integer> p1 = i -> i > 20;


Predicate<Integer> p2 = i -> i < 30;
Predicate<Integer> p3 = i -> i == 0;

Predicate<Integer> p = p1.and(p2).or(p3); // (p1 AND p2) OR p3


Predicate<Integer> p = p3.or(p1).and(p2); // (p3 OR p1) AND p2


@FunctionalInterface
public interface Predicate<T> {

boolean test(T t);

// default methods

static <T> Predicate<T> isEqual(Object o) { ... }


}

@FunctionalInterface
public interface Predicate<T> {

boolean test(T t);

// default methods

static <T> Predicate<T> isEqual(Object o) { ... }


}

Predicate<String> p = Predicate.isEqual("two") ;

Predicate<String> p = Predicate.isEqual("two") ;

Stream<String> stream1 = Stream.of("one", "two", "three") ;

Stream<String> stream2 = stream1.filter(p) ;


Predicate<String> p = Predicate.isEqual("two") ;

Stream<String> stream1 = Stream.of("one", "two", "three") ;

Stream<String> stream2 = stream1.filter(p) ;








List<Person> list = ... ;


Stream<Person> stream = list.stream();
Stream<Person> filtered =
stream.filter(person -> person.getAge() > 20);

List<Person> list = ... ;


Stream<Person> stream = list.stream();
Stream<Person> filtered =
stream.filter(person -> person.getAge() > 20);





List<String> result = new ArrayList<>();


List<Person> persons = ... ;

persons.stream()
.peek(System.out::println)
.filter(person -> person.getAge() > 20)
.peek(result::add);

List<String> result = new ArrayList<>();


List<Person> persons = ... ;

persons.stream()
.peek(System.out::println)
.filter(person -> person.getAge() > 20)
.peek(result::add);


List<String> result = new ArrayList<>();


List<Person> persons = ... ;

persons.stream()
.peek(System.out::println)
.filter(person -> person.getAge() > 20)
.peek(result::add);




List<Person> list = ... ;


Stream<Person> stream = list.stream();
Stream<String> names =
stream.map(person -> person.getName());

List<Person> list = ... ;


Stream<Person> stream = list.stream();
Stream<String> names =
stream.map(person -> person.getName());


@FunctionalInterface
public interface Function<T, R> {

R apply(T t);
}

@FunctionalInterface
public interface Function<T, R> {

R apply(T t);

default <V> Function<V, R> compose(Function<V, T> before);

default <V> Function<T, V> andThen(Function<R, V> after);


}

@FunctionalInterface
public interface Function<T, R> {

R apply(T t);

default <V> Function<V, R> compose(Function<V, T> before);

default <V> Function<T, V> andThen(Function<R, V> after);


}


@FunctionalInterface
public interface Function<T, R> {

R apply(T t);

default <V> Function<V, R> compose(


Function<? super V, ? extends T> before);

default <V> Function<T, V> andThen(


Function<? super R, ? extends V> after);
}

@FunctionalInterface
public interface Function<T, R> {

R apply(T t);

// default methods

static <T> Function<T, T> identity() {


return t -> t;
}
}


<R> Stream<R> flatMap(Function<T, Stream<R>> flatMapper);

<R> Stream<R> map(Function<T, R> mapper);



<R> Stream<R> flatMap(Function<T, Stream<R>> flatMapper);

<R> Stream<R> map(Function<T, R> mapper);



<R> Stream<R> flatMap(Function<T, Stream<R>> flatMapper);

<R> Stream<R> map(Function<T, R> mapper);



<R> Stream<R> flatMap(Function<T, Stream<R>> flatMapper);

<R> Stream<R> map(Function<T, R> mapper);



<R> Stream<R> flatMap(Function<T, Stream<R>> flatMapper);

<R> Stream<R> map(Function<T, R> mapper);



<R> Stream<R> flatMap(Function<T, Stream<R>> flatMapper);

<R> Stream<R> map(Function<T, R> mapper);









List<Integer> ages = ... ;


Stream<Integer> stream = ages.stream();
Integer sum =
stream.reduce(0, (age1, age2) -> age1 + age2);

List<Integer> ages = ... ;


Stream<Integer> stream = ages.stream();
Integer sum =
stream.reduce(0, (age1, age2) -> age1 + age2);


List<Integer> ages = ... ;


Stream<Integer> stream = ages.stream();
Integer sum =
stream.reduce(0, (age1, age2) -> age1 + age2);


@FunctionalInterface
public interface BiFunction<T, U, R> {

R apply(T t, U u);

// plus default methods


}

@FunctionalInterface
public interface BiFunction<T, U, R> {

R apply(T t, U u);

// plus default methods


}

@FunctionalInterface
public interface BinaryOperator<T>
extends BiFunction<T, T, T> {

// T apply(T t1, T t2);

// plus static methods


}





Stream<Integer> stream = ...;


BinaryOperation<Integer> sum = (i1, i2) -> i1 + i2;
Integer id = 0; // identity element for the sum

int red = stream.reduce(id, sum);

Stream<Integer> stream = Stream.empty();

int red = stream.reduce(id, sum);


System.out.println(red);

> 0

Stream<Integer> stream = ...;


BinaryOperation<Integer> sum = (i1, i2) -> i1 + i2;
Integer id = 0; // identity element for the sum

int red = stream.reduce(id, sum);

Stream<Integer> stream = Stream.of(1);

int red = stream.reduce(id, sum);


System.out.println(red);

> 1

Stream<Integer> stream = ...;


BinaryOperation<Integer> sum = (i1, i2) -> i1 + i2;
Integer id = 0; // identity element for the sum

int red = stream.reduce(id, sum);

Stream<Integer> stream = Stream.of(1, 2, 3, 4);

int red = stream.reduce(id, sum);


System.out.println(red);

> 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;


List<Integer> ages = ... ;


Stream<Integer> stream = ages.stream();
... max =
stream.max(Comparator.naturalOrder());

List<Integer> ages = ... ;


Stream<Integer> stream = ages.stream();
... max =
stream.max(Comparator.naturalOrder());


List<Integer> ages = ... ;


Stream<Integer> stream = ages.stream();
... max =
stream.max(Comparator.naturalOrder());


List<Integer> ages = ... ;


Stream<Integer> stream = ages.stream();
Optional<Integer> max =
stream.max(Comparator.naturalOrder());


Optional<String> opt = ... ;


if (opt.isPresent()) {
String s = opt.get() ;
} else {
...
}


Optional<String> opt = ... ;


if (opt.isPresent()) {
String s = opt.get() ;
} else {
...
}


Optional<String> opt = ... ;


if (opt.isPresent()) {
String s = opt.get() ;
} else {
...
}

String s = opt.orElse("") ; // defines a default value


Optional<String> opt = ... ;


if (opt.isPresent()) {
String s = opt.get() ;
} else {
...
}

String s = opt.orElseThrow(MyException::new) ; // lazy construct.












List<Person> persons = ...;

Optional<Integer> minAge =
persons.map(person -> person.getAge()) // Stream<Integer>
.filter(age -> age > 20) // Stream<Integer>
.min(Comparator.naturalOrder()); // terminal operation

List<Person> persons = ... ;

persons.map(person -> person.getLastName())


.allMatch(length < 20); // terminal op.

List<Person> persons = ... ;

persons.map(person -> person.getLastName())


.allMatch(length < 20); // terminal op.






List<Person> persons = ... ;

String result =
persons.stream()
.filter(person -> person.getAge() > 20)
.map(Person::getLastName)
.collect(
Collectors.joining(", ")
);


List<Person> persons = ... ;

List<String> result =
persons.stream()
.filter(person -> person.getAge() > 20)
.map(Person::getLastName)
.collect(
Collectors.toList()
);


List<Person> persons = ... ;

Map<Integer, List<Person>> result =


persons.stream()
.filter(person -> person.getAge() > 20)
.collect(
Collectors.groupingBy(Person::getAge)
);



List<Person> persons = ... ;

Map<Integer, List<Person>> result =


persons.stream()
.filter(person -> person.getAge() > 20)
.collect(
Collectors.groupingBy(Person::getAge)
);


List<Person> persons = ... ;

Map<Integer, Long> result =


persons.stream()
.filter(person -> person.getAge() > 20)
.collect(
Collectors.groupingBy(
Person::getAge,
Collectors.counting() // the downstream collector
)
);








You might also like