java – I want to combine lists with stream

Question: Question:

[[a, a, a], [b, b, b], [c, c, c]]
[a, a, a, b, b, b, c, c, c]
I want to do this, but is there a way to do it with stream in one shot?

List innerA = Arrays.asList("a","a","a");
List innerB = Arrays.asList("b","b","b");
List innerC = Arrays.asList("c","c","c");
List<List> outer = Arrays.asList(innerA,innerB,innerC);
//[[a, a, a], [b, b, b], [c, c, c]]

List innerABC = new ArrayList<>();
for(List inner : outer){

Answer: Answer:

You may be able to use Stream # flatMap (…) .

import java.util.*;
import java.util.function.*;

public class StreamTest {
  public static void main(String... args) {
    List<String> innerA = Arrays.asList("a", "a", "a");
    List<String> innerB = Arrays.asList("b", "b", "b");
    List<String> innerC = Arrays.asList("c", "c", "c");
    List<List<String>> outer = Arrays.asList(innerA, innerB, innerC);
    //List innerABC = new ArrayList<>();
    //for(List inner : outer){
    //    innerABC.addAll(inner);
    List<String> innerABC =
      .flatMap(lst ->

Scroll to Top