Here you can find the source of flatten(List> list, List
Parameter | Description |
---|---|
list | First list. |
flat | Second list. |
private static void flatten(List<?> list, List<Object> flat)
//package com.java2s; import java.util.ArrayList; import java.util.List; public class Main { /**//from ww w. j a va 2s.c o m * Helper method to flatten a potential list of lists into a list of * objects. * @param list First list. * @param flat Second list. */ private static void flatten(List<?> list, List<Object> flat) { for (Object item : list) { if (item instanceof List<?>) { flatten((List<?>) item, flat); } else { flat.add(item); } } } /** * Flattens a potential list of lists into a list of objects. * @param list The list to be flattened. * @return The flattened list. */ public static List<Object> flatten(List<?> list) { List<Object> result = new ArrayList<Object>(); flatten(list, result); return result; } }