简体   繁体   English

在 Java 中以相同方法迭代多个列表的最佳方法是什么

[英]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.我需要在 Java 中的单个方法中迭代列表后获得一个结果,该结果给我 4 个不同的总和。

I have 4 different lists with a lot of entries, and i need to iterate in the same way over those 4 lists.我有 4 个不同的列表,其中有很多条目,我需要以相同的方式遍历这 4 个列表。 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.每个列表大约有 500 个条目。

Thank you.谢谢你。

Cleanest way I can think of:我能想到的最干净的方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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