And Java util. stream. Stream processes two lists in parallel

For each element in each list, perform an action Elements can be processed in any order For example, in old Java:

List<A> aList;
List<B> bList; // aList is larger than bList

for (int i=0; i<bList.size(),i++) {
  aList.get(i).doSomethingWith(bList.get(i));
}

for (int j=i; j<aList.size(),j++) {
  aList.get(j).doSomething();
}

Using Java util. stream. Stream the best way to implement this method is which elements can be processed in parallel?

Solution

You need to process the two lists in parallel, so I don't think you can stream the lists yourself However, you can stream indexes and process them:

IntStream.range(0,aList.size())
    .parallel()
    .forEach(i -> {
        if (i < bList.size()) aList.get(i).doSomethingWith(bList.get(i));
        else aList.get(i).doSomething();
    });
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>