Thursday, July 5, 2018

Java 8 Stream examples

This post will help you to understand some of the important and frequently used Stream operations in Java 8 which makes your programming with Java easy.

Let's take our traditional example, Employee and Department.  

public class Employee {
 private String name;
 private Integer age;
 private String city;
 private Department department;
 public Employee(String name, Integer age, String city, Department department) { = name;
    this.age = age; = city;
    this.department = department;

 // getters and setters.


public class Department {

 private String departmentName;
 private Integer noOfEmployees;

 public Department(String departmentName, Integer noOfEmployees) {
      this.departmentName = departmentName;
      this.noOfEmployees = noOfEmployees;

        // getters and setters

I am going to have some sample data set as follows in order to show you some important functionalities of Java 8 Stream interface. We have four departments and set of employees from those departments.

      Department account = new Department("Account", 75); 
      Department hr = new Department("HR", 50);
      Department ops = new Department("OP", 25);
      Department tech = new Department("Tech", 150);          
      List<Employee> employeeList = Arrays.asList(new  Employee("David", 32, "Matara", account), 
                           new  Employee("Brayan", 25, "Galle", hr),
                           new  Employee("JoAnne", 45, "Negombo", ops),
                           new  Employee("Jake", 65, "Galle", hr),
                           new  Employee("Brent", 55, "Matara", hr),
                           new  Employee("Allice", 23, "Matara", ops),
                           new  Employee("Austin", 30, "Negombo", tech),
                           new  Employee("Gerry", 29, "Matara", tech),
                           new  Employee("Scote", 20, "Negombo", ops),
                           new  Employee("Branden", 32, "Matara", account),
                           new  Employee("Iflias", 31, "Galle", hr)); 

Find all employees who lives in 'Matara' city, sort them by their name and print the names of employees.
     .filter(e -> e.getCity().equalsIgnoreCase("Matara"))
     .forEach(e -> System.out.println(e.getName()));

Find distinct department names that employees work for.
            .map(e -> e.getDepartment().getDepartmentName())

Find the department names that these employees work for, where the number of employees in the department is over 50.
            .filter(d -> d.getNoOfEmployees() > 50)
            .forEach(d -> System.out.println(d.getDepartmentName()));

Create a comma separate string of department names sorted alphabetically.

String s =
                       .map(e -> e.getDepartment().getDepartmentName())
                       .reduce("", (a, b) -> (a + "," + b)); 

Are there any employees from HR Department?

if (
                .anyMatch(e -> e.getDepartment().getDepartmentName().equalsIgnoreCase("HR"))) { 
    System.out.println("Found employees frm HR department"); 

Print all employee's name who are working for account department.
            .filter(e -> e.getDepartment().getDepartmentName().equalsIgnoreCase("Account"))

What is the highest number of of employees in all departments?
            .map(e -> e.getDepartment().getNoOfEmployees())

Find the department which has the highest number of employees.
            .reduce( (d1, d2) -> d1.getNoOfEmployees() > d2.getNoOfEmployees() ? d1 : d2)
            .ifPresent(d -> System.out.println(d.getDepartmentName()));

The same thing can be done as follows using the max() method.
            .ifPresent(d -> System.out.println(d.getDepartmentName()));

Find the total number of employees in all the departments.
            .map(e -> e.getDepartment())
            .map(e-> e.getNoOfEmployees())


Post a Comment