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.
employeeList.stream() .filter(e -> e.getCity().equalsIgnoreCase("Matara")) .sorted(Comparator.comparing(Employee::getName)) .forEach(e -> System.out.println(e.getName()));
Find distinct department names that employees work for.
employeeList.stream() .map(e -> e.getDepartment().getDepartmentName()) .distinct() .forEach(System.out::println);
Find the department names that these employees work for, where the number of employees in the department is over 50.
employeeList.stream() .map(Employee::getDepartment) .filter(d -> d.getNoOfEmployees() > 50) .distinct() .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()) .distinct() .sorted() .reduce("", (a, b) -> (a + "," + b)); System.out.println(s);
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.
employeeList.stream() .filter(e -> e.getDepartment().getDepartmentName().equalsIgnoreCase("Account")) .map(Employee::getName) .forEach(System.out::println);
What is the highest number of of employees in all departments?
employeeList.stream() .map(e -> e.getDepartment().getNoOfEmployees()) .reduce(Integer::max) .ifPresent(System.out::print);
Find the department which has the highest number of employees.
employeeList.stream() .map(Employee::getDepartment) .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.
employeeList.stream() .map(Employee::getDepartment) .max(Comparator.comparing(Department::getNoOfEmployees)) .ifPresent(d -> System.out.println(d.getDepartmentName()));
Find the total number of employees in all the departments.
employeeList.stream() .map(e -> e.getDepartment().getNoOfEmployees()) .distinct().reduce(Integer::sum).ifPresent(System.out::println);
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. |
The last example – what if several departments have the same number of employees? Maybe it should be:
employeeList.stream()
.map(e -> e.getDepartment())
.distinct()
.map(d -> d.getNoOfEmployees())
.reduce(Integer::sum).ifPresent(System.out::println);