[Go to site: main page, start]

0% found this document useful (0 votes)
3 views12 pages

Java Stream API Interview Guide

Uploaded by

gautamghose1981
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views12 pages

Java Stream API Interview Guide

Uploaded by

gautamghose1981
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Stream API Interview Guide (Senior Java

Developer)

1. Stream Fundamentals
Stream API introduced in Java 8 provides a functional way to process collections. Pipeline = Source
-> Intermediate Operations -> Terminal Operation. Key Concepts: • Lazy Evaluation • Internal
Iteration • Stateless vs Stateful Operations • Sequential vs Parallel Streams Interview Question: Q:
Difference between Collection and Stream? A: Collection stores data; Stream processes data.
2. map(), filter(), flatMap()
map() transforms one object into another. filter() removes unwanted elements. flatMap() flattens
nested collections. Scenario: Employee -> Skills Without flatMap: List> With flatMap: List Interview
Question: Q: Difference between map and flatMap? A: map keeps nesting; flatMap removes
nesting.
3. groupingBy()
groupingBy is one of the most important collectors. Scenario: Group employees by department.
[Link]() .collect([Link](Employee::getDepartment)) Common
downstream collectors: toList() toSet() counting() mapping() summingDouble() averagingDouble()
maxBy() minBy() Interview Question: Q: groupingBy vs partitioningBy? A: groupingBy creates
multiple groups. partitioningBy creates only two groups.
4. partitioningBy()
Creates Map>. Scenario: Partition employees into high-paid and low-paid.
[Link](e -> [Link]() > 100000) Output: true -> High Paid false -> Others
5. Optional
Optional prevents NullPointerException. Methods: [Link]() [Link]()
[Link]() orElse() orElseGet() orElseThrow() ifPresent() Interview Question: Q: Difference
between orElse and orElseGet? A: orElse always evaluates. orElseGet evaluates lazily.
6. reduce()
Used to reduce stream elements into a single value. Examples: Sum Maximum Minimum
Concatenation Scenario: Total salary of employees. [Link]()
.map(Employee::getSalary) .reduce(0.0, Double::sum) Interview Question: Q: Difference between
reduce and collect? A: reduce returns one value; collect returns complex structures.
7. toMap()
Scenario: Convert Employee list into Map [Link]( Employee::getId, [Link]() )
Interview Question: Q: Why does toMap throw IllegalStateException? A: Duplicate keys without
merge function.
8. Parallel Streams
Uses ForkJoinPool. When to use: • Large dataset • CPU intensive work • Stateless operations
Avoid: • Database calls • REST calls • Blocking I/O Interview Question: Q: When not to use parallel
streams? A: For small collections or blocking operations.
9. Concurrent Collectors
groupingByConcurrent() Returns ConcurrentHashMap. Scenario: Group millions of records by
department in parallel. [Link](Employee::getDepartment)
10. Top 20 Interview Scenarios
1. Group orders by customer. 2. Group employees by department. 3. Group students by grade. 4.
Count products per category. 5. Count page views per URL. 6. Sum expenses by department. 7.
Average revenue by tier. 8. Maximum mileage vehicle by model. 9. Customer names by priority. 10.
Transaction amounts by type. 11. Article titles by author. 12. Property zip codes by agent. 13.
Medications by diagnosis. 14. Join chat messages by user. 15. Task duration statistics by status.
16. Cheapest product by category. 17. Usernames by guild. 18. Total nights booked by room type.
19. Highest salary employee by department. 20. Longest employee name by department.
11. Senior-Level Scenario Questions & Answers
Q: How would you find duplicate employees? A: groupingBy(Employee::getId, counting()) and filter
count > 1. Q: How would you flatten employee skills? A: flatMap(Employee::getSkills().stream()). Q:
How would you get top-paid employee? A: max([Link](Employee::getSalary)) Q:
How would you group and count? A: groupingBy(key, counting()) Q: How would you safely handle
null results? A: Optional.
12. Cheat Sheet
map -> Transform filter -> Remove flatMap -> Flatten reduce -> Single Value collect -> Collection
Result groupingBy -> Multiple Groups partitioningBy -> Two Groups toMap -> Convert to Map
Optional -> Null Safety parallelStream -> Parallel Processing

You might also like