map() is a Stream operation that transforms each element into a new value using a given function, and applying String::toUpperCase converts every string in a stream to its uppercase form.
Key Points: • map() applies a one-to-one transformation function to each element, producing a new stream of the mapped type. • String::toUpperCase is a method reference that's equivalent to the lambda name -> name.toUpperCase(). • Because streams don't modify the source, the original list of names remains unchanged after mapping. • Method references are preferred over equivalent lambdas here since they're more concise and equally readable. • collect(Collectors.toList()) materializes the transformed stream back into a concrete list.
Example: Given the list ["Alice", "Bob", "Charlie"], mapping with String::toUpperCase produces a new list ["ALICE", "BOB", "CHARLIE"] while leaving the original list untouched.
Code Example:
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
List<String> upperNames = names.stream()
.map(String::toUpperCase)
.collect(Collectors.toList());Interview Tip: A concise interview answer is:
"I use map(String::toUpperCase) to transform each element of the stream into its uppercase form and collect the results into a new list, leaving the original list unchanged since streams don't mutate their source."