正如其他人所提到的,对控制流使用异常并不理想。您可以改用运算符来测试元素是否为 .nullpointer的答案显示了一个很好的例子。如果你想要一个更通用的选项,你也可以做这样的事情:instanceof
Collection
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;
public static <E> List<E> deepFlatten(final Iterable<?> iterable, final Class<E> type) {
if (type.isPrimitive() || type.isArray() || Iterable.class.isAssignableFrom(type)) {
throw new IllegalArgumentException(
"type must not denote a primitive, array, or java.lang.Iterable type: " + type);
}
final List<E> result = new ArrayList<>();
for (final Object element : iterable) {
if (element instanceof Iterable<?>) {
result.addAll(deepFlatten((Iterable<?>) element, type)); // recursion
} else if (element != null && element.getClass().isArray()) {
if (element instanceof Object[]) {
result.addAll(deepFlatten(Arrays.asList((Object[]) element), type)); // recursion
} else { // primitive array
final Iterable<?> itrArray = IntStream.range(0, Array.getLength(element))
.mapToObj(index -> Array.get(element, index))::iterator; // method reference
result.addAll(deepFlatten(itrArray, type)); // recursion
}
} else {
/*
* Will throw ClassCastException if any element is not an instance
* of "type". You could also throw a NullPointerException here if
* you don't want to allow null elements.
*/
result.add(type.cast(element));
}
}
return result;
}
这还通过递归处理“嵌入式”数组以及 s。请注意,由于歧义,它不处理s;我们应该扁平化键或值,还是两者兼而有之?Iterable
Map
调用上述内容:
Iterable<?> iterable = List.of(
"A", "B", "C", "D",
List.of("E", "F", List.of("G", "H"), "I", "J"),
"K",
new String[]{"L", "M", "N", "O", "P"},
new String[][]{{"Q", "R"}, {"S", "T"}, {"U"}, {"V"}},
new Object[]{"W", "X"},
"Y", "Z"
);
List<String> flattened = deepFlatten(iterable, String.class);
System.out.println(flattened);
给了我:
[A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z]
请注意,字母是有序的,因为 s 和数组具有有保证的迭代顺序。如果你包含一个结果,则每次可能的顺序可能不一样。List
Iterable
Set
deepFlatten