Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions streams/Employee.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
70 changes: 70 additions & 0 deletions streams/EmployeeAnalysis.java
Original file line number Diff line number Diff line change
@@ -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<Employee> 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<String, Long> 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<String, Optional<Employee>> 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<Employee> 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";
}
}
}