Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Interview question

What is the difference between map() and flatMap() in Java Streams? Java Streams में map() और flatMap() में क्या अंतर है?

Answer
MethodTransforms toUse case
map()Stream<R> - one output per input, may be nestedSimple 1-to-1 transformation
flatMap()Stream<R> - flattens nested streams into oneWhen each input produces MULTIPLE outputs (or a stream of them)
import java.util.stream.*;

// map() - simple 1-to-1 transformation
List<String> words = List.of('hello', 'world');
List<Integer> lengths = words.stream()
    .map(String::length)
    .collect(Collectors.toList());
System.out.println(lengths);  // [5, 5]

// PROBLEM: map() with a function returning a Stream creates NESTED streams
List<List<Integer>> nested = words.stream()
    .map(word -> word.chars().boxed().collect(Collectors.toList()))
    .collect(Collectors.toList());
// Result: List<List<Integer>> - a list of lists, not flattened

// flatMap() - flattens the nested structure into a single stream
List<String> sentences = List.of('Hello World', 'How are you');
List<String> allWords = sentences.stream()
    .flatMap(sentence -> Arrays.stream(sentence.split(' ')))  // each sentence -> multiple words
    .collect(Collectors.toList());
System.out.println(allWords);  // [Hello, World, How, are, you]

// Practical example: flattening a list of lists
List<List<Integer>> listOfLists = List.of(
    List.of(1, 2, 3),
    List.of(4, 5),
    List.of(6)
);
List<Integer> flatList = listOfLists.stream()
    .flatMap(List::stream)  // each inner list becomes a stream, all merged into one
    .collect(Collectors.toList());
System.out.println(flatList);  // [1, 2, 3, 4, 5, 6]

// Rule of thumb: use flatMap() whenever your mapping function
// itself returns a Stream/Collection that needs to be merged, not nested
Methodक्या transform करता हैUse case
map()एक input, एक outputSimple 1-to-1 transformation
flatMap()Nested streams को flatten करता हैहर input से multiple outputs
import java.util.stream.*;

List<String> words = List.of('hello', 'world');
List<Integer> lengths = words.stream()
    .map(String::length)
    .collect(Collectors.toList());
System.out.println(lengths);  // [5, 5]

// Problem: map() से nested streams बनते हैं
List<List<Integer>> nested = words.stream()
    .map(word -> word.chars().boxed().collect(Collectors.toList()))
    .collect(Collectors.toList());

// flatMap() - nested structure को flatten करता है
List<String> sentences = List.of('Hello World', 'How are you');
List<String> allWords = sentences.stream()
    .flatMap(sentence -> Arrays.stream(sentence.split(' ')))
    .collect(Collectors.toList());
System.out.println(allWords);  // [Hello, World, How, are, you]

List<List<Integer>> listOfLists = List.of(
    List.of(1, 2, 3),
    List.of(4, 5),
    List.of(6)
);
List<Integer> flatList = listOfLists.stream()
    .flatMap(List::stream)
    .collect(Collectors.toList());
System.out.println(flatList);  // [1, 2, 3, 4, 5, 6]

Was this answer clear?