Avatar ·

How to convert a list of lists (List<List<T>>) to a flat list (List<T>) in Java 8?

Hello everyone! I've just started learning Java 8 and the Stream API, and I've encountered a task. I have a data structure that is a list of lists of strings. For example, I'm getting data from a CSV file, where each row is a list of fields, and the entire file is a list of such rows. I need to merge all these rows into one common list, i.e., "flatten" the nested structure. Here's a simplified example of what I have: ```java List> listOfLists = Arrays.asList( Arrays.asList("a", "b", "c"), Arrays.asList("d", "e"), Arrays.asList("f", "g", "h", "i") ); ``` I want to get the output: ```java List flatList = Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h", "i"); ``` I know that in old Java (before 8) this could be done using