Programming Assignment Unit 8
Using the Function Interface and Streams in Java
Introduction
This assignment demonstrates the use of Java's Function interface and Stream API to
process employee data efficiently.
Purpose of the Function Interface
The Function<T,R> interface represents a function that accepts one input and produces one
output.
Complete Java Program
import [Link].*;
import [Link];
import [Link];
class Employee {
private String name;
private int age;
private String department;
private double salary;
public Employee(String name, int age, String department, double salary) {
[Link] = name;
[Link] = age;
[Link] = department;
[Link] = salary;
}
public String getName() { return name; }
public int getAge() { return age; }
public String getDepartment() { return department; }
public double getSalary() { return salary; }
}
public class EmployeeManagement {
public static void main(String[] args) {
List<Employee> employees = [Link](
new Employee("John", 25, "IT", 50000),
new Employee("Mary", 35, "HR", 60000),
new Employee("David", 40, "Finance", 75000),
new Employee("Sarah", 29, "Marketing", 55000),
new Employee("James", 45, "IT", 80000)
);
Function<Employee, String> employeeInfo =
emp -> [Link]() + " - " + [Link]();
List<String> employeeDetails = [Link]()
.map(employeeInfo)
.collect([Link]());
[Link]([Link]::println);
double averageSalary = [Link]()
.mapToDouble(Employee::getSalary)
.average()
.orElse(0);
[Link]("Average Salary: " + averageSalary);
[Link]()
.filter(emp -> [Link]() > 30)
.forEach(emp -> [Link]([Link]()));
}
}
Sample Output
John - IT
Mary - HR
David - Finance
Sarah - Marketing
James - IT
Average Salary: 64000.0
Conclusion
The program demonstrates practical use of the Function interface and Stream API for
filtering, mapping, and analyzing employee data.