How do you use flatMap with a simple code example?

flatMap, Java, Stream API, functional programming
This example demonstrates the use of the flatMap method in Java's Stream API, which allows for flattened results from nested collections.
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class FlatMapExample { public static void main(String[] args) { List> nestedList = Arrays.asList( Arrays.asList("A", "B", "C"), Arrays.asList("D", "E"), Arrays.asList("F", "G", "H") ); List flatList = nestedList.stream() .flatMap(List::stream) .collect(Collectors.toList()); System.out.println(flatList); } }

flatMap Java Stream API functional programming