Java tutorial
//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); 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); } } } }