Flattens the provided list into a single list. - Java java.util

Java examples for java.util:List Operation

Description

Flattens the provided list into a single list.

Demo Code


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

public class Main {
    public static void main(String[] argv) {
        List lists = java.util.Arrays.asList("asdf", "java2s.com");
        System.out.println(flatten(lists));
    }// w  w  w  .ja  v a 2  s .c o  m

    /**
     * Flattens the provided list into a single list.
     * 
     * @param lists
     * @return
     */
    public final static <V> List<V> flatten(final List<List<V>> lists) {

        int size = 0;

        for (final List<V> list : lists) {
            size += list.size();
        }

        final List<V> res = new ArrayList<V>(size);

        for (final List<V> list : lists) {
            res.addAll(list);
        }

        return res;

    }
}

Related Tutorials