在ArrayList中的LinkedList的比较 [英] comparison of Linkedlist over arraylist
问题描述
我明白了的LinkedList
作为一个双向链表实现。其附加的性能,并删除比的ArrayList
,更好,但对get和set方法更糟糕。
这是否意味着我应该选择的LinkedList
在的ArrayList
插入?
我写了一个小测试,发现的ArrayList
是插入更快。那么如何链表比 ArrayList的速度更快
?
请参考下面的例子中,我已经做了。
进口java.util.Date;
进口java.util.LinkedList中;
进口的java.util.List; 公共类TestLinkedList { 公共静态无效的主要(字串[] args){ 长lStartTime =新的日期()的getTime()。
的System.out.println(lStartTime ::+ lStartTime);
清单<整数GT; integerList =新的LinkedList<整数GT;();
的for(int i = 0; I<千万,我++){
integerList.add(ⅰ);
} 长lEndTime =新的日期()的getTime()。
的System.out.println(lEndTime ::+ lEndTime); 长差= lEndTime - lStartTime; 的System.out.println(经过的毫秒:+差); } }
LinkedList的的确是更快的插入,问题是你的榜样。在您的code,你通过追加到年底所有的时间插入。对于ArrayList中它是作为对链表一样容易。你应该做的是建立都说5000项的列表,然后开始在中间插入。这里阵列变慢 - 你有阵列的其余部分的插入位置后,所有的时间偏移。这就是将示区别。分析事物如何运作,也就不难理解了。下面是修改code:
进口java.util.Date;
进口java.util.LinkedList中;
进口的java.util.ArrayList;
进口的java.util.List; 公共类习题{ 公共静态无效的主要(字串[] args){ 长lStartTime =新的日期()的getTime()。
的System.out.println(lStartTime ::+ lStartTime);
清单<整数GT; integerList =新的LinkedList<整数GT;();
的for(int i = 0; I< 5000;我++){
integerList.add(0,i)的;
}
的for(int i = 0; I< 100000;我++){
integerList.add(1000,I);
} 长lEndTime =新的日期()的getTime()。
的System.out.println(lEndTime ::+ lEndTime); 长差= lEndTime - lStartTime; 的System.out.println(经过的毫秒:+差); }
}
I understood that LinkedList
is implemented as a double linked list. Its performance on add and remove is better than Arraylist
, but worse on get and set methods.
Does that mean I should choose LinkedList
over Arraylist
for inserting?
I wrote a small test and found ArrayList
is faster in inserting. Then how does linked list faster than ArrayList
?
Please refer the below example which I have done.
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
public class TestLinkedList {
public static void main(String[] args) {
long lStartTime = new Date().getTime();
System.out.println("lStartTime:: " + lStartTime);
List<Integer> integerList = new LinkedList<Integer>();
for (int i = 0; i < 10000000; i++) {
integerList.add(i);
}
long lEndTime = new Date().getTime();
System.out.println("lEndTime:: " + lEndTime);
long difference = lEndTime - lStartTime;
System.out.println("Elapsed milliseconds: " + difference);
}
}
Linkedlist is indeed faster on insertion, the problem is with your example. In your code you insert by appending to the end all the time. For ArrayList it is as easy as for LinkedList. What you should do is to build a list of say 5000 items and then start inserting in the middle. Here array becomes slow - you have to shift all the time the rest of the array after the insertion position. This is what will show the difference. Analyzing how the things work, it is not difficult to understand why. Here is the modified code:
import java.util.Date;
import java.util.LinkedList;
import java.util.ArrayList;
import java.util.List;
public class Prob {
public static void main(String[] args) {
long lStartTime = new Date().getTime();
System.out.println("lStartTime:: " + lStartTime);
List<Integer> integerList = new LinkedList<Integer>();
for (int i = 0; i < 5000; i++) {
integerList.add(0, i);
}
for (int i = 0; i < 100000; i++) {
integerList.add(1000, i);
}
long lEndTime = new Date().getTime();
System.out.println("lEndTime:: " + lEndTime);
long difference = lEndTime - lStartTime;
System.out.println("Elapsed milliseconds: " + difference);
}
}
这篇关于在ArrayList中的LinkedList的比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!