How to iterate over multiple lists in parallel in Java?

I want to create a function to iterate over multiple lists. Now I know that these lists are exactly the same size (and they can be of different types), 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 that takes 3 elements on the same chunk in each of these lists, for example:

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 an equivalent to what I saw in guava here, but not yet implemented: http://code.google.com/p/guava-libraries/issues/detail?id=677

They talk about using Iterators.interleave or Iterators.zip and I would like to do something similar but I could not, so can someone please help me a little? Thank!

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

+3


source to share


3 answers


An Iterator concatenation can be a cool idea, for example:

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

      

Then, inside the implementation, you create iterators for each of the lists, then loop through the elements and put them in an array, then your consumption of this stuff will look something like this:



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

      

What's nice about this solution is you hide all those details about whether one list has ended or not (of course, you have to decide what you want to do if that happens, but that could be wrapped internally too).

+8


source


You can create a new thread and list the list. Express a few of this therad and you can iterate your list in parallel.

If you want to pass a List of any type of template, you can simply specify your method parameter as List, although this may lead to compiler warnings. Another thing you can try is to pass the list as List <T extends Object> and perform a runtime check on the type of T and action accordingly.



However, if "in parallel" you don't mean multithreading / concurrency - instead just want you to be able to iterate over three lists in one loop, then something like this (warning code is only a rough example) is not tested / standard compliant encoding):

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
}

      

+1


source


I don't think you are really talking in parallel since the values ​​are used to call the method. If you want the same items from each list, running through different lists at the same time on different streams is not good for you.

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

+1


source







All Articles