Here you can find the source of flatten(Collection> list, Class
public static <T> List<T> flatten(Collection<?> list, Class<T> type)
//package com.java2s; //License from project: Apache License import java.util.ArrayList; import java.util.Collection; import java.util.List; public class Main { public static <T> List<T> flatten(Collection<?> list, Class<T> type) { List<Object> retVal = new ArrayList<Object>(); flatten(list, retVal);// w w w . j a v a 2 s .c o m return (List<T>) retVal; } private static void flatten(Collection<?> fromTreeList, Collection<Object> toFlatList) { for (Object item : fromTreeList) { if (item instanceof Collection<?>) { flatten((Collection<?>) item, toFlatList); } else { toFlatList.add(item); } } } }