java 迭代列表的最佳方法是什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29575427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 15:31:36  来源:igfitidea点击:

What is the best way to iterate over list

javalistcollectionsiteratorthread-safety

提问by Nimesh

I have worked pretty much on collection but I have few doubts.

我在收藏方面做了很多工作,但我几乎没有怀疑。

I am aware that we can iterate list with iterator.

我知道我们可以用迭代器迭代列表。

Another way is that we can go through as below:

另一种方法是我们可以通过以下方式:

for(int i=0; i<list.size(); i++){
   list.get(i);
}

Here I think there is problem that each time it will call list.size() that will build whole tree that will impact performance.

在这里,我认为存在问题,每次它都会调用 list.size() 来构建会影响性能的整棵树。

I thought other solution as well like:

我认为其他解决方案也像:

int s = list.size();
for(int i=0; i<s; i++){
  list.get(i);
}

I think this can solve the problem. I am not much exposed to thread. I am thinking that whetherthis should be right approach or not.

我认为这可以解决问题。我不太接触线程。我在想这是否应该是正确的方法。

Another way I thought is like:

我认为的另一种方式是:

for (Object obj; list){

}

With this new for loop, I think compiler again checks size of list.

有了这个新的 for 循环,我认为编译器会再次检查列表的大小。

Please give best solution from these or alternative performance efficient approach. Thank you for your help.

请从这些或替代性能有效的方法中给出最佳解决方案。谢谢您的帮助。

采纳答案by JB Nizet

Calling size()at each iteration is not really a problem. This operation is O(1) for all the collections I know of: size() simply returns the value of a field of the list, holding its size.

size()在每次迭代中调用并不是真正的问题。对于我知道的所有集合,此操作是 O(1):size() 仅返回列表字段的值,并保持其大小。

The main problem of the first way is the repeated call to get(i). This operation is O(1) for an ArrayList, but is O(n) for a LinkedList, making the whole iteration O(n2) instead of O(n): get(i)forces the list to start from the first element of the list (or the last one), and to go to the next node until the ith element.

第一种方式的主要问题是重复调用get(i). 这个操作对于 ArrayList 是 O(1),但是对于 LinkedList 是 O(n),使得整个迭代 O(n 2) 而不是 O(n):get(i)强制列表从列表的第一个元素开始(或最后一个),然后转到下一个节点,直到第 i 个元素。

Using an iterator, or using a foreach loop (which internally uses an iterator), guarantees that the most appropriate way of iterating is used, because the iterator knows about how the list is implemented and how best go from one element to the next.

使用迭代器,或使用 foreach 循环(内部使用迭代器),保证使用最合适的迭代方式,因为迭代器知道如何实现列表以及如何最好地从一个元素到下一个元素。

BTW, this is also the only way to iterate through non-indexed collections, like Sets. So you'd better get used to use that kind of loop.

顺便说一句,这也是迭代非索引集合(如 Sets)的唯一方法。所以你最好习惯使用那种循环。

回答by CyberAleks

For your example is the best way:

对于您的示例是最好的方法:

for (Object obj: list){

}

It is the same like in java version < 1.5:

它与 java 版本 < 1.5 中的相同:

for (Iterator it = hs.iterator() ; it.hasNext() ; ){}

It use iterator of collection. You actually don't need the size of collection. The .size() method is should actually don't build the tree, but .get() can loops to the given element. .get() and .size() methods depend on List implementation. .get() In ArrayList should be actually O(1) complexity and not O(n)

它使用集合迭代器。您实际上不需要集合的大小。.size() 方法实际上不应该构建树,但是 .get() 可以循环到给定的元素。.get() 和 .size() 方法依赖于 List 实现。.get() 在 ArrayList 中实际上应该是 O(1) 复杂度而不是 O(n)

UPDATE

更新

In java 8 you can use:

在 Java 8 中,您可以使用:

myList.forEach{ Object elem -> 
 //do something
}

回答by Mohit Gupta

The best way to iterate the list in terms of performance would be to use iterators ( your second approach using foreach ). If you are using list.get(i), it's performance would depend upon the implementation of the list. For ArrayList, list.get(i) is O(1) where as it's O(n) for LinkedList.

在性能方面迭代列表的最佳方法是使用迭代器(第二种方法使用 foreach )。如果您使用 list.get(i),它的性能将取决于列表的实现。对于 ArrayList,list.get(i) 是 O(1),而对于 LinkedList,它是 O(n)。

Also, list.size() is O(1) and should not have any impact over the performance.

此外, list.size() 是 O(1) 并且不应该对性能产生任何影响。

回答by shepard23

for (Object obj: list){

}

Above code for me is the best way, it is clean and can be read easily.

上面的代码对我来说是最好的方式,它很干净而且很容易阅读。

The forEach in Java 8 is nice too.

Java 8 中的 forEach 也很好。