Java Streams Intermediate Operations Interview Questions and Answers
Master Java Streams Intermediate Operations with production-ready interview questions covering filter, map, flatMap, distinct, sorted, peek, limit, skip, lazy evaluation, stateful vs stateless operations, and enterprise use cases.
Java Streams Intermediate Operations Interview Questions & Answers
Introduction
Intermediate operations are the building blocks of a Stream pipeline.
They transform a stream into another stream, allowing multiple operations to be chained together before a terminal operation executes the pipeline.
Examples include:
filter()map()flatMap()distinct()sorted()peek()limit()skip()
These operations are heavily used in enterprise applications for data filtering, transformation, enrichment, validation, and reporting.
1. What are Intermediate Operations?
Answer
Intermediate operations transform one stream into another.
Characteristics
- Return another
Stream - Are lazily evaluated
- Can be chained
- Do not execute until a terminal operation is invoked
Illustration
Collection
↓
Stream
↓
filter()
↓
map()
↓
sorted()
↓
Terminal Operation
Intermediate operations prepare the pipeline.
2. What is filter()?
Answer
filter() selects elements that satisfy a condition.
Example
List<String> names =
List.of("Alice", "Bob", "Andrew");
List<String> result =
names.stream()
.filter(n -> n.startsWith("A"))
.toList();
Output
Alice
Andrew
filter() reduces the number of elements in the stream.
3. What is map()?
Answer
map() transforms each element into another value.
Example
List<String> upper =
names.stream()
.map(String::toUpperCase)
.toList();
Output
ALICE
BOB
Illustration
Alice
↓
ALICE
map() performs one-to-one transformations.
4. What is flatMap()?
Answer
flatMap() converts each element into a stream and then flattens all streams into one.
Example
List<List<String>> data =
List.of(
List.of("A", "B"),
List.of("C", "D"));
List<String> result =
data.stream()
.flatMap(List::stream)
.toList();
Output
A
B
C
D
Illustration
List<List>
↓
flatMap()
↓
Single Stream
flatMap() is useful for nested collections.
5. What is distinct()?
Answer
distinct() removes duplicate elements.
Example
List<Integer> numbers =
List.of(1,2,2,3,3,4);
List<Integer> result =
numbers.stream()
.distinct()
.toList();
Output
1
2
3
4
Internally, distinct() uses equality (equals() and hashCode()).
6. What is sorted()?
Answer
sorted() sorts stream elements.
Natural order
numbers.stream()
.sorted()
.toList();
Custom comparator
employees.stream()
.sorted(
Comparator.comparing(
Employee::getSalary))
.toList();
Sorting is a stateful intermediate operation because it requires processing all elements before producing ordered output.
7. What is peek()?
Answer
peek() performs an action on each element without modifying it.
Example
names.stream()
.peek(System.out::println)
.map(String::toUpperCase)
.toList();
Typical use
- Debugging
- Logging
Avoid using peek() for business logic or side effects.
8. What are limit() and skip()?
Answer
limit() restricts the number of elements.
numbers.stream()
.limit(5)
.toList();
skip() ignores the first elements.
numbers.stream()
.skip(5)
.toList();
Illustration
1 2 3 4 5 6 7
↓
skip(2)
↓
3 4 5 6 7
↓
limit(3)
↓
3 4 5
These are commonly used for pagination.
9. What is the difference between map() and flatMap()?
Answer
map() |
flatMap() |
|---|---|
| One-to-one transformation | One-to-many transformation |
| Returns one object | Returns a Stream |
| Produces nested structure | Flattens nested streams |
| Used for simple conversion | Used for nested collections |
Example
map()
List<List>
↓
List<List>
-----------------
flatMap()
List<List>
↓
List
10. What is the difference between Stateless and Stateful Intermediate Operations?
Answer
Stateless Operations
Each element is processed independently.
Examples
filter()map()peek()flatMap()
Stateful Operations
Require information about multiple elements.
Examples
sorted()distinct()
Illustration
Stateless
↓
Each Element
Independent
--------------------
Stateful
↓
Need Entire Stream
Stateful operations generally require more memory.
11. Explain a production use case.
Answer
Scenario
A Spring Boot application retrieves customer orders.
Requirement
- Completed orders only
- Remove duplicates
- Sort by amount
- Return customer names
Example
List<String> customers =
orders.stream()
.filter(Order::isCompleted)
.distinct()
.sorted(
Comparator.comparing(
Order::getAmount))
.map(Order::getCustomerName)
.toList();
Workflow
Orders
↓
Filter
↓
Distinct
↓
Sort
↓
Map
↓
Response
The pipeline is easy to read and maintain.
12. What are common mistakes?
Answer
Common mistakes include:
Using peek() for business logic.
Confusing map() with flatMap().
Sorting unnecessarily.
Calling expensive operations before filter().
Ignoring lazy evaluation.
Creating overly long Stream pipelines.
Always place inexpensive filtering operations as early as possible.
13. What are the best practices?
Answer
Recommended practices
- Filter early to reduce processing.
- Use
map()for one-to-one transformation. - Use
flatMap()for nested collections. - Avoid side effects inside intermediate operations.
- Keep pipelines readable.
- Use method references where appropriate.
- Minimize stateful operations.
- Profile before optimizing parallel execution.
14. Which intermediate operations are most commonly used?
Answer
| Operation | Purpose |
|---|---|
filter() |
Select elements |
map() |
Transform elements |
flatMap() |
Flatten nested structures |
distinct() |
Remove duplicates |
sorted() |
Sort elements |
peek() |
Debug or inspect elements |
limit() |
Restrict results |
skip() |
Ignore initial elements |
These operations form the core of most Stream pipelines.
15. What interview tips should you remember?
Answer
Interviewers commonly ask:
- Intermediate operations
filter()map()flatMap()distinct()sorted()peek()limit()skip()- Stateful vs Stateless operations
Remember
- Intermediate operations return another Stream.
- They are lazily evaluated.
filter()selects elements.map()transforms elements.flatMap()flattens nested streams.distinct()removes duplicates.sorted()is stateful.- Keep pipelines readable and efficient.
Summary
Intermediate operations form the heart of Java Stream pipelines. They enable filtering, transformation, flattening, sorting, deduplication, and pagination while leveraging lazy evaluation for efficient processing. Understanding when to use each operation, how they behave, and their performance characteristics is essential for writing clean, maintainable, and production-ready Java code.
Key Takeaways
- Understand intermediate operations.
- Learn
filter(),map(), andflatMap(). - Understand
distinct()andsorted(). - Use
peek()only for debugging. - Learn
limit()andskip(). - Understand lazy evaluation.
- Compare stateless and stateful operations.
- Build efficient Stream pipelines.
- Follow Stream best practices.
- Support interview answers with real production examples.