STUDYECRAT Java : Stream API
60s
Java Stream API
Prove your skills in this interactive quiz
Live Code
Run snippets directly
Timed
60s per question
Scored
Earn 3D badges
★ Java 8 Stream API: Key Interview Points
1. reduce() vs. collect()
List<Integer> nums = Arrays.asList(1, 2, 3);
int sum = nums.stream().reduce(0, (a, b) -> a + b);
List<Integer> doubled = nums.stream().collect(Collectors.toList());
// reduce() aggregates; collect() transforms to a container
int sum = nums.stream().reduce(0, (a, b) -> a + b);
List<Integer> doubled = nums.stream().collect(Collectors.toList());
// reduce() aggregates; collect() transforms to a container
- Tip: Use
reduce()
for immutable operations (e.g., sum). Usecollect()
for mutable results (e.g., lists). - Real Use:
reduce()
is common in financial calculations (e.g., portfolio totals).
2. Stream Laziness & Terminal Operations
Stream<Integer> stream = Stream.of(1, 2, 3).filter(n -> {
System.out.println("Filtering: " + n);
return n > 1;
});
// No output until terminal op (e.g., count()) is called
System.out.println("Filtering: " + n);
return n > 1;
});
// No output until terminal op (e.g., count()) is called
- Tip: Intermediate ops (e.g.,
filter()
) execute only after terminal ops (e.g.,forEach()
). - Real Use: Debugging lazy streams requires adding
peek()
to log values.
3. flatMap() for Nested Collections
List<List<String>> nested = Arrays.asList(
Arrays.asList("a", "b"),
Arrays.asList("c", "d"));
List<String> flat = nested.stream()
.flatMap(List::stream)
.collect(Collectors.toList());
// Converts [[a,b], [c,d]] → [a,b,c,d]
Arrays.asList("a", "b"),
Arrays.asList("c", "d"));
List<String> flat = nested.stream()
.flatMap(List::stream)
.collect(Collectors.toList());
// Converts [[a,b], [c,d]] → [a,b,c,d]
- Tip: Use
flatMap()
to merge Kafka topic partitions or nested JSON arrays. - Real Use: Processing database results with JOINs (e.g., orders and order items).
4. Optional with Streams
List<Integer> nums = Arrays.asList(1, 2, 3);
Optional<Integer> firstEven = nums.stream()
.filter(n -> n % 2 == 0)
.findFirst();
System.out.println(firstEven.orElse(-1));
// Safely handles empty results
Optional<Integer> firstEven = nums.stream()
.filter(n -> n % 2 == 0)
.findFirst();
System.out.println(firstEven.orElse(-1));
// Safely handles empty results
- Tip: Always use
orElse()
/orElseThrow()
withfindFirst()
to avoidNoSuchElementException
. - Real Use: API responses where a field might be missing (e.g., user addresses).
5. Parallel Stream Pitfalls
List<Integer> nums = Arrays.asList(1, 2, 3);
int sum = nums.parallelStream()
.reduce(0, (a, b) -> a + b);
// Thread-safe for associative ops (e.g., sum)
int sum = nums.parallelStream()
.reduce(0, (a, b) -> a + b);
// Thread-safe for associative ops (e.g., sum)
- Tip: Avoid
parallelStream()
with stateful ops (e.g.,sorted()
) or non-thread-safe collectors. - Real Use: Batch processing large datasets (e.g., log analysis).
💡 Pro Interview Tip
When asked about Streams, always mention lazy evaluation and immutability. Interviewers often test if you understand these core concepts.