What is the difference between map() and flatMap()? - Cracking the Java Coding Interview

preview_player
Показать описание
Cracking the #Java #Coding #Interview - Question 72: What is the difference between map() and flatMap()?
Рекомендации по теме
Комментарии
Автор

A example where a stream of one element yields in a result of zero elements:

larryd
Автор

The example for flatmap is weird. Wouldn't it be better to map and a collector which addAll the lists together?

larryd
Автор

Personally, I found the explanation a bit confusion. So if it helps anybody: flatMap is like concatMap in Haskell, mapcan in Lisp or mapcat in Clojure.

TheDarkOne
Автор

Easy - one's flat, one's not

jordanzimmerman
Автор

You can also implement `filter` using `flatMap`: stream.flatMap(x -> predicate.test(x) ? Stream.of(x) : Stream.empty());
(but don't try this at home, kids)

jay_
visit shbcf.ru