I understand the concept of flatMap in functional programming kind of, but I am totally lost about it Java. For example:
public class TransformationsWithFlatMap {
private static final List<List<String>> arrayListOfNames = List.of(
List.of("Mariam", "Alex", "Ismail"),
List.of("John", "Alesha", "Andre"),
List.of("Susy", "Ali")
);
public void withFlatMap() throws Exception {
// [Mariam, Alex, Ismail, John, Alesha, Andre, Susy, Ali]
Function<List<String>, Stream<? extends String>> stream = List::stream;
List<String> result = arrayListOfNames.stream().flatMap(stream).collect(Collectors.toList());
}
}
First of all, flatMap seem to expect ( java.util.function.Function<? super T, ? extends Stream<? extends R>> mapper ) when we use it for anything. I have no idea what that means.
And then if I insert List::stream (according to some tutorial) that is equal to Function<List<String>, Stream<? extends String>>. This seem like chinese to me.
How can I understand flatMap in Java streams better, and be able to read complex types like the one shown here?