Alex Rivera | Logout

Iterate through multiple collections in the same "for" loop?

Asked 2012-03-30T19:04:27.130
19

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

So something like:

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

Thanks

Edit
Report

1 Answer

25

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

for (Foo element : Iterables.concat(collection1, collection2)) {
    foo.frob();
}
answered 2012-03-30T19:06:35.133

Your Answer