Skip to content
Advertisement

Does flatMap method only flattens the stream and not map it?

I see it’s written everywhere that flatMap flattens and map the stream but in practical for example in the below program using only flatMap we can’t perform mapping operation(changing it to uppercase). We need to separately use map method here.

List<String> uppercase = Stream.of(asList("A", "B"), asList("C", "D"))
            .flatMap(List::stream)
            .map(String::toUpperCase)
            .collect(Collectors.toList());
    System.out.println(uppercase);

Is my understanding about flatMap wrong? Can anyone please help to understand this – whether flatMap only flattens the stream or it can flattens & map both? If so then how can we write code for that?

Advertisement

Answer

It’s only those specific method references that limit you. Using a lambda expression, you can still do both:

.flatMap(list -> list.stream().map(String::toUpperCase))
.collect(Collectors.toList());

I should mention that it’s only that sequence of method references that limited you, not that it’s impossible to do it with method references. The key is the mapper you pass to it. Take these as examples:

Stream<String> uppercaseArray(String[] a) {
    return Arrays.stream(a).map(String::toUpperCase);
}
Stream.of(new String[] {"ab"}, new String[] {"cd", "ef"})
    .flatMap(this::uppercaseArray); //map and transform

// Or a completely different perspective
Stream.of("abc", "def").flatMapToInt(String::chars);
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement