← All Exercises

Summarize Salaries by Department

intermediatejava~20min

Background

Downstream collectors turn one grouping into several metrics. summarizingDouble keeps count, sum, min, max, and average in a single pass.

Problem

Given employees with department and salary, return headcount and average salary per department, rounded to two decimals, sorted by average descending then name.

Input

A list of Employee records with a non-null department and a positive salary; departments may contain one or many employees.

Output

A list of Stats records with department, headcount, and averageSalary rounded to two decimals, ordered by average descending then department.

Sample tests

Sample 1

Input: Engineering 120000 and 100000; Sales 90000 and 70000; Support 60000
Expected: Engineering headcount=2 avg=110000.00
Sales headcount=2 avg=80000.00
Support headcount=1 avg=60000.00

Sample 2

Input: only Sales with one salary of 90000
Expected: Sales headcount=1 avg=90000.00

Your solution

Learning objective

Use downstream collectors, round computed averages, and sort aggregated records with a deterministic comparator.