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

What is the differences between 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 s =;
  • Stream is an interface present in Once we got the stream, by using that we can process objects of that collection.
  • We can process the objects in the following 2 phases
    1. Configuration
    2. Processing

1) Configuration:

We can configure either by using filter mechanism or by using map mechanism.


  • We can configure a filter to filter elements from the collection based on some boolean condition by using filter() method of Stream interface.
	public Stream filter(Predicate<T> t)
		here (Predicate<T > t ) can be a boolean valued function/lambda expression
Stream s =;
Stream s1 = s.filter(i -> i%2==0);
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);
			It can be lambda expression also
Stream s =;
Stream s1 => i+10);
Once we performed configuration we can process objects by using several methods.

2) Processing

  • 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

I. Processing by collect() method

This method collects the elements from the stream and adding to the specified to the collection indicated (specified) by argument.

Ex 1: To collect only even numbers from the array list

Approach-1: Without Streams
1) import java.util.*; 
2) class Test { 
3) public static void main(String[] args) { 
4) ArrayList<Integer> l1 = new ArrayList<Integer>(); 
5) for(int i=0; i<=10; i++) { 
6) l1.add(i); 
7) } 
8) System.out.println(l1); 
9) ArrayList<Integer> l2 = new ArrayList<Integer>(); 
10) for(Integer i:l1) { 
11) if(i%2 == 0) 
12) l2.add(i); 
13) } 
14) System.out.println(l2); 
15) } 
16) }
Approach-2: With Streams
1) import java.util.*; 
2) import*; 
3) class Test { 
4) public static void main(String[] args) { 
5) ArrayList<Integer> l1 = new ArrayList<Integer>(); 
6) for(inti=0; i<=10; i++) { 
7) l1.add(i); 
8) } 
9) System.out.println(l1); 
10) List<Integer> l2 = -> i%2==0).collect(Collectors.toList()); 
11) System.out.println(l2); 
12) } 
13) }
Ex: Program for map() and collect() Method
1) import java.util.*; 
2) import*; 
3) class Test { 
4) public static void main(String[] args) { 
5) ArrayList<String> l = new ArrayList<String>(); 
6) l.add("rvk"); l.add("rk"); l.add("rkv"); l.add("rvki"); l.add("rvkir"); 
7) System.out.println(l); 
8) List<String> l2 = ->s.toUpperCase()).collect(Collectors.toList()); 
9) System.out.println(l2); 
10) } 
11) }

II. Processing by count() method

This method returns number of elements present in the stream.
public long count()
long count = ->s.length()==5).count();
sop("The number of 5 length strings is:"+count);

III. 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.
sop("according to default natural sorting order:"+l3);

List<String>,s2) -> -s1.compareTo(s2)).collect(Collectors.toList());
sop("according to customized sorting order:"+l4);

IV. Processing by min() and max() methods

min(Comparator c)
returns minimum value according to specified comparator.

max(Comparator c)
returns maximum value according to specified comparator
String,s2) -> s1.compareTo(s2)).get();

sop("minimum value is:"+min);

String,s2) -> s1.compareTo(s2)).get();

sop("maximum value is:"+max);

V. 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.
Ex:>sop(s)); println);
1) import java.util.*; 
2) import*; 
3) class Test1 { 
4) public static void main(String[] args) { 
5) ArrayList<Integer> l1 = new ArrayaList<Integer>(); 
6) l1.add(0); l1.add(15); l1.add(10); l1.add(5); l1.add(30); l1.add(25); l1.add(20); 
7) System.out.println(l1); 
8) ArrayList<Integer>> i+10).collect(Collectors.toList()); 
9) System.out.println(l2); 
10) long count =>i%2==0).count(); 
11) System.out.println(count); 
12) List<Integer>; 
13) System.out.println(l3); 
14) Comparator<Integer> comp=(i1,i2)->i1.compareTo(i2); 
15) List<Integer>; 
16) System.out.println(l4); 
17) Integer; 
18) System.out.println(min); 
19) Integer; 
20) System.out.println(max);
22) println); 
24) } 
25) }

VI. toArray() method

We can use toArray() method to copy elements present in the stream into specified array
Integer[] ir =[] :: new);
for(Integer i: ir) {

VII. 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={10.0,10.1,10.2,10.3};
Stream s1=Stream.of(d);
s1.forEach(System.out :: println);