How to convert a List of Lists to a List in Java 8

In this section, we will show you how to convert a List<List<Object>> to a List<Object> in Java 8 using Stream API.

Following ways can be used for converting List<List<Object>> to a List<Object>

1. By using flatMap () method 
2. By using forEach () method
3. By using mapMult () method


Example 1: Use flatMap () method

Java 8 Stream flatMap () method is used to flatten a Stream of collections to a stream of objects. The objects are combined from all the collections in the original Stream. Flattening is referred to as merging multiple collections/arrays into one.

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class Main {

// Driver code
public static void main(String[] args)
{
//Creating List of Lists
List<List<String>> listOfLists = Collections.
singletonList(Arrays.
asList("Java", "Go", "python", "kotlin"));

/*
Use flatMap to flatten the internal lists
(after converting them to Streams) into a single Stream,
and then collect the result into a list:use flatMap
to flatten the internal lists (after converting them to Streams)
into a single Stream, and then collect the result into a list:
*/
List<String> list = listOfLists.stream().
flatMap(List::stream).collect(Collectors.toList());

System.out.println("listOfLists => " + listOfLists);
System.out.println("list => " + list);

}

}

Console Output:
listOfLists => [[Java, Go, python, kotlin]]
list => [Java, Go, python, kotlin]

Example 2: Use forEach () method

The forEach method performs the specified action on each element of an iterable object until all elements are processed, or the action throws an exception.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Main {

// Driver code
public static void main(String[] args)
{
//Creating List of Lists
List<List<String>> listOfLists = Collections.
singletonList(Arrays.
asList("Java", "Go", "python", "kotlin"));

List<Object> list = new ArrayList<>();

//List is Iterable, this code calls the
// forEach method (Java 8 feature),
// which is inherited from Iterable.
listOfLists.forEach(list::addAll);

System.out.println("listOfLists => " + listOfLists);
System.out.println("list => " + list);

}

}

Console Output:
listOfLists => [[Java, Go, python, kotlin]]
list => [Java, Go, python, kotlin]

Example 3: Since java-16, we can use mapMulti () method

Java 16 introduces a new Stream.mapMulti method which allows us to replace elements in a stream with multiple elements.

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;

public class Main {

// Driver code
public static void main(String[] args)
{
//Creating List of Lists
List<List<String>> listOfLists = Collections.
singletonList(Arrays.
asList("Java", "Go", "python", "kotlin"));


List<String> list = listOfLists.stream()
.mapMulti((List<String> r, Consumer<String> consumer) -> {
r.forEach(consumer::accept);
})
.collect(Collectors.toList());

System.out.println("listOfLists => " + listOfLists);
System.out.println("list => " + list);

}

}

Console Output:
listOfLists => [[Java, Go, python, kotlin]]
list => [Java, Go, python, kotlin]

More related topics,

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete