Flattening a List of List to a List with Java 8 stream API

how to iterate list inside list in java 8
java 8 flatmap
java-stream nested list
get list from list java 8
java 8 flatten list of lists
java collect a list of lists
java 8 convert list of list to single list
convert list of list into single list java 8

I have the following code which could be much simpler using Java 8 stream API:

List<List<String>> listOfListValues;
public List<String> getAsFlattenedList() {
        List<String> listOfValues= new ArrayList<>();
        for (List<String> value: listOfListValues) {
            listOfValues.add(String.valueOf(value));
        }
        return listOfValues;
    }

I searched for a solution on SO and found this:

listOfListValues.stream()
        .flatMap(List::stream)
        .collect(Collectors.toList());

But this doesn't do the same what I want.

Flattening a List of List to a List with Java 8 stream API, You require only a "simple" map here: List<List<String>> listOfListValues; public List<String> getAsFlattenedList() { return  In order to flatten this nested collection into a list of strings, we can use forEach together with a Java 8 method reference: public <T> List<T> flattenListOfListsImperatively( List<List<T>> nestedList) { List<T> ls = new ArrayList<>(); nestedList.forEach(ls::addAll); return ls; }

No need to use flatmap.

listOfListValues.stream()
        .map(String::valueOf)
        .collect(Collectors.toList());

Explanation: The flatMap function combines a map and a flat operation. This is not needed. Flattening means converting something like [ [1,2,3],[4,5,6,7],[8,9] ] to [ 1,2,3,4,5,6,7,8,9 ] i.e. converting a 2D array to a 1D array.

How can I turn a List of Lists into a List in Java 8?, You can use flatMap to flatten the internal lists (after converting them to Streams) into a single Stream, and then collect the result into a list: Example 3 – Combine flatMap () with reduce () Example: List<List<Integer>> -> Stream -> FlatMap -> Stream -> Reduce -> Int. Code: List<List<Integer>> listOfList = Arrays.asList(. Arrays.asList(1, 2, 3, 4), Arrays.asList(5, 6, 7, 8), Arrays.asList(9, 10, 11, 12) );

You can do it like so,

listOfListValues.stream().map(List::toString).collect(Collectors.toList());

How to Flatten Streams using flatMap() method in Java 8 and above , an object array. Stream<List<T>> – Stream where each element is a List. The flatMap() method can be used for flattening Streams in Java as shown below:​. Flattening is referred by converting several lists of lists, and merge all those lists to create single list containing all the elements from all the lists. Flattening example. Before flattening : [ [1, 2, 3], [4, 5], [6, 7, 8]] After flattening : [1, 2, 3, 4, 5, 6, 7, 8] 2. Stream.flatMap () method. 2.1.

You can use a flatMap here:

  List<List<Integer>> listOfLists = new ArrayList<>();
    listOfLists.add(Arrays.asList(1,2,3,4,5));
    listOfLists.add(Arrays.asList(10,20,30,40));
    listOfLists.add(Arrays.asList(100,200,300,400));

    System.out.println(listOfLists);

    List<Integer> collect = listOfLists.stream().flatMap(Collection::stream).collect(Collectors.toList());
    System.out.println(collect);

And the output is: [[1, 2, 3, 4, 5], [10, 20, 30, 40], [100, 200, 300, 400]]

[1, 2, 3, 4, 5, 10, 20, 30, 40, 100, 200, 300, 400]

Flatten a Stream of Lists in Java using forEach loop, Given a Stream of Lists in Java, the task is to Flatten the Stream using forEach() method. Examples: Input: lists = [ [1, 2], [3, 4, 5, 6], [8, 9] ] Output: [1, 2, 3, 4, 5, 6,  Using Java 8 and if you prefer not to instantiate a List instance by yourself, like in the suggested (and accepted) solution. someMap.values().forEach(someList::addAll); You could do it all by streaming with this statement: List<String> someList = map.values().stream().flatMap(c -> c.stream()).collect(Collectors.toList());

Java Stream flatMap() method example, Learn to use Java Stream flatMap() method which is used to flatten a stream of collections to a stream By Lokesh Gupta | Filed Under: Java 8 Flattening is referred by converting several lists of lists, and merge all those lists to create single Drop me your questions related to Stream flatMap() method in Java Stream API. how to write the code below in java 8 using streams to avoid nested loop : C c1 = null;String name = "name1"for (A a: listOfAObjects) { for (B b: a.getList()) { for (C c: b.getPr()) { if (c.getName().equalsIgnoreCase(name)) { c1= c; break; } } }}

Java, This article assumes some familiarity with Java 8 Stream API. If we use the map method, we will get a stream of name lists, not a stream of  Learn to convert stream to list using Collectors.toList() and Collectors.toCollection() APIs. This post contains multiple examples for collecting stream elements to list under different usecases. 1. Convert stream of strings to ArrayList. Java 8 example to convert stream to ArrayList using using Collectors.toList() method.

Merging nested Lists or Arrays with Java 8, Merging nested Lists or Arrays with Java 8. Posted by Benjamin Muskalla. When accessing 3rd party APIs with pagination, we tend to see the same actually merge/flatten all those nested lists, you can use Stream#flatMap. The List interface provides four methods for positional (indexed) access to list elements. Lists (like Java arrays) are zero based. Note that these operations may execute in time proportional to the index value for some implementations (the LinkedList class, for example). Thus, iterating over the elements in a list is typically preferable to

Comments
  • No need to use flatmap. Detailed answer below. Like and accept whichever you find useful.
  • If I try to print your list like this: System.out.println(listOfLists.stream().map(List::toString).collect(Collectors.toList())); I get the following output: [[1, 2, 3, 4, 5], [10, 20, 30, 40], [100, 200, 300, 400]]