Java 8 – Print the Name of All Departments in the Organization

Introduction

Java 8 introduced the Stream API, which provides a functional and powerful way to process collections of data. One common task in an organization is to list the names of all departments. With the Stream API, you can easily extract, filter, and manipulate data, such as printing the names of all departments, ensuring each department is listed only once.

In this guide, we will explore how to use Java 8 streams to print the name of all unique departments in an organization.

Table of Contents

  • Problem Statement
  • Solution Steps
  • Java Program
    • Example: Printing the Name of All Departments
  • Conclusion

Problem Statement

You have a list of Employee objects, and each employee belongs to a department. The task is to print the names of all unique departments in the organization.

Example:

  • Problem: Given a list of Employee objects, extract and print the names of all unique departments.
  • Goal: Use Java 8’s Stream API to efficiently extract and print unique department names.

Solution Steps

  1. Create an Employee Class: Define an Employee class with fields such as name, gender, and department.
  2. Extract Department Names: Use map to extract the department name from each employee.
  3. Filter Unique Departments: Use distinct to ensure only unique department names are included.
  4. Print Department Names: Print the list of unique department names.

Java Program

Example: Printing the Name of All Departments

First, define the Employee class with the necessary fields.

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

/**
 * Java 8 - Print the Name of All Departments in the Organization
 * Author: https://www.rameshfadatare.com/
 */
public class DepartmentNamesExample {

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

        // Extract and print unique department names
        employees.stream()
                 .map(Employee::getDepartment)
                 .distinct()
                 .forEach(System.out::println);
    }
}

class Employee {
    private String name;
    private String gender;
    private String department;

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

    public String getName() {
        return name;
    }

    public String getGender() {
        return gender;
    }

    public String getDepartment() {
        return department;
    }
}

Output

IT
HR
Finance

Explanation

  • Employee Class: The Employee class includes fields name, gender, and department. The getDepartment() method returns the department to which the employee belongs.
  • map(Employee::getDepartment): The map method is used to extract the department name from each Employee object.
  • distinct(): The distinct() method filters out duplicate department names, ensuring each department is printed only once.
  • forEach(System.out::println): This method prints each unique department name to the console.

Conclusion

Using Java 8’s Stream API, you can easily extract and print the names of all unique departments in an organization. The combination of map, distinct, and forEach makes it straightforward to process and display unique data from a list of objects. This approach can be adapted to various scenarios where you need to extract and handle unique values from a collection.

Leave a Comment

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

Scroll to Top