Java 8 Stream API and Collectors
What is Stream?
a typed interface which gives ways to efficiently process large amounts of data
A stream is an object on which one can define operations like map/filter/reduce.
It is also an object that does not hold any data.
An object that should not change the data it processes.
How can we build streams?
Listpersons = ...; Stream stream = persons.stream(); stream.forEach(p -> System.out.println(p));
Example of one more operation filter in java 8:
Listpersons = ...; Stream stream = persons.stream(); Stream filtered = stream.filter( person -> person.getAge() > 30);
Example of stream usage:
import java.util.function.Predicate;
import java.util.stream.Stream;
public class FirstPredicates {
public static void main(String[] args) {
Stream stream = Stream.of("one","two","three","four","five");
stream.forEach(s -> System.out.println(s));
// using predicate
Predicate p1 = s -> s.length()>3;
stream
.filter(p1)
.forEach( s -> System.out.println(s));
// using predicate or
Predicate p2 = Predicate.isEqual("two");
Predicate p3 = Predicate.isEqual("three");
stream
.filter(p2.or(p3))
.forEach( s -> System.out.println(s));
}
}
Output:
one
two
three
four
five
three
four
five
two
three
Stream API defines intermediary operations
forEach()
filter(Predicate)
peek()
Mapping operation: map returns a stream, so it is intermediary operation.
Example:
Listpersons = ...; Stream stream = persons.stream(); Stream names = stream .map( p -> p.getName());
flatMap example:
class FlatMapExample { public static void main(String[] args) { Listlist1 = Arrays.asList(1,2,3); List list2 = Arrays.asList(4,5,6,7); List list3 = Arrays.asList(8,9,10,11,12); List > list = Arrays.asList(list1,list2,list3); System.out.println(list); Function
,Stream
> flapMapper = l -> l.stream(); list .stream() .flatMap(flatMapper) .forEach(System.out::println); } }
Optional means there might be no result as well.
Example:
Listages = ...; Stream stream = ages.stream(); Optional max = stream.max(Comparator.naturalOrder());
Reductions available:
- max(),min(), count()
Boolean reductions
- allMatch(), noneMatch(), anyMatch()
Reductions that return optional
- findFirst(), findAny()
Terminal Operation : Example
Listpersons = ...; Optional minAge = persons.stream() .map(person -> person.getAge()) .filter( s -> s < 20) .min(Comparator.naturalOrder());
There is another type of reduction called Collectors
Collecting to string Example: Result is string with all names of people with age > 20, separated by comma
Listpersons = ...; String result = persons.stream() .filter(person -> person.getAge() > 20) .map(Person::getLastName) .collect( Collectors.joining(", ") ); Map > result = persons.stream() .filter(person -> person.getAge() > 20) .collect( Collectors.groupingBy(Person::getAge) ); Map result = persons.stream() .filter(person -> person.getAge() > 20) .collect( Collectors.groupingBy( Person::getAge, Collectors.counting() // it will count persons of same age ) );
No comments:
Post a Comment