What are Streams API in Java 8 ? How its unique

Reading Time: 3 minutes

First of all, Java 8 Streams should not be confuses with Java I/O streams. The Stream API is use to process collections of objects. Java provides a new additional package in Java 8 called java.util.stream. In stream one or more operations can be perform. The Streams operations are either intermediate or terminal.

Stream provides following features:

  • The stream does not store elements.
  • A stream is not a data structure instead it takes input from the Collections, Arrays or I/O channels.
  • Streams don’t change the original data structure.
  • Stream is lazy and evaluates code only when required.
  • The elements of a stream are only visited once during the life of a stream.

Stream Operations

Streams abstraction has a long list of useful functions. They are divided into intermediate operations (return Stream<T>) and terminal operations (return a result of definite type). Let us have a look on some of the stream operations.

Streams Intermediate Operations

Intermediate operations return the stream itself so you can chain multiple methods calls in a row.

Filter

The filter() method accepts a Predicate to filter all elements of the stream.

Example :

List<String> names = Arrays.asList("Java","Rust","DevOps");
List<String> result = names.stream().filter(s -> s.startsWith("J"))
                .collect(Collectors.toList());
result.forEach(System.out::println);

Output :

Map

The map() method is used to returns a stream consisting of the results of applying the given function to the elements of this stream.

Example :

List<String> number = Arrays.asList(2,3,4,5);
List<String> square = number.stream().map(x->x*x).collect(Collectors.toList());
square.forEach(System.out::println);

Output :

Sorted

The sorted() method is an intermediate operation that returns a sorted view of the stream. 

Example :

List<String> names = Arrays.asList("Java","Rust","DevOps");
List<String> result = names.stream().sorted().collect(Collectors.toList());
result.forEach(System.out::println);

Output :

Streams Terminal Operations

Terminal operations return a result of a certain type after processing all the stream elements.

Collect

The collect() method is used to receive elements from a steam and store them in a collection.

Example :

List<Integer> number = Arrays.asList(2,3,4,5,3);
Set<Integer> square = number.stream().map(x->x*x).collect(Collectors.toSet());
square.forEach(System.out::println);

Output :

forEach

The forEach() method is used to iterate through every element of the stream.

Example :

List<Integer> number = Arrays.asList(2,3,4,5);
number.stream().map(x->x*x).forEach(y->System.out.println(y));

Output :

Reduce

The reduce() method takes a sequence of input elements and combines them into a single summary result by repeated operation.

Example :

List<Integer> number = Arrays.asList(2,3,4,5);
int even = number.stream().filter(x->x%2==0).reduce(0, Integer::sum);
System.out.println(even);

Output :

Thanks for Reading !!


Knoldus-blog-footer-image