Monday, October 17, 2016

Streams Introduction

Streams:
--------
java.util.stream package (Streams),which enables the concise and declarative expression of possibly-parallel bulk operations on various data sources.
All stream computations share a common structure: They have a stream source, zero or moreintermediate operations, and a single terminal operation.
Stream source: Streams can create following ways
Method
Description
Collection.stream()
Create a stream from the elements of a collection.
Stream.of(T...)
Create a stream from the arguments passed to the factory method.
Stream.of(T[])
Create a stream from the elements of an array.
Stream.empty()
Create an empty stream.
Stream.iterate(T first, BinaryOperator<T> f)
Create an infinite stream consisting of the sequence first,
f(first), f(f(first)), ...
Stream.iterate(T first, Predicate<T> test,
BinaryOperator<T> f)
(Java 9 only) Similar to Stream.iterate(T first,
BinaryOperator<T> f), except the stream terminates on the first
elements for which the test predicate returns false.
Stream.generate(Supplier<T> f)
Create an infinite stream from a generator function.
IntStream.range(lower, upper)
Create an IntStream consisting of the elements from lower to upper,
exclusive.
IntStream.rangeClosed(lower, upper)
Create an IntStream consisting of the elements from lower to upper,
inclusive.
BufferedReader.lines()
Create a stream consisting of the lines from a BufferedReader.
BitSet.stream()
Create an IntStream consisting of the indexes of the set bits in a
BitSet.
CharSequence.chars()
Create an IntStream corresponding to the chars in a String.

Intermediate operations :-

Operation
Contents
filter(Predicate<T>)
The elements of the stream matching the predicate
map(Function<T, U>)
The result of applying the provided function to the elements of the
stream
flatMap(Function<T, Stream<U>>
The elements of the streams resulting from applying the provided
stream-bearing function to the elements of the stream
distinct()
The elements of the stream, with duplicates removed
sorted()
The elements of the stream, sorted in natural order
Sorted(Comparator<T>)
The elements of the stream, sorted by the provided comparator
limit(long)
The elements of the stream, truncated to the provided length
skip(long)
The elements of the stream, discarding the first N elements
takeWhile(Predicate<T>)
(Java 9 only) The elements of the stream, truncated at the first element
for which the provided predicate is not true
dropWhile(Predicate<T>)
(Java 9 only) The elements of the stream, discarding the initial segment
of elements for which the provided predicate is true


 Terminal stream operations:- 
Operation
Description
forEach(Consumer<T> action)
Apply the provided action to each element of the stream.
toArray()
Create an array from the elements of the stream.
reduce(...)
Aggregate the elements of the stream into a summary value.
collect(...)
Aggregate the elements of the stream into a summary result container.
min(Comparator<T>)
Return the minimal element of the stream according to the comparator.
max(Comparator<T>)
Return the maximal element of the stream according to the comparator.
count()
Return the size of the stream.
{any,all,none}Match(Predicate<T>)
Return whether any/all/none of the elements of the stream match the
provided predicate.
findFirst()
Return the first element of the stream, if present.
findAny()
Return any element of the stream, if present.


No comments:

Post a Comment