Here you can find the source of flattenCollection(Collection col)
public static Collection flattenCollection(Collection col)
//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; } }