简体   繁体   中英

What is the best way to iterate over multiple lists in the same method in Java

I need to achieve a result that give me 4 different sums after iterate over lists in a single method in Java.

I have 4 different lists with a lot of entries, and i need to iterate in the same way over those 4 lists. Whats the best way to do it, and maintain the best possible performance. Should i implement some Thread Pool? or does not really matter? The lists have around 500 entries each.

Thank you.

Cleanest way I can think of:

IntStream
  .range(0,4)
  .parallel()
  .foreach(i -> 
    lists[i]
    .stream()
    .parallel()
    .forEach(item -> operation(item)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM