I've looked around but I can't seem to find an API call that does the following: I need to merge all ArrayLists in an ArrayList to form one ArrayList with all the elements from all the sub-ArrayLists, if that makes sense.
Here's an example:
{"It's", "a", {"small", "world, "after"}, {"all"}} becomes {"It's", "a", "small", "world", "after", "all"}
public List<?> flatten(List<?> input) {
List<Object> result = new ArrayList<Object>();
for (Object o: input) {
if (o instanceof List<?>) {
result.addAll(flatten((List<?>) o));
} else {
result.add(o);
}
}
return result;
}
为了建立在Thilo的答案之上,并避免重新实现自己的答案 ,请考虑Groovy的Collection.flatten()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.