正确使用ArrayList和LinkedList—性能的改进

USING ArrayList AND LINKEDLIST

ArrayList and LinkedList are two Collections classes used for storing lists of object references. For example, you could have an ArrayList of Strings, or a LinkedList of Integers. This tip compares the performance of ArrayList and LinkedList, and offers some suggestions about which of these classes is the right choice in a given situation.

The first key point is that an ArrayList is backed by a primitive Object array. Because of that, an ArrayList is much faster than a LinkedList for random access, that is, when accessing arbitrary list elements using the get method. Note that the get method is implemented for LinkedLists, but it requires a sequential scan from the front or back of the list. This scan is very slow. For a LinkedList, there''s no fast way to access the Nth element of the list.

Consider the following example. Suppose you have a large list of sorted elements, either an ArrayList or a LinkedList. Suppose too that you do a binary search on the list. The standard binary search algorithm starts by checking the search key against the value in the middle of the list. If the middle value is too high, then the upper half of the list is eliminated. However, if the middle value is too low, then the lower half of the list is ignored. This process continues until the key is found in the list, or until the lower bound of the search becomes greater than the upper bound.

Here''s a program that does a binary search on all the elements in an ArrayList or a Lin

时间: 2024-09-20 04:14:03

正确使用ArrayList和LinkedList—性能的改进的相关文章

ArrayList和LinkedList的几种循环遍历方式及性能对比分析

主要介绍ArrayList和LinkedList这两种list的五种循环遍历方式,各种方式的性能测试对比,根据ArrayList和LinkedList的源码实现分析性能结果,总结结论. 通过本文你可以了解(1)List的五种遍历方式及各自性能 (2)foreach及Iterator的实现 (3)加深对ArrayList和LinkedList实现的了解. 阅读本文前希望你已经了解ArrayList顺序存储和LinkedList链式的结构,本文不对此进行介绍. 相关:HashMap循环遍历方式及其性

Java中ArrayList和LinkedList的遍历与性能分析_java

前言 通过本文你可以了解List的五种遍历方式及各自性能和foreach及Iterator的实现,加深对ArrayList和LinkedList实现的了解.下面来一起看看吧. 一.List的五种遍历方式 1.for each循环 List<Integer> list = new ArrayList<Integer>(); for (Integer j : list) { // use j } 2.显示调用集合迭代器 List<Integer> list = new Ar

比较List和ArrayList的性能及ArrayList和LinkedList优缺点

List和ArrayList的性能比较 在使用ArrayList这样的非泛型集合的过程中,要进行装箱和拆箱操作,会有比较大的性能损失,而使用泛型集合就没有这样的问题.List是泛型,而ArrayList是非泛型.存数据岛ArrayList都需要专程object,读取又要转换成相应的数据类型,List则不需要. //用来记录开始和结束的时间  DateTime startTime = new DateTime();  DateTime endTime = new DateTime(); //定义集

[Java] ArrayList、LinkedList、Vector的区别

版权声明:请尊重个人劳动成果,转载注明出处,谢谢! 首先我们来看一下继承关系: 我们可以看出ArrayList.LinkedList.Vector都实现了List的接口.  接下来分别看一下三个数据结构的说明. public class ArrayList extends AbstractList  implements List, RandomAccess, Cloneable, Serializable List 接口的大小可变数组的实现.实现了所有可选列表操作,并允许包括 null 在内的

Java 容器 &amp; 泛型:二、ArrayList 、LinkedList和Vector比较

一.List回顾 序列(List),有序的Collection,正如它的名字一样,是一个有序的元素列表.确切的讲,列表通常允许满足 e1.equals(e2) 的元素对 e1 和 e2,并且如果列表本身允许 null 元素的话,通常它们允许多个 null 元素.实现List的有:ArrayList.LinkedList.Vector.Stack等.值得一提的是,Vector在JDK1.1的时候就有了,而List在JDK1.2的时候出现,待会我们会聊到ArrayList和Vector的区别.  

.NET Framework源码研究系列之---ArrayList与LinkedList

在上一篇<.NET Framework源码研究系列之---马甲List>中我们一起研究了.NET中 List的源代码,也得到一些兄弟的热心反馈.其中一位仁兄说希望看到ArrayList与LinkedList源 代码,所以今天就以此为话题,大家一起看一下.NET中是如何实现ArrayList和LinkedList 的. 我们先看ArrayList和LinkedList在.NET中的位置,ArrayList的命名空间是 System.Collections,LinkedList的命名空间是Syst

java集合框架05——ArrayList和LinkedList的区别

本文为博主原创文章,转载请注明出处:http://blog.csdn.net/eson_15/article/details/51145788 前面已经学习完了List部分的源码,主要是ArrayList和LinkedList两部分内容,这一节主要总结下List部分的内容. List概括         先来回顾一下List在Collection中的的框架图:     从图中我们可以看出:         1. List是一个接口,它继承与Collection接口,代表有序的队列.       

ArrayList和LinkedList的区别

一般大家都知道ArrayList和LinkedList的大致区别:1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构.2.对于随机访问get和set,ArrayList觉得优于LinkedList,因为LinkedList要移动指针.3.对于新增和删除操作add和remove,LinedList比较占优势,因为ArrayList要移动数据.    这一点要看实际情况的.若只对单条数据插入或删除,ArrayList的速度反而优于LinkedList.但若是

Java中ArrayList和LinkedList区别

一般大家都知道ArrayList和LinkedList的大致区别: 1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构. 2.对于随机访问get和set,ArrayList觉得优于LinkedList,因为LinkedList要移动指针. 3.对于新增和删除操作add和remove,LinedList比较占优势,因为ArrayList要移动数据. ArrayList和LinkedList是两个集合类,用于存储一系列的对象引用(references).例如