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-

		ArrayList<Integer> arrayList = new ArrayList<Integer>();
		arrayList.add(0);
		arrayList.add(10);
		arrayList.add(20);
		arrayList.add(5);
		arrayList.add(15);
		arrayList.add(25);
		System.out.println(arrayList);

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

				List<Integer> list = new ArrayList<Integer>();
				for(Integer i:arrayList){
					if(i%2 == 0)
					list.add(i);
				}
						System.out.println(arrayList);

output will be [0,10,20]

with stream concept-

List<Integer> list = arrayList.stream().filter(i->i%2==0).collect(Collectors.toList());
		System.out.println(arrayList);

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.

List<Integer> l = arrayList.stream().map(i-> i+5).collect(Collectors.toList());

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

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

	long noOfEvenInteger = arrayList.stream().filter(i-> i%2!=0).count();

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

List<Integer> l = arrayList.stream().sorted().collect(Collectors.toList()); return an ascending order list.

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;

List<Integer> list = arrayList.stream().sorted((i1,i2)->(i1, i2)-> (i1<i2)?1:(i1 > i2)?-1:0).collect(Collectors.toList());

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

arrayList.stream().forEach(System.out::println);

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

Consumer<Integer> fun = i->{System.out.println("square of"+i+"is = "+(i*i))};
arrayList.stream().forEach(fun);

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

Integer[] arr =	arrayList.stream().toArray(Integer::new);

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

Stream s = stream.of(10,1,2,12,34);
s.forEach(System.out::println);

 

Leave a Reply