Home » Java » Core Java » Java 8 Stream examples

About Semika Kaluge

Semika Kaluge
I am working in software engineering field for six years of time by now. Currently, I am working for Shipxpress Inc in Sri Lanka. Primarily, I love to involve with Java developments and related frameworks like Spring, Struts, Hibernate and many more, specially interested in Javascript.

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) {
    this.name = name;
    this.age = age;
    this.city = 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 = employeeList.stream()
                       .map(e -> e.getDepartment().getDepartmentName())
                       .reduce("", (a, b) -> (a + "," + b)); 

Are there any employees from HR Department?

if (employeeList.stream()
                .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().getNoOfEmployees())
Published on Java Code Geeks with permission by Semika Kaluge, partner at our JCG program. See the original article here: Java 8 Stream examples

Opinions expressed by Java Code Geeks contributors are their own.

(0 rating, 0 votes)
You need to be a registered member to rate this.
1 Comment Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Leave a Reply

1 Comment threads
0 Thread replies
Most reacted comment
Hottest comment thread
1 Comment authors
Peetey Recent comment authors

This site uses Akismet to reduce spam. Learn how your comment data is processed.

newest oldest most voted
Notify of

The last example – what if several departments have the same number of employees? Maybe it should be:

.map(e -> e.getDepartment())
.map(d -> d.getNoOfEmployees())