老生常谈的关于List的效率问题
public static long getLinkedListAddTime(){
LinkedList<String> list = new LinkedList<String>();
long start = System.currentTimeMillis();
for (int i = 0; i < 771000; i++) {
list.add("s");
}
long end = System.currentTimeMillis();
return end-start;
}
public static long getArrayListAddTime(){
ArrayList<String> list = new ArrayList<String>();
long start = System.currentTimeMillis();
for (int i = 0; i < 771000; i++) {
list.add("s");
}
long end = System.currentTimeMillis();
return end-start;
}
public static void main(String[] args) {
System.out.println(getLinkedListAddTime());
System.out.println(getArrayListAddTime());
}
我直接用add(object o)这个方法添加的话,2个list比较添加的时间,发现反而linkedList的时间耗费的多。
然而我改成add(int i,Object o)这个方法的话,结果则是正常的,也就是大家常说的ArrayList添加耗时更多,LinkedList耗时较少。
我看了一下ArrayList的add(int i,Object o)和add(object o)基本上都是差不多的
/**
* Appends the specified element to the end of this list.
*
* @param e element to be appended to this list
* @return <tt>true</tt> (as specified by {@link Collection#add})
*/
public boolean add(E e) {
ensureCapacity(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
/**
* Inserts the specified element at the specified position in this
* list. Shifts the element currently at that position (if any) and
* any subsequent elements to the right (adds one to their indices).
*
* @param index index at which the specified element is to be inserted
* @param element element to be inserted
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public void add(int index, E element) {
if (index > size || index < 0)
throw new IndexOutOfBoundsException(
"Index: "+index+", Size: "+size);
ensureCapacity(size+1); // Increments modCount!!
System.arraycopy(elementData, index, elementData, index + 1,
size - index);
elementData[index] = element;
size++;
}
有没有懂的大牛来给小兵解答一下这个问题,非常感谢!!!!!
LinkedList<String> list = new LinkedList<String>();
long start = System.currentTimeMillis();
for (int i = 0; i < 771000; i++) {
list.add("s");
}
long end = System.currentTimeMillis();
return end-start;
}
public static long getArrayListAddTime(){
ArrayList<String> list = new ArrayList<String>();
long start = System.currentTimeMillis();
for (int i = 0; i < 771000; i++) {
list.add("s");
}
long end = System.currentTimeMillis();
return end-start;
}
public static void main(String[] args) {
System.out.println(getLinkedListAddTime());
System.out.println(getArrayListAddTime());
}
我直接用add(object o)这个方法添加的话,2个list比较添加的时间,发现反而linkedList的时间耗费的多。
然而我改成add(int i,Object o)这个方法的话,结果则是正常的,也就是大家常说的ArrayList添加耗时更多,LinkedList耗时较少。
我看了一下ArrayList的add(int i,Object o)和add(object o)基本上都是差不多的
/**
* Appends the specified element to the end of this list.
*
* @param e element to be appended to this list
* @return <tt>true</tt> (as specified by {@link Collection#add})
*/
public boolean add(E e) {
ensureCapacity(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
/**
* Inserts the specified element at the specified position in this
* list. Shifts the element currently at that position (if any) and
* any subsequent elements to the right (adds one to their indices).
*
* @param index index at which the specified element is to be inserted
* @param element element to be inserted
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public void add(int index, E element) {
if (index > size || index < 0)
throw new IndexOutOfBoundsException(
"Index: "+index+", Size: "+size);
ensureCapacity(size+1); // Increments modCount!!
System.arraycopy(elementData, index, elementData, index + 1,
size - index);
elementData[index] = element;
size++;
}
有没有懂的大牛来给小兵解答一下这个问题,非常感谢!!!!!
...全文
相关推荐
老生常谈Python之装饰器、迭代器和生成器
下面小编就为大家带来一篇老生常谈Python之装饰器、迭代器和生成器。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
Java8处理List的双层循环问题
主要介绍了Java8处理List的双层循环问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
Python 中list ,set,dict的大规模查找效率对比详解
主要介绍了Python 中list ,set,dict的大规模查找效率对比详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
详解Java8新特性Stream之list转map及问题解决
主要介绍了详解Java8新特性Stream之list转map及问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
快速解决List集合add元素,添加多个对象出现重复的问题
主要介绍了快速解决List集合add元素,添加多个对象出现重复的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧


Java SE

