0% found this document useful (0 votes)
266 views

Stream API

The document discusses Java 8 streams, which provide a way to process sequences of elements from collections, arrays, or I/O sources. Streams are lazy sequences that support intermediate operations like filter and map, as well as terminal operations like forEach and collect. Intermediate operations are lazy and are only executed when a terminal operation is called to produce a non-stream result.

Uploaded by

NIKHIL B S
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
266 views

Stream API

The document discusses Java 8 streams, which provide a way to process sequences of elements from collections, arrays, or I/O sources. Streams are lazy sequences that support intermediate operations like filter and map, as well as terminal operations like forEach and collect. Intermediate operations are lazy and are only executed when a terminal operation is called to produce a non-stream result.

Uploaded by

NIKHIL B S
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Stream API

One of the major new features in Java 8 is the introduction of the stream
functionality – java.util.stream – which contains classes for processing sequences of
elements.
The central API class is the Stream<T>. The following section will demonstrate how
streams can be created using the existing data-provider sources.

Introduced in Java 8, the Stream API is used to process collections of objects. A


stream is a sequence of objects that supports various methods which can be
pipelined to produce the desired result.
The features of Java stream are –

 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, they only provide the result
as per the pipelined methods.
 Each intermediate operation is lazily executed and returns a stream as a
result, hence various intermediate operations can be pipelined. Terminal
operations mark the end of the stream and return the result.

Intermediate operations return another Stream which allows you to call multiple
operations in a form of a query. Intermediate operations do not get executed until a
terminal operation is invoked as there is a possibility they could be processed
together when a terminal operation is executed.

Terminal operations produces a non-stream, result such as primitive value, a


collection or no value at all. Terminal operations are typically preceded by
intermediate operations which return another Stream which allows operations to be
connected in a form of a query.

The distinction between these operations is that an intermediate operation is lazy


while a terminal operation is not. When you invoke an intermediate operation on a
stream, the operation is not executed immediately. It is executed only when a
terminal operation is invoked on that stream. In a way, an intermediate operation is
memorized and is recalled as soon as a terminal operation is invoked. You can chain
multiple intermediate operations and none of them will do anything until you invoke
a terminal operation. At that time, all of the intermediate operations that you
invoked earlier will be invoked along with the terminal operation.
NOTE: Intermediate operations are lazy. This means that they will be invoked only
if it is necessary for the terminal operation execution.

Intermediate Operations:
(i) filter: The filter method is used to select elements as per the Predicate
passed as argument.
(ii) map: The map method is used to map the items in the collection to other
objects according to the Predicate passed as argument.
(iii) sorted: The sorted method is used to sort the stream.

Terminal Operations:
(i) collect: The collect method is used to return the result of the intermediate
operations performed on the stream.
(ii) forEach: The forEach method is used to iterate through every element of
the stream.
(iii) reduce: The reduce method is used to reduce the elements of a stream to
a single value.

Stream Creation

There are many ways to create a stream instance of different sources. Once created,
the instance will not modify its source, therefore allowing the creation of multiple
instances from a single source.

(i) Empty Stream: The empty() method should be used in case of a creation of
an empty stream

Stream<String> streamEmpty = Stream.empty();

(ii) Stream of Collection: Stream can also be created of any type of Collection
(Collection, List, Set)

Collection<String> collection = Arrays.asList("a", "b", "c");


Stream<String> streamOfCollection = collection.stream();

(iii) Stream of Array: Array can also be a source of a Stream

String[] array = {"C","A","h","f"};


Stream<String> strmOfArr = Stream.of(array);

Coding Examples:

You might also like