Java Collection Flatten flatten(Collection> nestedList)

Here you can find the source of flatten(Collection> nestedList)

Description

combines all the lists in a collection to a single list

License

Open Source License

Declaration

public static <T> List<T> flatten(Collection<List<T>> nestedList) 

Method Source Code

//package com.java2s;

import java.util.ArrayList;

import java.util.Collection;

import java.util.List;

public class Main {
    /**/*from   w  w  w  .  jav a2s  .  c  om*/
     * combines all the lists in a collection to a single list
     */
    public static <T> List<T> flatten(Collection<List<T>> nestedList) {
        List<T> result = new ArrayList<T>();
        for (List<T> list : nestedList) {
            result.addAll(list);
        }
        return result;
    }

    /**
     * Add all the items from an iterable to a collection.
     *
     * @param <T>
     *          The type of items in the iterable and the collection
     * @param collection
     *          The collection to which the items should be added.
     * @param items
     *          The items to add to the collection.
     */
    public static <T> void addAll(Collection<T> collection,
            Iterable<? extends T> items) {
        for (T item : items) {
            collection.add(item);
        }
    }
}

Related

  1. flatten(Class cls, Collection> collections)
  2. flatten(Collection> collections)
  3. flatten(Collection> cols)
  4. flatten(Collection l)
  5. flatten(Collection list, Class type)
  6. flatten(Collection> collOfLists)
  7. flatten(Collection collection, char separator)
  8. flatten(Collection original)
  9. flatten(final S targetCollection, final Collection> setOfSets)