Avatar ·

What is the difference between map() and flatMap() methods in Java 8 Stream?

Hi everyone! I recently started learning Java 8 and am trying to understand the Stream API. I read several articles, but I can't figure out the difference between the `map()` and `flatMap()` methods. I have a simple example. Let's say there is a list of strings: ```java List words = Arrays.asList("Hello", "World"); ``` If I apply `map()` to this list and want to split each string into individual characters: ```java words.stream() .map(word -> word.split("")) .forEach(System.out::println); ``` I get output like `[Ljava.lang.String;@1b6d3586` — some hash codes, not characters. Why does this happen? How can I get a list of characters? I heard that `flatMap()` can help, but I don't understand how exactly. What is the fundamental difference between these methods? When should I use `map()`?