Courseiva
Working with Streams and Lambda ExpressionseasyMultiple ChoiceObjective-mapped

1Z0-829 Working with Streams and Lambda Expressions Practice Question

A developer is converting legacy for loops to streams. The legacy code: List<Integer> list = new ArrayList<>(); for (String s : strings) { if (s.length() > 5) { list.add(s.length()); } } They write: List<Integer> list = strings.stream() .filter(s -> s.length() > 5) .map(s -> s.length()) .collect(Collectors.toList()); But it doesn't compile. The error is: 'cannot find symbol: method collect(Collector<Object,?,List<Object>>)'. What is the likely issue?

⚠ Common exam trap

The trap here is that candidates overlook the automatic specialization of `map` to `IntStream` when the lambda returns a primitive `int`, and assume `collect(Collectors.toList())` is always available on any stream, leading them to focus on unrelated options like parallelism or ordering.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

The map operation returns an IntStream, which does not have a collect method. Use map(s -> s.length()).boxed().collect(...) or mapToInt(...).boxed().

The issue is that `map(s -> s.length())` on a `Stream<String>` returns an `IntStream`, not a `Stream<Integer>`. The `IntStream` interface does not have a `collect(Collector)` method; it only has `collect(Supplier, BiConsumer, BiConsumer)`. To use `Collectors.toList()`, you must convert the `IntStream` back to a `Stream<Integer>` via `.boxed()`, or use `mapToInt(...).boxed()`, or use `map(s -> (Integer) s.length())` to keep a `Stream<Integer>`.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • The lambda in filter is incorrectly written; it should be s.length > 5.

    Why it's wrong here

    The lambda syntax is correct; the error is not about the filter.

  • The map operation returns an IntStream, which does not have a collect method. Use map(s -> s.length()).boxed().collect(...) or mapToInt(...).boxed().

    Why this is correct

    s.length() returns int, so map produces an IntStream. To collect to List<Integer>, you need to box to Stream<Integer>.

  • The stream should be made unordered to allow the collector to function.

    Why it's wrong here

    Unordered does not affect collect availability.

  • Use parallelStream() to enable the collect method.

    Why it's wrong here

    Parallelism does not affect the availability of collect.

About these practice questions

One of 513 original 1Z0-829 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This 1Z0-829 practice question is part of Courseiva's free Oracle certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 1Z0-829 exam.