ArrayList 和 LinkedList的遍历性能差异
来源:互联网 发布:炫舞按键精灵源码 编辑:程序博客网 时间:2024/06/01 08:04
众所周知,ArrayList采用数组结构适用于随机读取,LinkedList采用双向链表结构适用于插入删除;但对于只用于遍历的情况下,他们之间的性能又有什么差异呢?
其实这是邮遍历的方式决定的。
List的遍历方式可以分为5种, 具体如下:
List<E> list = new xxx()
- foreach
for(E e: list) { ````}
- for +iterator
for(Iterator<E> iterator = list.iterator; iterator.hasNext();) { ```}
- while + iterator
Iterator<E> iterator = list.iterator;while(iterator.hasNext();) { ```}
- fori
for(int i = 0; i < list.size(); i++) { list.get(i);}
- fori +list.size()
int i = list.size();for(int i = 0; i < size; i++) { list.get(i);}
1,2,3都是采用iterator的方式来遍历,iterator针对arrayList和linkedList分别做了各自的实现,遍历的时间开销都是O(n);
4,5在遍历的时候会有如下不同:
对ArrayList而言list.get(i)开销为O(1), 加上外层for,总开销为O(n)
对LinkedList而言list.get(i)开销为O(n), 加上外层for, 总开销为O(n²)
所以从代码风格和性能统一考虑,尽量多用iterator来做遍历
0 0
- ArrayList 和 LinkedList的遍历性能差异
- LinkedList、Vector和ArrayList之间的性能差异
- LinkedList 与ArrayList的性能差异
- ArrayList和LinkedList的操作时间的差异
- ArrayList和LinkedList的操作性能对比
- ArrayList和LinkedList的操作性能对比
- Arraylist和linkedlist的性能测试
- ArrayList和LinkedList的操作性能对比
- 【java】数组、arraylist、linkedlist随机访问和遍历性能比较
- ArrayList和LinkedList遍历方式及性能对比分析
- ArrayList和LinkedList的几种循环遍历方式及性能对比分析
- ArrayList和LinkedList的几种循环遍历方式及性能对比分析
- ArrayList和LinkedList的几种循环遍历方式及性能对比分析
- ArrayList和LinkedList的几种循环遍历方式及性能对比分析
- ArrayList和LinkedList的几种循环遍历方式及性能对比分析
- ArrayList和LinkedList的几种循环遍历方式及性能对比分析
- ArrayList和LinkedList的几种循环遍历方式及性能对比分析
- ArrayList和LinkedList的几种循环遍历方式及性能对比分析
- php的cookie和session小应用
- linux下实用的指令
- leetcode51&52 N-Queens
- Swift如何检测iCloud已登录(在线)
- codeforces Round 373 div2 C. Efim and Strange Grade 模拟+ priority_queue
- ArrayList 和 LinkedList的遍历性能差异
- 1227: Gang Black and Gang White
- 关于Ubuntu
- AngularJS系列之ES6特性(二)
- BZOJ1101——[POI2007]Zap
- 13 :单机 :容器连接
- Java线程池使用说明
- 2016 noip 游记
- GPS nmealib学习笔记