主要介紹ArrayList和LinkedList這兩種list的五種循環遍歷方式,各類方式的性能測試對比,根據ArrayList和LinkedList的源碼實現分析性能結果,總結結論。
經過本文你能夠了解(1)List的五種遍歷方式及各自性能 (2)foreach及Iterator的實現 (3)加深對ArrayList和LinkedList實現的瞭解。
閱讀本文前但願你已經瞭解ArrayList順序存儲和LinkedList鏈式的結構,本文不對此進行介紹。android
相關:HashMap循環遍歷方式及其性能對比數組
1. List的五種遍歷方式
下面只是簡單介紹各類遍歷示例(以ArrayList爲例),各自優劣會在本文後面進行分析給出結論。
(1) for each循環數據結構
1
2
3
4
|
List<Integer> list = new ArrayList<Integer>();
for (Integer j : list) {
// use j
}
|
(2) 顯示調用集合迭代器app
1
2
3
4
|
List<Integer> list = new ArrayList<Integer>();
for (Iterator<Integer> iterator = list.iterator(); iterator.hasNext();) {
iterator.next();
}
|
或ide
1
2
3
4
5
|
List<Integer> list = new ArrayList<Integer>();
Iterator<Integer> iterator = list.iterator();
while (iterator.hasNext()) {
iterator.next();
}
|
(3) 下標遞增循環,終止條件爲每次調用size()函數比較判斷函數
1
2
3
4
|
List<Integer> list = new ArrayList<Integer>();
for (int j = 0; j < list.size(); j++) {
list.get(j);
}
|
(4) 下標遞增循環,終止條件爲和等於size()的臨時變量比較判斷oop
1
2
3
4
5
|
List<Integer> list = new ArrayList<Integer>();
int size = list.size();
for (int j = 0; j < size; j++) {
list.get(j);
}
|
(5) 下標遞減循環post
1
2
3
4
|
List<Integer> list = new ArrayList<Integer>();
for (int j = list.size() - 1; j >= 0; j--) {
list.get(j);
}
|
在測試前你們能夠根據對ArrayList和LinkedList數據結構及Iterator的瞭解,想一想上面五種遍歷方式哪一個性能更優。
二、List五種遍歷方式的性能測試及對比
如下是性能測試代碼,會輸出不一樣數量級大小的ArrayList和LinkedList各類遍歷方式所花費的時間。
PS:若是運行報異常in thread 「main」 java.lang.OutOfMemoryError: Java heap space,請將main函數裏面list size的大小減少。
其中getArrayLists函數會返回不一樣size的ArrayList,getLinkedLists函數會返回不一樣size的LinkedList。
loopListCompare函數會分別用上面的遍歷方式1-5去遍歷每個list數組(包含不一樣大小list)中的list。
print開頭函數爲輸出輔助函數。
測試環境爲Windows7 32位系統 3.2G雙核CPU 4G內存,Java 7,Eclipse -Xms512m -Xmx512m
最終測試結果以下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
compare loop performance of ArrayList
-----------------------------------------------------------------------
list size | 10,000 | 100,000 | 1,000,000 | 10,000,000
-----------------------------------------------------------------------
for each | 1 ms | 3 ms | 14 ms | 152 ms
-----------------------------------------------------------------------
for iterator | 0 ms | 1 ms | 12 ms | 114 ms
-----------------------------------------------------------------------
for list.size() | 1 ms | 1 ms | 13 ms | 128 ms
-----------------------------------------------------------------------
for size = list.size() | 0 ms | 0 ms | 6 ms | 62 ms
-----------------------------------------------------------------------
for j-- | 0 ms | 1 ms | 6 ms | 63 ms
-----------------------------------------------------------------------
compare loop performance of LinkedList
-----------------------------------------------------------------------
list size | 100 | 1,000 | 10,000 | 100,000
-----------------------------------------------------------------------
for each | 0 ms | 1 ms | 1 ms | 2 ms
-----------------------------------------------------------------------
for iterator | 0 ms | 0 ms | 0 ms | 2 ms
-----------------------------------------------------------------------
for list.size() | 0 ms | 1 ms | 73 ms | 7972 ms
-----------------------------------------------------------------------
for size = list.size() | 0 ms | 0 ms | 67 ms | 8216 ms
-----------------------------------------------------------------------
for j-- | 0 ms | 1 ms | 67 ms | 8277 ms
-----------------------------------------------------------------------
|
第一張表爲ArrayList對比結果,第二張表爲LinkedList對比結果。
表橫向爲同一遍歷方式不一樣大小list遍歷的時間消耗,縱向爲同一list不一樣遍歷方式遍歷的時間消耗。
PS:因爲首次遍歷List會稍微多耗時一點,for each的結果稍微有點誤差,將測試代碼中的幾個Type順序調換會發現,for each耗時和for iterator接近。
三、遍歷方式性能測試結果分析
(1) foreach介紹
foreach是Java SE5.0引入的功能很強的循環結構,for (Integer j : list)應讀做for each int in list。
for (Integer j : list)實現幾乎等價於
1
2
3
4
|
Iterator<Integer> iterator = list.iterator();
while(iterator.hasNext()) {
Integer j = iterator.next();
}
|
下面的分析會將foreach和顯示調用集合迭代器兩種遍歷方式歸類爲Iterator方式,其餘三種稱爲get方式遍歷。
這時咱們已經發現foreach的一大好處,簡單一行實現了四行的功能,使得代碼簡潔美觀,另外一大好處是相對於下標循環而言的,foreach沒必要關心下標初始值和終止值及越界等,因此不易出錯。Effective-Java中推薦使用此種寫法遍歷,本文會驗證這個說法。
使用foreach結構的類對象必須實現了Iterable接口,Java的Collection繼承自此接口,List實現了Collection,這個接口僅包含一個函數,源碼以下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package java.lang;
import java.util.Iterator;
/**
* Implementing this interface allows an object to be the target of
* the "foreach" statement.
*
* @param <T> the type of elements returned by the iterator
*
* @since 1.5
*/
public interface Iterable<T> {
/**
* Returns an iterator over a set of elements of type T.
*
* @return an Iterator.
*/
Iterator<T> iterator();
}
|
iterator()用於返回一個Iterator,從foreach的等價實現中咱們能夠看到,會調用這個函數獲得Iterator,再經過Iterator的next()獲得下一個元素,hasNext()判斷是否還有更多元素。Iterator源碼以下:
1
2
3
4
5
6
7
|
public interface Iterator<E> {
boolean hasNext();
E next();
void remove();
}
|
(2) ArrayList遍歷方式結果分析
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
compare loop performance of ArrayList
-----------------------------------------------------------------------
list size | 10,000 | 100,000 | 1,000,000 | 10,000,000
-----------------------------------------------------------------------
for each | 1 ms | 3 ms | 14 ms | 152 ms
-----------------------------------------------------------------------
for iterator | 0 ms | 1 ms | 12 ms | 114 ms
-----------------------------------------------------------------------
for list.size() | 1 ms | 1 ms | 13 ms | 128 ms
-----------------------------------------------------------------------
for size = list.size() | 0 ms | 0 ms | 6 ms | 62 ms
-----------------------------------------------------------------------
for j-- | 0 ms | 1 ms | 6 ms | 63 ms
-----------------------------------------------------------------------
|
PS:因爲首次遍歷List會稍微多耗時一點,for each的結果稍微有點誤差,將測試代碼中的幾個Type順序調換會發現,for each耗時和for iterator接近。
從上面咱們能夠看出:
a. 在ArrayList大小爲十萬以前,五種遍歷方式時間消耗幾乎同樣
b. 在十萬之後,第4、五種遍歷方式快於前三種,get方式優於Iterator方式,而且
1
2
3
4
|
int size = list.size();
for (int j = 0; j < size; j++) {
list.get(j);
}
|
用臨時變量size取代list.size()性能更優。咱們看看ArrayList中迭代器Iterator和get方法的實現
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
private class Itr implements Iterator<E> {
int cursor; // index of next element to return
int lastRet = -1; // index of last element returned; -1 if no such
int expectedModCount = modCount;
public boolean hasNext() {
return cursor != size;
}
@SuppressWarnings("unchecked")
public E next() {
checkForComodification();
int i = cursor;
if (i >= size)
throw new NoSuchElementException();
Object[] elementData = ArrayList.this.elementData;
if (i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i + 1;
return (E) elementData[lastRet = i];
}
……
}
public E get(int index) {
rangeCheck(index);
return elementData(index);
}
|
從中能夠看出get和Iterator的next函數一樣經過直接定位數據獲取元素,只是多了幾個判斷而已。
c . 從上能夠看出即使在千萬大小的ArrayList中,幾種遍歷方式相差也不過50ms左右,且在經常使用的十萬左右時間幾乎相等,考慮foreach的優勢,咱們大可選用foreach這種簡便方式進行遍歷。
(3) LinkedList遍歷方式結果分析
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
compare loop performance of LinkedList
-----------------------------------------------------------------------
list size | 100 | 1,000 | 10,000 | 100,000
-----------------------------------------------------------------------
for each | 0 ms | 1 ms | 1 ms | 2 ms
-----------------------------------------------------------------------
for iterator | 0 ms | 0 ms | 0 ms | 2 ms
-----------------------------------------------------------------------
for list.size() | 0 ms | 1 ms | 73 ms | 7972 ms
-----------------------------------------------------------------------
for size = list.size() | 0 ms | 0 ms | 67 ms | 8216 ms
-----------------------------------------------------------------------
for j-- | 0 ms | 1 ms | 67 ms | 8277 ms
-----------------------------------------------------------------------
|
PS:因爲首次遍歷List會稍微多耗時一點,for each的結果稍微有點誤差,將測試代碼中的幾個Type順序調換會發現,for each耗時和for iterator接近。
從上面咱們能夠看出:
a 在LinkedList大小接近一萬時,get方式和Iterator方式就已經差了差很少兩個數量級,十萬時Iterator方式性能已經遠勝於get方式。
咱們看看LinkedList中迭代器和get方法的實現
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
private class ListItr implements ListIterator<E> {
private Node<E> lastReturned = null;
private Node<E> next;
private int nextIndex;
private int expectedModCount = modCount;
ListItr(int index) {
// assert isPositionIndex(index);
next = (index == size) ? null : node(index);
nextIndex = index;
}
public boolean hasNext() {
return nextIndex < size;
}
public E next() {
checkForComodification();
if (!hasNext())
throw new NoSuchElementException();
lastReturned = next;
next = next.next;
nextIndex++;
return lastReturned.item;
}
……
}
public E get(int index) {
checkElementIndex(index);
return node(index).item;
}
/**
* Returns the (non-null) Node at the specified element index.
*/
Node<E> node(int index) {
// assert isElementIndex(index);
if (index < (size >> 1)) {
Node<E> x = first;
for (int i = 0; i < index; i++)
x = x.next;
return x;
} else {
Node<E> x = last;
for (int i = size - 1; i > index; i--)
x = x.prev;
return x;
}
}
|
從上面代碼中能夠看出LinkedList迭代器的next函數只是經過next指針快速獲得下一個元素並返回。而get方法會從頭遍歷直到index下標,查找一個元素時間複雜度爲哦O(n),遍歷的時間複雜度就達到了O(n2)。
因此對於LinkedList的遍歷推薦使用foreach,避免使用get方式遍歷。
(4) ArrayList和LinkedList遍歷方式結果對比分析
從上面的數量級來看,一樣是foreach循環遍歷,ArrayList和LinkedList時間差很少,可將本例稍做修改加大list size會發現二者基本在一個數量級上。
但ArrayList get函數直接定位獲取的方式時間複雜度爲O(1),而LinkedList的get函數時間複雜度爲O(n)。
再結合考慮空間消耗的話,建議首選ArrayList。對於個別插入刪除很是多的可使用LinkedList。
四、結論總結經過上面的分析咱們基本能夠總結下:(1) 不管ArrayList仍是LinkedList,遍歷建議使用foreach,尤爲是數據量較大時LinkedList避免使用get遍歷。(2) List使用首選ArrayList。對於個別插入刪除很是多的可使用LinkedList。(3) 可能在遍歷List循環內部須要使用到下標,這時綜合考慮下是使用foreach和自增count仍是get方式。