在Java 8,I在性感方式使用這些循環:
//parallel loop
public static <A, B> void loop(Collection<A> a, Collection<B> b, IntPredicate intPredicate, BiConsumer<A, B> biConsumer) {
Iterator<A> ait = a.iterator();
Iterator<B> bit = b.iterator();
if (ait.hasNext() && bit.hasNext()) {
for (int i = 0; intPredicate.test(i); i++) {
if (!ait.hasNext()) {
ait = a.iterator();
}
if (!bit.hasNext()) {
bit = b.iterator();
}
biConsumer.accept(ait.next(), bit.next());
}
}
}
//nest loop
public static <A, B> void loopNest(Collection<A> a, Collection<B> b, BiConsumer<A, B> biConsumer) {
for (A ai : a) {
for (B bi : b) {
biConsumer.accept(ai, bi);
}
}
}
的一些示例,這些2所列出:
List<Integer> a = Arrays.asList(1, 2, 3);
List<String> b = Arrays.asList("a", "b", "c", "d");
環路內的一個分鐘大小和b:
loop(a, b, i -> i < Math.min(a.size(), b.size()), (x, y) -> {
System.out.println(x + " -> " + y);
});
輸出:
的一個和b最大尺寸內環路(在較短的列表中的元素將被循環):
loop(a, b, i -> i < Math.max(a.size(), b.size()), (x, y) -> {
System.out.println(x + " -> " + y);
});
輸出:
1 -> a
2 -> b
3 -> c
1 -> d
環路Ñ倍((如果n大於列表的大小)大的元件將被循環):
loop(a, b, i -> i < 5, (x, y) -> {
System.out.println(x + " -> " + y);
});
輸出:
1 -> a
2 -> b
3 -> c
1 -> d
2 -> a
循環永遠:
loop(a, b, i -> true, (x, y) -> {
System.out.println(x + " -> " + y);
});
適用於您的情況:
loop(list1, list2, i -> i < Math.min(a.size(), b.size()), (e1, e2) -> {
doStuff(e1);
doStuff(e2);
});
回覆:語義純度 - 你可以迭代過程中檢查列表2邊界('(I'list1.length)&&(I'list2.length)'),或者如果您知道迭代期間列表將不會被修改,您可以在循環之前檢查是否有'list1'和'list2'具有相同的長度,在這種情況下,您可以避免在迭代期間用明確的良心檢查兩者的邊界。 – QuantumMechanic 2011-04-04 23:47:24
如果一個線程修改'list2'而不是'list1',那麼我就搞定了。 – 2011-04-04 23:53:38
就像我說的 - 「如果你知道列表在迭代過程中將不會被修改」。而且,那樣會讓你比現在更糟糕。如果你不得不擔心多個線程觸及這些列表,那麼你就有更多的擔憂,而不僅僅是這個循環。 – QuantumMechanic 2011-04-05 00:03:44