Tuesday, August 18, 2015

Stream Collection types

Java is inheritably sequential and there is no direct mean to introduce parallel processing at library level, Stream API is going to fill that gap. By using this, you can filter elements of collection on given criterion e.g. if you have a List of employees, you can filter all employees based upon their name, department and so on.

Stream API allows you to process Collection both sequentially and parallel. This is also useful for bulk data operation. You can create a sequential and parallel stream as follows :

List employees = getListOfEmployees();
 
// sequential version
Stream  stream = employees.stream();
 
//parallel version
Stream  parallelStream = employees.parallelStream();

Collection and Stream

Collection interface is enhanced to provide stream support. It now has a stream() method which returns a sequential Stream with this collection as its source. Once you get the reference of stream, you can perform bulk data operations with this collection.

One of the important things to note is that Stream do not modify the original source. For every operation, a new Stream is created and original collection remains unmodified. Similarly, you cannot reuse Stream either. Reusing a closed stream will throw IllegalStateException as shown below:

Exception in thread "main" java.lang.IllegalStateException: stream has already been operated upon or closed
    at java.util.stream.AbstractPipeline.(AbstractPipeline.java:203)
    at java.util.stream.ReferencePipeline.(ReferencePipeline.java:94)
    at java.util.stream.ReferencePipeline$StatelessOp.(ReferencePipeline.java:618)
    at java.util.stream.ReferencePipeline$2.(ReferencePipeline.java:163)
    at java.util.stream.ReferencePipeline.filter(ReferencePipeline.java:162)
    at Test.main(Test.java:33)

It supports functional programming idioms such as map and reduce along with parallel processing capability, support for filtering elements from Collection.


Stream operations 

Stream operations are mainly divided into two categories : intermediate and terminal operations. Intermediate operations such as filter() or map() returns a new Stream, while terminal operations such as Stream.forEach() produce a result or side effect. After the terminal operation, the stream pipeline is considered consumed, and can no longer be used.

Intermediate operations are also of two types stateless and stateful. As name suggests, stateless operations doesn't retain any state from previously processed element. For example : filter() and map(). On the other hand distinct() and sorted() are example of stateful operations, which can have state from previously processed elements, while processing new elements.

Important methods in java.util.stream

Return Type
Method Name
Short Description
Long
count()
Returns the count of elements in this stream
Stream
Distinct()
Returns a stream consisting of the distinct elements (according to Object.equals(Object)) of this stream.
static Stream
Empty()
Returns an empty sequential Stream.
Stream
Filter(Predicate predicate)
Returns a stream consisting of the elements of this stream that match the given predicate.
Stream
flatMap(Function> mapper)
Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element.
void
forEach(Consumer action)
Performs an action for each element of this stream.
static Stream
generate(Supplier s)
Returns an infinite sequential unordered stream where each element is generated by the provided Supplier.
static Stream
iterate(T seed, UnaryOperator f)
Returns an infinite sequential ordered Stream produced by iterative application of a function f to an initial element seed, producing a Stream consisting of seed, f(seed), f(f(seed)), etc.
Stream
Limit(long maxSize)
Returns a stream consisting of the elements of this stream, truncated to be no longer than maxSize in length.
Stream
map(Function mapper)
Returns a stream consisting of the results of applying the given function to the elements of this stream.
Optional  
max(Comparator comparator)
Returns the maximum element of this stream according to the provided Comparator.
Optional  
min(Comparator comparator)
Returns the minimum element of this stream according to the provided Comparator.
Stream
skip(long n)
Returns a stream consisting of the remaining elements of this stream after discarding the first n elements of the stream.
Stream
sorted()
Returns a stream consisting of the elements of this stream, sorted according to natural order.
Stream
Sorted (Comparator comparator)
Returns a stream consisting of the elements of this stream, sorted according to the provided Comparator.
Object[]
toArray()
Returns an array containing the elements of this stream.

No comments:

Post a Comment

Java garbage collection

In this post , we ’ ll take a look at how garbage collection works , why it ’ s important in Java , and how it works in...