To address the assignment, to create a Java program that utilizes the Function
interface and streams to process a dataset of employee objects. Below is a
comprehensive guide and a sample Java program that achieves the described
requirements.
Explanation of the Function Interface
The Function interface in Java is a functional interface defined in
java.util.function. It represents a function that takes an input of type T and
produces a result of type R. The primary method of this interface is R apply(T t).
This interface is particularly useful when you need to pass behavior (i.e., a function)
as a parameter to other methods or when working with streams and functional
programming concepts.
Purpose and Characteristics of Streams
Java Streams provide a high-level abstraction for processing sequences of elements
(e.g., collections) in a functional style. Streams support various operations such as
filtering, mapping, and reducing. They can be sequential or parallel, enabling efficient
processing of data.
Characteristics of Streams:
1. Declarative: Streams allow you to specify what you want to achieve rather than how.
2. Pipelining: Operations on streams are chained, allowing for a pipeline of operations.
3. Lazy Evaluation: Intermediate operations are not executed until a terminal operation is
invoked.
4. Parallelism: Streams can be processed in parallel, leveraging multiple cores.
Java Program Implementation
Below is a Java program that reads a dataset of employees, uses the Function
interface to process employee data, and utilizes streams to perform various operations.
Code:
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;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getDepartment() {
return department;
}
public double getSalary() {
return salary;
}
}
public class EmployeeDataProcessing {
public static void main(String[] args) {
// Step 1: Create a dataset
List<Employee> employees = Arrays.asList(
new Employee("Alice", 28, "HR", 55000),
new Employee("Bob", 35, "IT", 60000),
new Employee("Charlie", 40, "Finance", 75000),
new Employee("David", 32, "IT", 65000),
new Employee("Eve", 29, "Marketing", 50000)
);
// Step 2: Define a function to concatenate name and department
Function<Employee, String> nameAndDepartmentFunction = e -> e.getName() + " (" +
e.getDepartment() + ")";
// Step 3: Generate a collection of concatenated strings using streams
List<String> concatenatedStrings = employees.stream()
.map(nameAndDepartmentFunction)
.collect(Collectors.toList());
System.out.println("Concatenated Strings:");
concatenatedStrings.forEach(System.out::println);
// Step 4: Find the average salary using streams
double averageSalary = employees.stream()
.mapToDouble(Employee::getSalary)
.average()
.orElse(0.0);
System.out.println("Average Salary: " + averageSalary);
// Step 5: Filter employees above a certain age and then generate concatenated strings
int ageThreshold = 30;
List<String> filteredConcatenatedStrings = employees.stream()
.filter(e -> e.getAge() > ageThreshold)
.map(nameAndDepartmentFunction)
.collect(Collectors.toList());
System.out.println("Filtered Concatenated Strings (Age > " + ageThreshold + "):");
filteredConcatenatedStrings.forEach(System.out::println);
}
}
Summary
Purpose of the Function Interface: The Function interface is used to encapsulate a
function that takes an input and produces an output. It is a key component in functional
programming and stream operations.
Characteristics of Streams: Streams offer a functional approach to processing data, with
support for pipelining, lazy evaluation, and parallelism, enhancing efficiency and readability
in data manipulation tasks.
Usage in Data Manipulation: The provided program demonstrates how to use the
Function interface to map employee objects to concatenated strings and how to use
streams to process data efficiently. It includes generating a list of formatted strings,
calculating the average salary, and filtering employees based on age.