java streams

Concept of Stream API Java 1.8

Stream concept is introduced in java 1.8 and present in java.util.stream package.It is used to process the object from collection or any group of objects or data source. We can easily understand java stream concept by it’s name stream, In stream water flows from one water source to destination and we can perform some operations like filtering, collecting etc on stream to get useful water,Same in java stream concept we can think object flows from one object source to destination by Stream pipelines(A stream pipeline is composed of a stream source, zero or more intermediate operations, and a terminal operation.).

We should be familiar with some concept before going to further in stream concept and remember one thing streams don’t actually store elements;  they are computed on demand.

1> C.stream() – we are going to get an stream object where C is collection object.

2>filter()- used to do filtering based on predicate(predicate is nothing but boolean condition, basically it is a functional interface so it can be replaced by lambda expression)

3> collect()- it is a terminal operation use to collect filtered or mapped data. Example-

output will be [0,10,20,5,15,25]. Now,we want to get a new  ArrayList of integer which contains only even integers from existing arrayList object. Here, how we process collection object without stream concept

output will be [0,10,20]

with stream concept-

output will be [0,10,20].In collection if we perform bulk operation then highly recommended to use stream concept.

some more useful method of stream concept-

4> map()- for every object if we want to perform some action and want some result object then we use map method if we want to increase all the arrayList data by 5 then we can use this function.

it always take functional interface(interface which contains only one interface) so we can replace it by lambda expression.

5> count() – use to count how many objects are there in stream ( not in data source).

6> sorted() – use to perform sorting in ascending order.

for customization we go for comparator concept.comparator is also a functional interface so we can replace it by lambda expression.

Comparator – it contains compare() method and we can replace it by lambda expression compare(obj1, obj2)-     return -ve if obj1 has to come before obj2;          return +ve if obj1 has to come after obj2;   return 0 if both are equal;

we perform sorting in desc order here, what we do inside compare method (i1, i2)-> (i1<i2)?1:(i1 > i2)?-1:0 . We compare two object i1 and i2 if i1 is smaller than i2 it means i1 has to come before i2, so it will return +ve else it check i1 is bigger or not if yes it means i1 has to come after i2, so it will return -ve and if both are equal then it will return 0;

7> forEach() – for every element if we want to perform some functionality.

it will print all object.We can call our own method also.

8> toArray() – to convert stream of object into array.

9> Stream.of(arr) – use to get stream from array or other group of data;

 

Leave a Reply