Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nifty way to iterate over parallel arrays in Java using foreach

I've inherited a bunch of code that makes extensive use of parallel arrays to store key/value pairs. It actually made sense to do it this way, but it's sort of awkward to write loops that iterate over these values. I really like the new Java foreach construct, but it does not seem like there is a way to iterate over parallel lists using this.

With a normal for loop, I can do this easily:

for (int i = 0; i < list1.length; ++i) {     doStuff(list1[i]);     doStuff(list2[i]); } 

But in my opinion this is not semantically pure, since we are not checking the bounds of list2 during iteration. Is there some clever syntax similar to the for-each that I can use with parallel lists?

like image 410
Travis Webb Avatar asked Apr 04 '11 23:04

Travis Webb


People also ask

Can you use a forEach loop on an array Java?

The forEach in JavaThe foreach loop is generally used for iteration through array elements in different programming languages. The Java provides arrays as well as other collections and there should be some mechanism for going through array elements easily; like the way foreach provides.

Which is faster forEach or for loop Java?

Foreach performance is approximately 6 times slower than FOR / FOREACH performance. The FOR loop without length caching works 3 times slower on lists, comparing to arrays. The FOR loop with length caching works 2 times slower on lists, comparing to arrays.

Does forEach iterate in order Java?

Yes. The foreach loop will iterate through the list in the order provided by the iterator() method. See the documentation for the Iterable interface.

What is forEach loop with example?

In this program, foreach loop is used to traverse through a collection. Traversing a collection is similar to traversing through an array. The first element of collection is selected on the first iteration, second element on second iteration and so on till the last element.


2 Answers

I would use a Map myself. But taking you at your word that a pair of arrays makes sense in your case, how about a utility method that takes your two arrays and returns an Iterable wrapper?

Conceptually:

for (Pair<K,V> p : wrap(list1, list2)) {     doStuff(p.getKey());     doStuff(p.getValue()); } 

The Iterable<Pair<K,V>> wrapper would hide the bounds checking.

like image 92
Isaac Truett Avatar answered Sep 28 '22 00:09

Isaac Truett


From the official Oracle page on the enhanced for loop:

Finally, it is not usable for loops that must iterate over multiple collections in parallel. These shortcomings were known by the designers, who made a conscious decision to go with a clean, simple construct that would cover the great majority of cases.

Basically, you're best off using the normal for loop.

If you're using these pairs of arrays to simulate a Map, you could always write a class that implements the Map interface with the two arrays; this could let you abstract away much of the looping.

Without looking at your code, I cannot tell you whether this option is the best way forward, but it is something you could consider.

like image 20
Tikhon Jelvis Avatar answered Sep 28 '22 00:09

Tikhon Jelvis