Stream API in Java

Java Stream API is used to process collections of an object in a functional way. Stream API provides methods that can be pipelined to get the required result. The Stream is all about describing computations on data. It is not like Collections which is used to store and access data. Java Stream API is added in Java 8 Version with other functional interfaces. In this tutorial, we will explain the use of the Stream API.

A stream pipeline consists of three types of things.

a.) Stream Source b.) Zero or more intermediate operations c.)  Terminal Operation

Stream Source

From collections and arrays

  1.  Collection.stream()
  2. Collection.parallelStream() 
  3. Arrays.stream(T array) or Stream.of()

From Static factories

  1. IntStream.range()
  2. Files.walk()

Stream Operations

Stream operations can execute sequentially or in parallel. There are two types of operations available in Java Stream.

Intermediate Operation

Intermediate operations can be merged

filter:  filter method is used to filter a collection of an object based on some the predicate passed as an argument.

List names = Arrays.asList("Java","Python","PHP");
List filteredList = names.stream().filter(s->s.startsWith("P")).collect(Collectors.toList());

 map: The map method converts the collection of the object according to the method passed as an argument into it.

List lowerCaseList = Arrays.asList("java","python","php");
List toUpperCaseList = lowerCaseList.stream().map(String::toUpperCase)
.collect(Collectors.toList());

sorted: sorted method is used to sort the collection of the object in default order or based on the method passed as an argument into it.

 List unsortedList = Arrays.asList("python","php","java");
 List sortedList = unsortedList.stream().sorted().collect(Collectors.toList());

limit: Truncates a stream to a certain number passed as a parameter into it.

List list = Arrays.asList("python","php","java");
List s = list.stream().sorted().limit(1).collect(Collectors.toList());

Terminal Operation

Terminal operations are used to produce a final result from a stream pipeline. A result is any nonstream value such as a List, an Integer, or even void. The Stream pipeline is only evaluated when the terminal operation is called. These are some terminal operations normally used with Stream of collections.

collect: The collect method is used to collect the result of the intermediate operations and convert and return it based on the method passed in it from java.util.stream.Collectors class.

List list = Arrays.asList("java","python","php");
Set set = list.stream().map(String::toUpperCase).collect(Collectors.toSet());

forEach: forEach method is used to iterate over the stream of collection's objects.

 List list = Arrays.asList("python","php","java");
 list.stream().sorted().forEach(System.out::println);

reduce: It is used to convert the stream of elements into one single value.

Stream.of(3,2,6,5,8,6).reduce((x,y)->x+y).ifPresent(System.out::println);

How to identify if a method performs the intermediate operation or a terminal operation?

List list = Arrays.asList("python","php","java","groovy","cobol");
long count = list.stream().filter(s -> s.length()>4).distinct().limit(2).count();

count() operation in the stream pipeline returning a long value, as it is nonstream value so that count is performing a terminal operation. All previous operations, filter, distinct, limit, are connected and return a Stream. They are therefore intermediate operations.

Summary:

1. A stream is a sequence of elements from a source that supports data processing operations.

2. Streams make use of internal iteration - the iteration is abstracted away through operations such as
filter, map, and sorted.

3. There are two types of stream operations - intermediate and terminal operations.

4. Intermediate operations such as filter and map return a stream and can be chained together. They’re
used to set up a pipeline of operations but don’t produce any result.

5. Terminal operations such as forEach and count return a nonstream value and process a stream
pipeline to return a result.

6. The elements of a stream are computed on demand.