Click here to read about Getting started with Java 8- Part 2
Streams :-A java.util.Stream represents a sequence of elements on which one or more operations can be performed.For Example let's say we have a series of number in a List.List integerList = Arrays.asList(1, 2, 4, 8, 10, 13, 15);Collections in Java 8 are extended so you can simply create streams either by calling Collection.stream() or Collection.parallelStream().The following sections explain the most common stream operations:-
1) Filter :-Anytime you are looping and check for a condition for each element in the list, you might want to think about using filter method on stream. Filter accepts a predicate to filter all elements of the stream. Let's say you want to separate out even numbers from the above list.List result = integerList.stream()
.filter(value -> value % 2 == 0)
.collect(Collectors.toList());
2) Map :-If you’ve got a function that converts a value of one type into another, map lets you apply this function to a stream of values,producing another stream of the new values. Map returns a stream consisting of the results of applying the given function to the elements of this stream.Let's say you want to convert each element in the list to uppercase.List result = names.stream()
.map(String::toUpperCase)
.collect(Collectors.toList());
3) FlatMap :-FlatMap lets you replace a value with a Stream and concatenates all the streams together.List together = Stream.of(asList(1, 2), asList(3, 4))
.flatMap(Collection::stream)
.collect(toList());
The above code will return a list of Integer containing 1, 2, 3, 4.3) Match :-Various matching operations can be used to check whether a certain predicate matches the stream. All of those operations are terminal and return a boolean result. anyMatch - Returns true if any elements of the stream matches the provided condition.boolean result = integerList.stream()
.anyMatch(value -> 2 == value);
allMatch - Returns true only if all elements of this stream match the provided condition.boolean result = integerList.steam()
.allMatch(value -> value % 2 == 0);
noneMatch - Returns true when no elements of this stream match the provided condition.boolean result = integerList.stream()
.noneMatch(value -> value == 20)
4) Count :-Count is a terminal operation that returns the number of elements in the stream as a long. Let's say you want to find the number of people from London.long fromLondon = persons.stream()
.filter(person -> "london".equalsIgnoreCase(person.isFrom()))
.count();
5) Reduce :-This terminal operation performs a reduction on the elements of the stream with the given function. The result is an Optional holding the reduced value.</span To add all the numbers.Integer result = integerList.stream()
.reduce((v1, v2) -> v1 + v2)
.get();
6) collect -It is a terminal operation that is used to collect stream data as a collection. toList ->List result = integerList.stream()
.map(value -> value * 2)
.collect(Collectors.toList());
toSet ->Set result = integerList.stream()
.map(value -> value / 2)
.collect(Collectors.toSet());
toMap ->Map<String, String> result = people.stream()
.collect(Collectors.toMap(p1 -> p1.getName(), p2 -> p2 -> p2.isFrom()));
This will return each map of name to city for each person.groupingBy ->Groups the data according to the given criteria. The below code will group all the Employees in same department together.Map<Department, List> byDept = employees.stream()
.collect(Collectors.groupingBy(Employee::getDepartment));
Using other Collection ->LinkedHashSet result = integerList.stream()
.collect(Collectors.toCollection(LinkedHashSet::new));
Now Let's see how we can put different stream operations together ->finding names of tracks over a minute in length.public Set findLongTracks(List albums) {
return albums.stream()
.flatMap(album -> album.getTracks())
.filter(track -> track.getLength() > 60)
.map(track -> track.getName())
.collect(toSet());
}
By:Aditya Agrawal