遍历同一个“for"中的多个集合.环形?

2022-01-10 00:00:00 for-loop syntax iterator java

我想知道是否有这样一种方法可以遍历多个集合,并在 java 中为每个循环扩展.

I wonder if there's such a way to iterate thru multiple collections with the extended for each loop in java.

比如:

for (Object element : collection1, collection2, ....)
         // do something ...

谢谢

推荐答案

您完全可以使用 Guava的Iterables.concat():

You can do exactly this with Guava's Iterables.concat():

for (Foo element : Iterables.concat(collection1, collection2)) {
    foo.frob();
}

相关文章