flatten a nested List of List - Java java.util

Java examples for java.util:List Flatten

Description

flatten a nested List of List

Demo Code


//package com.java2s;
import java.util.ArrayList;

import java.util.List;

public class Main {
    public static void main(String[] argv) {
        List list = java.util.Arrays.asList("asdf", "java2s.com");
        System.out.println(flatten(list));
    }//from  ww w.j  a v a2  s .c  om

    public static <A> List<A> flatten(List<List<A>> list) {
        List<A> r = new ArrayList<A>();
        for (List<A> l : list) {
            for (A a : l) {
                r.add(a);
            }
        }
        return r;
    }
}

Related Tutorials