Java 8 – Print Employee Details Whose Age Is Greater Than 25

Introduction

Java 8 introduced the Stream API, which provides a functional and expressive way to process collections of data. One common task is filtering elements based on certain criteria. For instance, you may need to filter employees based on their age and print the details of those whose age is greater than 25. The Stream API makes this operation simple and concise.

In this guide, we will explore how to use Java 8 streams to filter and print the details of employees whose age is greater than 25.

Table of Contents

  • Problem Statement
  • Solution Steps
  • Java Program
    • Example: Filtering and Printing Employee Details by Age
  • Conclusion

Problem Statement

You need to filter a list of employees to find those whose age is greater than 25 and then print their details. This filtering is a common requirement in scenarios where you need to process or analyze data based on specific conditions.

Example:

  • Problem: Given a list of Employee objects, filter and print the details of employees whose age is greater than 25.
  • Goal: Use Java 8’s Stream API to efficiently perform this filtering and printing operation.

Solution Steps

  1. Create an Employee Class: Define an Employee class with fields such as name, age, and department.
  2. Filter Employees by Age: Use the filter method to filter employees whose age is greater than 25.
  3. Print the Employee Details: Use the forEach method to print the details of the filtered employees.

Java Program

Example: Filtering and Printing Employee Details by Age

First, define the Employee class with the necessary fields.

import java.util.Arrays;
import java.util.List;

/**
 * Java 8 - Print Employee Details Whose Age Is Greater Than 25
 * Author: https://www.rameshfadatare.com/
 */
public class FilterEmployeesByAgeExample {

    public static void main(String[] args) {
        List<Employee> employees = Arrays.asList(
            new Employee("Amit", 30, "IT"),
            new Employee("Priya", 25, "HR"),
            new Employee("Raj", 28, "Finance"),
            new Employee("Suman", 24, "IT"),
            new Employee("Kiran", 35, "HR")
        );

        // Filter and print employees whose age is greater than 25
        employees.stream()
                 .filter(employee -> employee.getAge() > 25)
                 .forEach(employee -> 
                     System.out.println("Name: " + employee.getName() +
                                        ", Age: " + employee.getAge() +
                                        ", Department: " + employee.getDepartment()));
    }
}

class Employee {
    private String name;
    private int age;
    private String department;

    public Employee(String name, int age, String department) {
        this.name = name;
        this.age = age;
        this.department = department;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public String getDepartment() {
        return department;
    }
}

Output

Name: Amit, Age: 30, Department: IT
Name: Raj, Age: 28, Department: Finance
Name: Kiran, Age: 35, Department: HR

Explanation

  • Employee Class: The Employee class includes fields name, age, and department. The getAge() method is used to retrieve the age of the employee.
  • filter(employee -> employee.getAge() > 25): The Stream API filters employees by their age, keeping only those whose age is greater than 25.
  • forEach(): The filtered employee details are printed to the console. For each employee that meets the age criterion, their name, age, and department are printed.

Conclusion

Using Java 8’s Stream API, filtering and printing the details of employees whose age is greater than 25 is both efficient and easy to implement. The combination of filter and forEach methods provides a clear and concise way to process and display data based on specific conditions.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top