Stream API

Toprocess objects of the collection, in 1.8 version Streams concept introduced.

What is the difference between java.util.streams and streams?

java.util streams meant for processing objects from the collection. i.e, it represents a stream of objects from the collection but streams meant for processing binary and character data with respect to file. i.e it represents stream of binary data or character data from the file .hence streams and java.util streams both are different.

What is the difference between collection and stream?

If we want to represent a group of individual objects as a single entity then we should go for collection. If we want to process a group of objects from the collection then we should go for streams. We can create a stream object to the collection by using stream() method of Collection interface stream() method is a default method added to the Collection in 1.8 version.
                                          default Stream stream()


Stream stream =; Stream
Stream  s1= stream.fìlter (i -> i%2==O);

Hence to filter elements of collection based on some Boolean condition we should go for filter() method.


If we want to create a separate new object, for every object present in the collection based on our requirement then we should go for map () method of Stream interface.
                                    public Stream map (Function f);


Stream stream =;
Stream s1 = (i-> i+10);
Once we performed configuration we can process objects by using several methods.


  • Processing by collect() method
  • Processing by count() method
  • Processing by sorted() method
  • Processing by min() and max() methods
  • foreach() method
  • toArray()method
  • Stream.of()method

Processing by collect() method

This method collects the elements from the stream and adding to the specified to the collection indicated (specified) by argument.
To collect only even numbers from the array list.

Approach-1: without Streams

Approach-2: With Stream

Program for map() and collect Method()

Processing by count() method

This method returns number of elements present in the stream.
                                        public long count()
long count=ìlter(s ->s.length == 5).count();
syso(“the number of 5 length strings is:”+count);

Processing by sorted() method

If we sort the elements present inside stream then we should go for sorted() method. the sorting can either default natural sorting order or customized sorting order specified by comparator.
sorted()- default natural sorting order
sorted(Comparator c)-customized sorting order

syso(“according to default natural sorting order:”+i1);
List ,s2);
syso(“according to customized sorting order:”+i2);

Processing by min() and max() methods

>min(Comparator e) returns minimum value according to specified comparator.
>max(Comparator c) returns maximum value according to specified comparator

forEach() method: This method will not return anything. This method will take lambda expression as argument and apply that lambda expression for each element present in the stream.>sop(s)); println);

toArray() method:
We can use toArray() method to copy elements present ¡n the stream into specified array.

Stream.of() Method:

We can also apply a stream for group of values and for arrays.

Stream s = Stream.of(99,999,9999,99999);
s.forEach (System.out :: println);
Double[]d = {1O.O,1O.1,1O.2,1O.3};
Stream si =Stream.of(d);
si.forEach(System.out:: println);

Java 8 Features Date & Time API