map() — one-to-one transformation. flatMap() — one-to-many, flattens nested structures. Think of map() as a machine that replaces each item with one new item, while flatMap() replaces each item with a bag of items, then pours all bags into one pile.
map():
Stream<R>.1Stream<Integer> lens = words.stream()2 .map(String::length);3// ["hello", "world"] → [5, 5]
flatMap():
1Stream<String> chars = words.stream()2 .flatMap(w -> Arrays.stream(w.split("")));3// ["ab", "cd"] → ["a", "b", "c", "d"]
Key difference: