6

Assume I have a List<SomeObject> a.
Now also assume that I have another List<SomeProcessor> b.
Each SomeProcessor uses a for its processing.

Besides:

int idx = 0;   
for(SomeProcessor o:b){  
    o2 = a.get(idx);  
    o.doSomething(o2);  
    idx++;   
}

Is there a more elegant way to handle this?

Jim
  • 18,826
  • 34
  • 135
  • 254
  • does a support Iterable interface? – Wint Feb 21 '12 at 07:40
  • No.It is just a custom object.How would that help?I mean should there be one? – Jim Feb 21 '12 at 07:42
  • 1
    Check out this answer: http://stackoverflow.com/a/8104205/1029272 Easy to implement, and works a treat. I got it up and running within the last 5 minutes to try it out. – Deco Feb 21 '12 at 07:56
  • 2
    possible duplicate of [Best way to iterate over two lists simultaneously?](http://stackoverflow.com/questions/3137944/best-way-to-iterate-over-two-lists-simultaneously) – skaffman Feb 21 '12 at 09:20

3 Answers3

7
public interface Processor<T> {
    public void process(T object);
}

And then a helper method:

public static <T> void processAll(Collection<T> items, Collection<? extends Processor<T>> processors) {
    Iterator<T> i = items.iterator();
    Iterator<? extends Processor<T>> p = processors.iterator();
    while(i.hasNext() && p.hasNext())
        p.next().process(i.next()); 
}

You could put that helper method on the class which uses it, if there is only one (and make it private), or put it a utility class which is shared by the entire program.

Of course there are other ways to code processAll; for example, you could use a for loop on one of the collections. But in any case, breaking this low-level code out into a helper method will make your higher-level code cleaner and less "noisy". And if you are doing something similar in multiple parts of the program, they can share the helper method.

Alex D
  • 29,755
  • 7
  • 80
  • 126
  • I don't want to process it like that.I need to process each object once by 1 processor – Jim Feb 21 '12 at 07:35
  • @narek.gevorgyan:Yes they are always same size – Jim Feb 21 '12 at 07:41
  • @Alex D:So you suggest essentially to use the 2 iterators of the `lists` simultaneously, right? – Jim Feb 21 '12 at 07:50
  • @Jim, you could use a `for` loop and an iterator, or 2 iterators. In either case, the biggest thing you can do to make your code cleaner, is to move this detailed, low-level code into a helper method with a meaningful name. – Alex D Feb 21 '12 at 08:23
  • It's unfortunate that Java doesn't have closures... you could write a very nice, general-purpose method for "iterating over two lists and doing something to each pair" if it did. – Alex D Feb 21 '12 at 08:26
1

If you can generate a Map<SomeObject, SomeProcessor> instead of two lists, then it'll be elegant. This may not apply, but I'll just give it a shot.

Bob Wang
  • 606
  • 4
  • 8
1

Maybe you can try with this simpler way :

int idx = 0;
while(idx<a.size() && idx<b.size()){   
    b.get(idx).doSomething(a.get(idx++));
}
alain.janinm
  • 19,951
  • 10
  • 65
  • 112