Blog Image

Java 8 Stream Api  

Stream Api is provided as part of the java 8 in package. The features and the how to use stream api is explained as part of this blog. How use of stream api reduces writing extra code and let the developer to focus on business logic explained in this blog by giving a basic example.

Stream api is introduced in Java 8 as part of package, it lets the developer to process data declaratively and use the functionality of parallel processing without the need to writing extra code for it. Java 8 stream api abstracts the low level multi-threading logic so that the developer can focus on processing the data and business operations.

The stream is a sequence of objects of a specific type. Stream takes Collections, Arrays, or I/O resources as input source and compute it on demand dynamically. It do not store elements. Since the stream computes the data on-demand, it is not possible to reuse the same stream multiple times. So there is no way to create a reference to stream for future usage.

The Streams are lazily constructed collections which computes the values when client demands for it.
But when it comes to Collections, collections eagerly constructed values. The elements in the collection are computed before they becomes a part of that collection.

Streams are designed in such a way that most of its stream operations returns Streams only. This help us creating chain of various stream operations. This is called as pipelining.These operations are intermediate operations and their function is to take input, process it, and return output. collect() is a terminal operation which is normally present at the end of the pipelining operation.

Stream api support sequential as well as parallel processing, parallel processing provides high performance for large collections. Stream api also provides the iterations internally on the source elements like Collections, Arrays etc. Previously we have to write the logic for iterations explicitly.

Some important operations in stream:

  • map(): It is an intermediate operation which use to convert one element to other object.
  • filter(): It filters the stream of elements based on the given predicate.
  • sorted(): Sorted is an intermediate operation which returns a sorted view of the stream.
  • forEach(): This method helps in iterating over all elements of a stream and perform some operation on each of them.
  • collect(): It is used to receive elements from a stream and store them in a collection. It is the termination operation of stream function.

Below is the basic example for stream api using above operations:

public class Employee {
    protected int empId;
    protected String name;
    protected String designation;

    public Employee(int empId, String name, String designation) {
        this.empId = empId; = name;
        this.designation = designation;

    public int getEmpId() {
        return empId;

    public void setEmpId(int empId) {
        this.empId = empId;

    public String getName() {
        return name;

    public void setName(String name) { = name;

    public String getDesignation() {
        return designation;

    public void setDesignation(String designation) {
        this.designation = designation;


public class StreamApiTest {

    public static void main(String[] args) {
        List<Employee> employees = null;

        employees = new ArrayList<Employee>();
        Employee employee1 = new Employee(1, "John", "Manager");
        Employee employee2 = new Employee(2, "Mathew", "Developer");
        Employee employee3 = new Employee(3, "Will", "Developer");
        Employee employee4 = new Employee(4, "Tom", "Developer");
        Employee employee5 = new Employee(5, "Jim", "Tester");

        employees = Arrays.asList(employee1, employee2, employee3, employee4, employee5);
        /* use of map()  */
        List<String> names =;
        System.out.println("Names of employees: " + names);
        List<String> namesInCaps =;
        System.out.println("\nNames in Capitals: " + namesInCaps);
        /* use of sorted() */
        List<String> sortedNames =;
        System.out.println("\nNames by sorting oreder: " + sortedNames);
        /* use of filter() */
        List<String> designations =
   .filter(designation -> designation.startsWith("D")).collect(Collectors.toList());
        System.out.println("\nDesignations: " + designations);

        List<String> designationsLimits =
   .filter(designation -> designation.startsWith("D")).limit(2).collect(Collectors.toList());
        System.out.println("\nDisignation with limits :" + designationsLimits);

        /* forEach in stream api */
        System.out.println("\nforEach loop using java Stream api");
        /* use of count() */
        long count =;
        System.out.println("\nTotal elements in employee list: "+count);

The output for the above program is as follows:

Names of employees: [John, Mathew, Will, Tom, Jim]

Names in Capitals: [JOHN, MATHEW, WILL, TOM, JIM]

Names by sorting oreder: [Jim, John, Mathew, Tom, Will]

Designations: [Developer, Developer, Developer]

Disignation with limits :[Developer, Developer]

forEach loop using java Stream api

Total elements in employee list: 5

You can download and run the above maven example from the attachments given below.


About author

User Image

I am a software developer. I am working in StackSpace Software Solutions as a java developer.I am passionate about learning new things and it is the most beautiful experience to learn great things in proper way with proper dedication.



Be the first person to write a comment for this Blog
Load More

No More Comments

Leave a Comment

Your comment has been posted and will appear soon.