Java Collection Flatten flattenCollection(Collection col)

Here you can find the source of flattenCollection(Collection col)

Description

flatten Collection

License

Apache License

Declaration

public static Collection flattenCollection(Collection col) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.*;

public class Main {

    public static Collection flattenCollection(Collection col) {

        Collection toRet = null;//from  w  w  w . j  a  v  a 2  s. com
        try {
            toRet = col.getClass().newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
        } catch (IllegalAccessException e) {
            e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
        }

        for (Object o : col) {
            if (o instanceof Collection) {
                toRet.addAll(flattenCollection((Collection) o));
            } else {
                toRet.add(o);
            }
        }
        return toRet;
    }
}

Related

  1. flatten(Collection> nestedList)
  2. flatten(Collection> collOfLists)
  3. flatten(Collection collection, char separator)
  4. flatten(Collection original)
  5. flatten(final S targetCollection, final Collection> setOfSets)
  6. flattenCollection(Collection array, String fmt, char separator)
  7. flattenForest(Collection forest)
  8. flattenNodeIds( Collection> nodesInMotif)
  9. flattenStrings(Collection values)