Getting started with Java 8 - Part 3

Aditya Agrawal
June 27, 2020

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

.filter(value -> value % 2 == 0)


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 =



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



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 =

.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 =

.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 =

.filter(person -> "london".equalsIgnoreCase(person.isFrom()))


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 =

.reduce((v1, v2) -> v1 + v2)


6) collect -It is a terminal operation that is used to collect stream data as a collection. toList ->List result =

.map(value -> value * 2)


toSet ->Set result =

.map(value -> value / 2)


toMap ->Map<String, String> result =

.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 =


Using other Collection ->LinkedHashSet result =


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) {


.flatMap(album -> album.getTracks())

.filter(track -> track.getLength() > 60)

.map(track -> track.getName())



‍By:Aditya Agrawal

About Quinbay

Quinbay is a dynamic one stop technology company driven by the passion to disrupt technology today and define the future.
We private label and create digital future tech platforms for you.

Digitized . Automated . Intelligent