1. How do you group strings by their length?
Use Java Streams to bucket strings by length. This approach is concise, readable, and leverages the Collectors API for efficient grouping in a single pass.
Map> grouped =
list.stream()
.collect(Collectors.groupingBy(String::length));
Tip: For large lists, consider parallelStream() only when the operation is CPU-bound and the stream source is thread-safe.
2. How do you group numbers by even and odd?
Classify numeric values into named buckets (e.g., “Even” and “Odd”) by supplying a classifier function to groupingBy.
Map> grouped =
numbers.stream()
.collect(Collectors.groupingBy(n -> n % 2 == 0 ? "Even" : "Odd"));
SEO note: Labeling buckets improves readability when exposing results in reports or APIs.
3. How do you group employees by department?
Group domain objects by a property reference to create a map keyed by department, useful for reporting, aggregation, or building UI sections.
Map> grouped =
employees.stream()
.collect(Collectors.groupingBy(Employee::getDepartment));
Best practice: Keep the department value normalized (consistent names or IDs) to avoid fragmented buckets.
4. How do you group items by name and count them?
Combine grouping with a downstream collector like counting() to produce frequency maps for analytics or inventory summaries.
Map counted =
items.stream()
.collect(Collectors.groupingBy(Item::getName, Collectors.counting()));
Use case: Produce top-N reports by sorting the map entries by value.
5. How do you group people by age and collect names?
Use a downstream mapping collector to transform grouped Person objects into lists of names, reducing post-processing work.
Map> grouped =
people.stream()
.collect(Collectors.groupingBy(
Person::getAge,
Collectors.mapping(Person::getName, Collectors.toList())
));
Tip: For unique names per age, replace toList() with toSet().
6. How do you group transactions by currency and sum the amount?
Aggregate monetary values per currency using summingDouble (or BigDecimal with a custom collector for precision) to produce robust financial summaries.
Map totals =
transactions.stream()
.collect(Collectors.groupingBy(
Transaction::getCurrency,
Collectors.summingDouble(Transaction::getAmount)
));
Precision note: For money use BigDecimal and Collectors.reducing for accuracy.
7. How do you group books by author and sort titles?
Combine groupingBy with a mapping and a post-collector to return sorted title lists per author in one stream pipeline.
Map> grouped =
books.stream()
.collect(Collectors.groupingBy(Book::getAuthor,
Collectors.mapping(Book::getTitle,
Collectors.collectingAndThen(Collectors.toList(),
list -> list.stream().sorted().collect(Collectors.toList())
)
)
));
Optimization: If many titles exist per author, sort using Collections.sort on the collected list to avoid extra streams.
8. How do you group orders by status and count them?
Produce status-level metrics by counting orders per status — essential for dashboards and SLA reports.
Map statusCount =
orders.stream()
.collect(Collectors.groupingBy(Order::getStatus, Collectors.counting()));
Follow-up: Convert the map to a sorted list to display the most common statuses first.
9. How do you group students by grade and find the top scorer?
Use groupingBy with maxBy to locate the highest-scoring student per grade. Wrap results in Optional to handle empty groups safely.
Map> topScorers =
students.stream()
.collect(Collectors.groupingBy(Student::getGrade,
Collectors.maxBy(Comparator.comparing(Student::getScore))));
Enhancement: Replace Optional with a mapping collector to return a default value or transform to DTOs for presentation.
10. How do you group products by category and collect them into a TreeMap?
Create a sorted map keyed by category by providing a TreeMap supplier to groupingBy; useful when predictable key order improves UX or export formats.
Map> grouped =
products.stream()
.collect(Collectors.groupingBy(Product::getCategory,
TreeMap::new, Collectors.toList()));
SEO/UX benefit: Sorted category output improves discoverability in generated lists and APIs.
Search
Categories
Recent Posts
Top Git interview Questions for Experienced Developer
Speed-up tips and tricks for Windows 11
Top 15 Linux Commands used by Experienced Developer
Top SQL Interview Examples for Experienced Developer
Internal Working of Java HashMap
Recent Tags