Bird
0
0

Given a list of strings representing numbers, which code snippet correctly converts them to a list of Integer objects using wrapper methods?

hard📝 Application Q8 of 15
Java - Wrapper Classes
Given a list of strings representing numbers, which code snippet correctly converts them to a list of Integer objects using wrapper methods?
List strNums = List.of("1", "2", "3");
// Fill in the blank
List intNums = strNums.stream()._____.collect(Collectors.toList());
Amap(String::valueOf)
BmapToInt(Integer::parseInt)
Cmap(Integer::valueOf)
Dmap(Double::valueOf)
Step-by-Step Solution
Solution:
  1. Step 1: Understand conversion from String to Integer object

    Integer.valueOf converts String to Integer object, suitable for mapping.
  2. Step 2: Check other options

    Integer.parseInt returns int primitive, not Integer object. String.valueOf returns String. Double.valueOf returns Double object, not Integer.
  3. Final Answer:

    map(Integer::valueOf) -> Option C
  4. Quick Check:

    Use Integer.valueOf for String to Integer object mapping [OK]
Quick Trick: Use Integer.valueOf in streams to get Integer objects [OK]
Common Mistakes:
  • Using parseInt which returns primitive int
  • Mapping to String instead of Integer
  • Using Double.valueOf for integer conversion

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes