<R> Stream<R> flatMap(Function<? super T,? extends Stream<? extends R>> mapper)
Stream.flatMap(...) method returns a stream consisting of the results of replacing
each element of this stream with the contents of a mapped stream produced by applying the provided
mapping function to each element. The function produces a stream for each input
element and the output streams are flattened. Performs one-to-many mapping.
flatMap operation works as follows:
It takes an input stream and produces an output stream using a mapping function.
The mapping function takes an element from the input stream and maps the element to a
stream. The type of input element and the elements in the mapped stream may be different.
This step produces a stream of streams. Suppose the input stream is a
Stream<T> and the mapped stream is
R may be
the same or different.
Finally, it flattens the output stream (that is, a stream of streams) to produce a stream. That
Stream<Stream<R>> is flattened to
Let's look at a simple example. We have got a
Stream of lists of names, and we want all
the names from these in sequences. We can solve this problem using an approach like
the one in example below:
List<String> names1 = Arrays.asList("Dzmitry", "John"); List<String> names2 = Arrays.asList("David", "Laura"); Stream<List<String>> s = Stream.of(names1, names2); s.flatMap(names -> names.stream()).forEach(System.out::println);
Dzmitry John David Laura
We replace the
List<String> with a
Stream<String> using the
stream() method, and
flatMap does the rest. The
functional interface is the same as
map's — the
Function — but its return type
is restricted to streams and not any value.
flatMap(...) transforms each element of a stream into another form (just like
and generates sub-streams of the newly formed elements. Finally, it flattens all of the sub-streams into a single
stream of elements. As the
flatMap(...) is a map type of function, it also takes a function
and applies (maps) that function to each of the element in the stream.
The difference between
map(...) accepts a function that returns a mapped element and then the
function returns a stream of such elements (1 to 1).
flatMap accepts a function that returns streams of the mapped elements and then
flatMap finally returns a collective stream of all of the sub-streams that are
created by the each execution of the passed function (1 to 0...n).
java.util.Map interface has been extended with the
merge function in Java 8:
default V merge(K key, V value, BiFunction<? super V,? super V,? extends V> remappingFunction)
Map.merge(...) method does the following: if the specified key is not already associated with
a value or is associated with
null, associates it with the given value. Otherwise, replaces the value with
the results of the given remapping function, or removes if the result is
The default implementation of
merge is equivalent to performing the following steps for this
map, then returning the current value or
null if absent:
V oldValue = map.get(key); V newValue = (oldValue == null) ? value : remappingFunction.apply(oldValue, value); if (newValue == null) map.remove(key); else map.put(key, newValue);
The default implementation makes no guarantees about synchronization or atomicity properties of this method.