Java Streams API - Process set of elements

A Stream is a series of objects. Source of a stream is an Array, Collection, or Other I/O source. A Stream does not store data, it does only intermediate operations.

It can filter and manipulate the elements of its source. Streams are lazy, the source and intermediate operations do nothing until objects are needed by the terminal operation.

Steams APIs are contained by package. 

A water filter is the best example of streams.   It processes the water stream.  It has multiple filters that work one after another.  Filters do purification, reverse osmosis, refinement of water, and produce drinking water.  The water filter does not store the water it simply processes the water stream.

Getting Started 

1. Create a Stream and print all elements 

Let's create a stream from a collection and print all its elements. 

The following example creates a Stream from the list collection and prints all elements of the collection using its forEach method.

List<String> items = Arrays.asList("One", "Two", "Three", "Three", "Four");

//create stream
Stream<String> stream =;

// Print all elements from stream
stream.forEach(e -> {

2.  Sort the elements and then print 

Stream elements can be sorted using sorted() method.

The following example will create a stream, sort its elements using sorted() method, and then print all elements. -> {

3.  Convert each element into UPPER CASE then print

Stream elements can be modified using map() method.

The following example will create a stream, convert each element into the upper-case using its map() method, and then print all elements. -> e.toUpperCase()).forEach(e -> {

4.  Filter elements starting with T character and  print

Stream elements can be filtered using filter() method. 

The following example will filter elements starting with T character using filter() method, convert them into upper case, and then print all elements. -> e.startsWith("T")).map(e -> e.toUpperCase()).forEach(e -> {

5.  Remove duplicate elements 

Stream can remove its duplicate elements using distinct() method. 

The following example will filter elements, remove duplicate elements,  convert elements to upper case,  then print all elements. -> e.startsWith("T")).distinct().map(e -> e.toUpperCase()).forEach(e -> {

6.  All together 

The following example will filter elements, remove duplicate elements,  convert elements to upper case,  sort elements then print all elements. -> e.startsWith("T")).distinct().map(e -> e.toUpperCase()).sorted().forEach(e -> {

7. Get collection from Stream 

You can get processed elements from Stream into a collection using collect() method.

List<String> l = -> e.startsWith("T")).distinct().map(e -> e.toUpperCase()).sorted().collect(Collectors.toList());

Streams vs Collections

Stream and Collection  look similar but there are major differences between both:

 # Stream                                                 Collection                                         
 1It does not store data    It stores data         
 2It is read-onlyIt is read and write both     
 3It can only be read once It can be read multiple times
 4Elements can not be directly accessed Elements can be directly accessed 
i = list.get(0)

Stream Sources 

A stream can be created from arrays, collection, files, and other I/O sources.

1. Collections/ Arrays 

List collection, Set collections, and arrays are the common sources of the stream. Map can not be directly used as a source.

String[] a = { "One", "Two", "Three", "Four"};
Stream<String> aStream =;

List<String> l = Arrays.asList("One", "Two", "Three", "Four");
Stream<String> lStream =;

2. I/O

Data from File and  Network can be used as a source to the stream.

Stream<String> fiileData = Files.lines(Path.of("data.txt"));

3. Generators

A Stream can be generated from a function that returns elements for the stream.

For example in this example stream is created from random.nextInt() method.  After creating stream we print 5 random numbers.
Random random = new Random();
Stream<Integer> randomNumbers = Stream.generate(random::nextInt);
Same can be done by Math.random() method:
Stream<Double> randomNumbers = Stream.generate(Math::random);

Intermediate operations 

An Intermediate Operation transforms one stream into another stream.

Stream methods filter(), map(), distinct(), sorted() etc, perform intermediate operations, and produce another stream.

List<String> items = Arrays.asList("One", "Two", "Three", "Three", "Four");
Stream<String> stream =;; -> e.toUpperCase()); -> e.startsWith("T"));;
Intermediate operations are 
  1. map() -It maps a function to a stream and changes elements of the stream
  2. filter() - It filters element of the stream 
  3. distinct() - It removes duplicate elements from the stream
  4. sorted() - It sortes elements of the stream 
  5. limit() - It limits and returns the stream according to the given maxSize parameter
  6. skip() - It skips the given N elements of the stream  

Channing intermediate operations 

Channing makes Stream powerful. The output of one stream can be an input of another stream it is called Channing. 

You can chain multiple operations on stream and produce the desired result using the least code.

For example, the following code applies Channing operations filter, distinct, map, sort, and finally prints all elements of steam in a single line of code. -> e.startsWith("T")).distinct().map(e -> e.toUpperCase()).sorted();

Terminal stream operations

Streams are lazy, which means sources and intermediate operations do not do any work until a terminal is executed.

List<String> items = Arrays.asList("One", "Two", "Three", "Three", "Four");
Stream<String> stream =;
items = -> e.startsWith("T")).distinct().map(e -> e.toUpperCase()).sorted();
items.forEach(e -> {

In the above example filter, distinct, map, and sorted intermediate operations will not be executed until the terminal operation foreach() method is called. 

Terminal operations are 
  1. collect() - It converts an stream into collection
  2. toArray() - It converts a stream into array
  3. count() -It returns the count of the elements in the stream
  4. reduce() - It reduces the elements of the stream according to the given identity value. 
  5. min() - It  returns the minimum element of the stream according to the given comparator
  6. max() - It  returns the maximum element of the stream according to the given comparator
  7. anyMatch() - It will return any matched element according to the matched predicate. if the stream is empty then return false
  8. allMatch() -It will return all the matched elements from a stream. returns true if the stream is empty.
  9. noneMatch() - It returns the matched elements of the predicate. returns true if the stream is empty
  10. findAny() - It returns an optional describing some elements of the stream
  11. findFirst() -  It returns the first elements of the stream.

Statistical Operations 

You can performer max, min, average statistical operation on integer or double stream.

Classes IntSummaryStatistics and DoubleSummaryStatistics are used to get integer and double value statistical operations.

Methods mapToInt() and mapToDouble() are used to convert stream elements into integers or double values before applying the statistical operations.

Following example will apply statistical operations on marks and account balance:

List<Integer> marks = Arrays.asList(80, 90, 75, 50, 45, 95, 75);
IntSummaryStatistics mStats = -> x).summaryStatistics();
System.out.println("## Marks statistics ##");
System.out.println("Max Marks: " + mStats.getMax());
System.out.println("Min Matrks: " + mStats.getMin());
System.out.println("Average Marks:" + mStats.getAverage());
System.out.println("Sum : " + mStats.getSum());

List<Double> accountBalance = Arrays.asList(10000.89, 20000.77, 5000.66, 5500.55, 7000.88, 30000.99, 50000.00);
DoubleSummaryStatistics aStats = -> x).summaryStatistics();
System.out.println("## Balance statistics ## ");
System.out.println("Max Balance: " + aStats.getMax());
System.out.println("Min Balance: " + aStats.getMin());
System.out.println("Average Balance:" + aStats.getAverage());
System.out.println("Sum : " + aStats.getSum());

Real-World Example 

Let's have  Lucky Draw Contest in which contestants have to send SMS.  We will select random 3 winners from received SMSs 

We will follow the following procedure to select 3 winners:

Let's have data 

class Contestant {
public String phone = null;
public Contestant(String n, String p) {
name = n;
phone = p;
ArrayList<Contestant> list = new ArrayList<Contestant>();
list.add(new Contestant("Ram", "9912345678"));
list.add(new Contestant("Shyam", "9912342222"));
list.add(new Contestant("Ajay", "9912345770"));
list.add(new Contestant("Vijay", "9912345678"));
list.add(new Contestant("Jay", "9912345888"));
list.add(new Contestant("Pappu", "9912345111"));
list.add(new Contestant("InvalidNO", "11"));

1. Get the phone numbers ->

2. Get valid phone numbers -> -> e.length() == 10)

3. Remove duplicate phone numbers -> -> e.length() == 10).distinct()

4. Shuffle Phone numbers -> -> e.length() == 10).distinct()
.collect(Collectors.collectingAndThen(Collectors.toList(), e -> {

5. Get 3 Winners -> -> e.length() == 10).distinct()
.collect(Collectors.collectingAndThen(Collectors.toList(), e -> {

6. Display winners -> -> e.length() == 10).distinct()
.collect(Collectors.collectingAndThen(Collectors.toList(), e -> {
})).limit(3).forEach(e -> System.out.println(e));

WOW !!! Its very simple !! 


Q:  Write a program to print odd numbers from a list.

import java.util.Arrays;
import java.util.List;
public class PrintOddNumbers {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 4, 8, 40, 11, 22, 33, 99); -> o % 2 != 0).forEach(System.out::println);


Example Source Code

Find source code at GIT 

Also Watch:

► Deploy Angular build on Tomcat or WAMP server -  
► SpringBoot Hibernate Integration -  
► Spring Boot – Create RESTful Web Service -  
► Angular app using REST API (Part 3) -  
► Test Public REST API -  
► Angular First Project (Part-2) -  
► Angular First Project (Part-2) -  
► First Python Program -  




►URL -,


  1. Good Evening Sir,
    Does it create any impact on execution time if we change the sequence of filter->map()->reduce on any raw list(source)?

    Thank you for the content sharing sir.

    1. Yes, it will, you should execute operations in the order that number of elements will be reduced for next operation. Elimination filter operations should always come first.

  2. Thank you because you have been willing to share information with us. We will always appreciate all you have done here because I know you are very concerned with us. Whole house water filtration

  3. It is truly a well-researched content and excellent wording about Bilge Oil Water Separator System. I got so engaged in this material that I could not wait to read it. I am impressed with your work and skill. Thanks.

  4. I read your blog in which you shared your best knowledge and great tips about american made water filters. I really need this information. thank you so much for this knowledge. Keep sharing.

  5. You are sharing a piece of pleasant data here. The data you have given is really educational and critical for us. Gratitude for sharing an article like this.Buy sustainable water solutions online


Post a Comment

Popular posts from this blog

Java 8 Vs Java 9

Concurrent Collection in Java