map() applies a function to every element of a stream and returns a new stream of the results. Passing String::toUpperCase converts each String to upper case, and collect() gathers the results into a new list.
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class ToUpperCase {
public static void main(String[] args) {
List<String> names = Arrays.asList("aa", "bb", "cc", "dd");
List<String> upper = names.stream()
.map(String::toUpperCase)
.collect(Collectors.toList());
System.out.println(upper);
System.out.println(names);
}
}[AA, BB, CC, DD]
[aa, bb, cc, dd]
A typical follow-up is "What is the difference between map() and flatMap()?" map() turns each element into one value. flatMap() turns each element into a stream and flattens all of them into one stream, for example turning a List<List<String>> into a List<String>.
A concise interview answer is
I use map(String::toUpperCase) to transform each element and collect the results into a new list. map is a one-to-one transformation and doesn't change the original list, whereas flatMap is used when each element produces several values.
Last updated: 11 November 2026
Previous question: Write a Java 8 program to sort an array and convert it into a stream.
Next question: How do you convert a list of objects into a sorted map while handling duplicate keys?