0% found this document useful (0 votes)
10 views6 pages

programmed assessment

This document describes a Java program that utilizes the Function interface and Stream API to process employee data, including operations like transforming data, calculating average salary, and filtering employees by age. Key features include the use of the Function interface for data transformation, various stream operations for data processing, and performance considerations related to lazy evaluation. The program also demonstrates advanced stream usage by grouping employees by department and calculating average salaries.

Uploaded by

cisse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views6 pages

programmed assessment

This document describes a Java program that utilizes the Function interface and Stream API to process employee data, including operations like transforming data, calculating average salary, and filtering employees by age. Key features include the use of the Function interface for data transformation, various stream operations for data processing, and performance considerations related to lazy evaluation. The program also demonstrates advanced stream usage by grouping employees by department and calculating average salaries.

Uploaded by

cisse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Name: Hassan Azeez

Java Program Using Function Interface and Streams for Employee Data
Processing

Program Explanation

This program demonstrates the use of Java's Function interface and


Stream API to process employee data efficiently. The program performs
several operations:

1. Creates a collection of employee objects

2. Uses a Function to transform employee data

3. Processes data using streams with mapping, filtering, and aggregation

4. Calculates average salary

5. Filters employees by age threshold

The Function Interface

The `Function<T,R>` interface in Java represents a function that accepts


one argument (of type T) and produces a result (of type R). Its key
characteristics are:

- Single abstract method: `R apply(T t)`

- Can be used with lambda expressions or method references

- Enables functional programming style in Java

- Can be chained with other functions using `andThen()` or `compose()`

- Primarily used for transformations and data processing

In this program, we use a Function to transform an Employee object into a


concatenated string of name and department.

Java Program Implementation

```java

import java.util. ;
import java.util.function.Function;

import java.util.stream.Collectors;

class Employee {

private String name;

private int age;

private String department;

private double salary;

public Employee(String name, int age, String department, double


salary) {

this.name = name;

this.age = age;

this.department = department;

this.salary = salary;

// Getters

public String getName() { return name; }

public int getAge() { return age; }

public String getDepartment() { return department; }

public double getSalary() { return salary; }

@Override

public String toString() {

return "Employee{name='" + name + "', age=" + age +

", department='" + department + "', salary=" + salary + "}";

}
public class EmployeeDataProcessor {

public static void main(String[] args) {

// Create sample employee data

List<Employee> employees = Arrays.asList(

new Employee("John Doe", 28, "Engineering", 75000),

new Employee("Jane Smith", 32, "Marketing", 68000),

new Employee("Mike Johnson", 45, "Sales", 92000),

new Employee("Sarah Williams", 29, "Engineering", 78000),

new Employee("David Brown", 38, "HR", 65000),

new Employee("Lisa Davis", 41, "Finance", 85000)

);

// Function to concatenate name and department

Function<Employee, String> nameDeptConcatenator =

emp -> emp.getName() + " - " + emp.getDepartment();

// 1. Generate collection of concatenated strings using streams

List<String> employeeNameDeptList = employees.stream()

.map(nameDeptConcatenator)

.collect(Collectors.toList());

System.out.println("Employee Name-Department List:");

employeeNameDeptList.forEach(System.out::println);

// 2. Calculate average salary using streams

double averageSalary = employees.stream()

.mapToDouble(Employee::getSalary)

.average()
.orElse(0.0);

System.out.printf("\nAverage Salary: $%.2f\n", averageSalary);

// 3. Filter employees by age threshold (e.g., 30 years)

int ageThreshold = 30;

List<Employee> employeesAboveThreshold = employees.stream()

.filter(emp -> emp.getAge() > ageThreshold)

.collect(Collectors.toList());

System.out.println("\nEmployees above " + ageThreshold + "


years:");

employeesAboveThreshold.forEach(System.out::println);

// Bonus: Group employees by department with their average salary

System.out.println("\nDepartment-wise average salaries:");

Map<String, Double> avgSalaryByDept = employees.stream()

.collect(Collectors.groupingBy(

Employee::getDepartment,

Collectors.averagingDouble(Employee::getSalary)

);

avgSalaryByDept.forEach((dept, avg) ->

System.out.printf("%s: $%.2f\n", dept, avg));

Key Features Demonstrated


1. Function Interface Usage: The `nameDeptConcatenator` function
demonstrates how to transform objects using the Function interface.

2. Stream Operations:

- `map()` for transforming data

- `filter()` for selecting elements

- `collect()` for terminal operations

- `mapToDouble()` and `average()` for numerical calculations

3. Performance Considerations :

- Streams use lazy evaluation, processing data only when needed

- Operations are chained efficiently in a single pass where possible

- Built-in functions optimize common operations like averaging

4. Additional Feature : The program includes a bonus operation that


groups employees by department and calculates average salaries per
department, demonstrating more advanced stream usage.

5. Readability: The code uses meaningful variable names, proper


indentation, and clear structure to enhance maintainability.

This implementation effectively showcases how Java's Function interface


and Stream API can be used together to create concise, readable, and
efficient data processing pipelines.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy