Question

I want to create a function to iterate over multiple lists. Now I know these lists have exactly the same size (and they can have different types too), for example:

List<Integer> list1 = getList1();
List<String> list2 = getList2();
List<Long> list3 = getList3();
list1.size() == list2.size(); // returns true
list2.size() == list3.size(); // returns true

And I want to be able to call a function which takes 3 elements at the same slice in each of these lists so for exemple:

int calculate(int elemList1, String elemList2, long elemList3) {...}

// iterator over the lists in parallel {
    int ret = calculate(elemList1, elemList2, elemList3);
// }

I would like to do the equivalent of what I saw discussed in guava here but doesn't look implemented yet: http://code.google.com/p/guava-libraries/issues/detail?id=677

They talk about doing Iterators.interleave or Iterators.zip and I would like to do something similar but I haven't been able to, so can someone please help me a bit? Thanks!

I would prefer to not have to get the size of one list and iterate over them by index, because in the future i can have lists of different sizes so i would like to use only 1 way to do this.

Was it helpful?

Solution

A compound Iterator might be a cool idea, e.g.:

Iterator<Array<?>> compoundIterator = createIterator(List1, List2, List3);

Then inside the implementation, you would create iterators for each of the lists, then loop through the items and put them into an array, then your consumption of that stuff would look something like:

while (compoundIterator.hasElements()){
    Array[] elements = compountIterator.nextElement();
    calculate(elements[0], elements[1], elements[2]);
}

What's nice about this solution is you are hiding all those details about whether one list ran out or not (of course you have to decide what you want to do if one does, but that could be wrapped inside as well).

OTHER TIPS

I don't think you are really saying in parallel, since the values are being used to invoke a method. If you want the same elements from each list, concurrently skipping through different lists on different threads does you no good.

You just need to do a for loop and then call list1.get(i), list2.get(i), list3.get(i).

You can create a new thread and iterate your list there. Spawn multiple of this therad and you can iterate your list in parallel.

If you want to pass List of any template type, you can just specify your method parameter as List, although this might result in compiler warnings. Other thing you can try is pass the list as List<T extends Object> and do a runtime check of type T and action accordingly

However if by 'parallel' you are not referring to multithreading / concurrency -- instead just want to be able to iterate your 3 lists in one single loop, then something like this will do (warning code is rough example only -- not tested / complying with coding standard):

List list1 = ...
List list2 = ...
List list3 = ...

for(int i=0,j=0,k=0; i<list1.size() && j<list2.size() && k<list3.size(); ++i,++j,++k)
{
   Object elemOfList1 = list1.get(i);
   Object elemOfList2 = list2.get(j);
   Object elemOfList3 = list3.get(k);
   // do something here
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top