0% found this document useful (0 votes)
2 views

Skill Week -8_Functional Interfaces, Lambda Expressions, Map and Stream API

The document covers advanced Java concepts including custom functional interfaces, lambda expressions, and the Stream API for filtering and processing employee data, transactions, and student grades. It details the creation of various classes and methods to implement dynamic filtering, grouping, and aggregation of data, as well as higher-order functions and parallel stream processing. Key concepts such as method references, custom collectors, and efficient data handling are emphasized throughout the examples.

Uploaded by

hope98754
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Skill Week -8_Functional Interfaces, Lambda Expressions, Map and Stream API

The document covers advanced Java concepts including custom functional interfaces, lambda expressions, and the Stream API for filtering and processing employee data, transactions, and student grades. It details the creation of various classes and methods to implement dynamic filtering, grouping, and aggregation of data, as well as higher-order functions and parallel stream processing. Key concepts such as method references, custom collectors, and efficient data handling are emphasized throughout the examples.

Uploaded by

hope98754
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Week 8.

Functional Interfaces, Lambda Expressions, Map and Stream API

1) Custom Functional Interface with Complex Predicate Filtering.


Filter employees based on multiple dynamic criteria such as salary range,
department, and years of experience using a custom functional interface, lambda
expressions, and the Stream API.
1 Define the Employee class: This class will represent each employee with fields
like name, salary, department, and yearsOfExperience.
2 Create a Functional Interface: Define a functional interface that will allow for
dynamic filtering based on employee attributes.
3 Implement Filtering Logic: Using the Stream API, we will dynamically filter the
employee list based on the provided lambda expressions.

Explanation:

1 Employee Class: Represents employees with fields like name, salary,


department, and yearsOfExperience.
2 EmployeeFilter Interface: This custom functional interface is used to define the
filtering logic via lambdas.
3 filterEmployees Method: This method accepts a list of employees and an
EmployeeFilter functional interface. The Stream API is used to filter the
employees based on the lambda expression provided as the filter.
4 Lambda Expressions:
 highSalaryFilter: Filters employees with a salary greater than 80,000.
 engineeringDeptFilter: Filters employees belonging to the "Engineering"
department.
 experiencedFilter: Filters employees with 7 or more years of experience.
5 Combining Filters: The filters are combined using logical AND (&&). This allows
you to dynamically compose multiple filter conditions.

2) Combining Stream Operations with Method References and Lambdas.


You have a list of transactions, where each transaction has an amount and a
category (like "groceries," "utilities," etc.). You need to generate a report that groups
transactions by category and calculates the total amount spent in each category
using the Stream API, combining both method references and lambda expressions.

1 Transaction Class:
 This class represents each transaction with two fields: amount (of type
double) and category (of type String).
 There are two getter methods, getAmount() and getCategory(), used for
accessing these fields.
2 Stream Operations:
 The transactions.stream() method initiates a stream pipeline on the list of
transactions.
 We use Collectors.groupingBy() to group the transactions by category.
 The first argument to groupingBy() is a method reference
Transaction::getCategory, which extracts the category of each
transaction.
 The second argument is a collector Collectors.summingDouble(), which
sums the amounts of transactions. This is achieved using a lambda
expression (t -> t.getAmount()) that extracts the amount of each
transaction.
3 Result:
 The transactions are grouped by their category, and the sum of the
transaction amounts is computed for each category.
 Finally, the forEach() method is used to print the total amount for each
category.

Key Concepts Used:


 Method Reference: Transaction::getCategory to group by category.
 Lambda Expression: (t -> t.getAmount()) to sum the transaction amounts.
 Stream API: To process the transactions efficiently and perform grouping
and aggregation.

3) Higher-order Function with Functional Interfaces


Create a higher-order function that takes two functional interfaces:
1. A Function<Integer, Integer> to transform a list of integers.
2. A Predicate<Integer> to filter the integers after transformation.
We will then use the Stream API to apply these transformations and filters on a list
of integers.

1 Higher-order function transformAndFilter: This method accepts a list of integers,


a Function<Integer, Integer> (for transformation), and a Predicate<Integer> (for
filtering). It uses the Stream API to apply the transformation (map) and filtering
(filter) operations in sequence and returns the result as a list.
2 Lambda expressions:
 The squareFunction lambda squares each integer.
 The isEvenPredicate lambda filters out odd numbers and retains only even
ones.
3 Stream operations:
 map(transformer) applies the transformation (squaring the number).
 .filter(filter) applies the filter to keep only even numbers.
4) Parallel Stream with Custom Collector and Lambdas
Calculate the average grade by subject from the large set of student grades. The
process should be efficient, so you decide to use a parallel stream. Create a custom
Collector that uses a lambda expression to handle the accumulation and reduction
phases in parallel.
Objective: Implement a custom Collector using lambdas to compute the average
grade per subject in parallel using the Stream API.
1 Define the Data Structure: Create a StudentGrade class to hold the data (subject
and grade).
2 Create a Custom Collector: Implement a collector to accumulate grades and
calculate averages.
3 Parallel Stream Processing: Use the parallel stream to ensure the operation is
performed efficiently.

Explanation

1 StudentGrade Class: Holds data for each grade entry, including the subject and
the grade.
2 AverageGradeCollector Class:
 supplier: Provides a new HashMap to collect grades by subject.
 accumulator: Adds grades to the list corresponding to each subject.
 combiner: Merges two maps by combining lists of grades.
 finisher: Computes the average grade for each subject from the collected
lists.
3 Parallel Stream Processing:
 Use parallelStream() to process the grades list in parallel.
 Collect results using AverageGradeCollector.

You might also like