From 5dcaa33dc548190bec8acd6197d324725fa65f3b Mon Sep 17 00:00:00 2001 From: hirushi1999 Date: Tue, 14 Oct 2025 14:46:27 +0530 Subject: [PATCH] Java Streams example with Real world scenario --- streams/Employee.java | 67 +++++++++++++++++++++++++++++++++ streams/EmployeeAnalysis.java | 70 +++++++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+) create mode 100644 streams/Employee.java create mode 100644 streams/EmployeeAnalysis.java diff --git a/streams/Employee.java b/streams/Employee.java new file mode 100644 index 0000000..15d1d7c --- /dev/null +++ b/streams/Employee.java @@ -0,0 +1,67 @@ +package streams; + +public class Employee { + private int id; + private String name; + private String dept; + private int age; + private double salary; + private boolean active; + + public Employee(int id, String name, String dept, int age, double salary, boolean active) { + this.id = id; + this.name = name; + this.dept = dept; + this.age = age; + this.salary = salary; + this.active = active; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDept() { + return dept; + } + + public void setDept(String dept) { + this.dept = dept; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public double getSalary() { + return salary; + } + + public void setSalary(double salary) { + this.salary = salary; + } + + public boolean isActive() { + return active; + } + + public void setActive(boolean active) { + this.active = active; + } +} diff --git a/streams/EmployeeAnalysis.java b/streams/EmployeeAnalysis.java new file mode 100644 index 0000000..fb859d5 --- /dev/null +++ b/streams/EmployeeAnalysis.java @@ -0,0 +1,70 @@ +package streams; + +/** + * Program Title: Java Streams example with Real world scenari + * Author: [hirushi1999] + * Date: 2025-10-14 + * Demonstrates how to use Java Streams to perform data analysis on a list of employees. + * Scenario: + * - A company has employees working in various departments (HR, Engineering, Sales). + * - Each employee has attributes such as age, salary, and active status. + * Task: + * 1. Count how many employees fall into each age group. + * 2. Find the highest salary in each department among active employees. + * 3. List all active employees. + */ + +import java.util.*; +import java.util.stream.Collectors; + +public class EmployeeAnalysis { + public static void main(String[] args) { + //Create list of employees + List employeeList = Arrays.asList( + new Employee(1, "Alice", "HR", 25, 50000.0, true), + new Employee(2, "Bob", "Engineering", 35, 70000.0, false), + new Employee(3, "Charlie", "HR", 30, 55000.0, true), + new Employee(4, "David", "Engineering", 28, 72000.0, true), + new Employee(5, "Ella", "Sales", 26, 48000.0, true), + new Employee(6, "Frank", "Sales", 32, 49000.0, false), + new Employee(7, "Grace", "Engineering", 24, 69000.0, true), + new Employee(8, "Helen", "HR", 22, 51000.0, false), + new Employee(9, "Ian", "Sales", 27, 47000.0, true)); + + System.out.println("------------------------------------------------------"); + System.out.println("1.Count how many employees fall into each age group"); + Map countAgeRange = employeeList.stream() + .collect(Collectors.groupingBy(e -> getAgeGroup(e.getAge()).toString(), Collectors.counting())); + + countAgeRange.forEach((k, v) -> { System.out.println(k + ": " + v); }); + + System.out.println("------------------------------------------------------"); + System.out.println("2. Find the highest salary in each department among active employees"); + Map> highestsal = employeeList.stream() + .filter(Employee::isActive) + .collect(Collectors.groupingBy(Employee::getDept, + Collectors.maxBy(Comparator.comparingDouble(Employee::getSalary)))); + + highestsal.forEach((k, v) -> { + System.out.println(k + ": " + v.get().getName() + ": " + v.get().getSalary()); + }); + + System.out.println("------------------------------------------------------"); + System.out.println("3. List all active employees"); + List ActiveEmp = employeeList.stream().filter(Employee::isActive).collect(Collectors.toList()); + for (Employee e : ActiveEmp) { + System.out.print(e.getName() + ", "); + } + + } + + private static Object getAgeGroup(int age) { + if(age>=20 && age<=29){ + return "20-29"; + } else if (age>=30 && age<=39) { + return "30-39"; + } else { + return "+40"; + } + } +}