Java提高篇(三二)-----List总结
前面LZ已经充分介绍了有关于List接口的大部分知识,如ArrayList、LinkedList、Vector、Stack,通过这几个知识点可以对List接口有了比较深的了解了。只有通过归纳总结的知识才是你的知识。所以下面LZ就List接口做一个总结。推荐阅读:
一、List接口概述
List接口,成为有序的Collection也就是序列。该接口可以对列表中的每一个元素的插入位置进行精确的控制,同时用户可以根据元素的整数索引(在列表中的位置)访问元素,并搜索列表中的元素。 下图是List接口的框架图:

通过上面的框架图,可以对List的结构了然于心,其各个类、接口如下:
Collection:Collection 层次结构 中的根接口。它表示一组对象,这些对象也称为 collection 的元素。对于Collection而言,它不提供任何直接的实现,所有的实现全部由它的子类负责。
AbstractCollection:提供 Collection 接口的骨干实现,以最大限度地减少了实现此接口所需的工作。对于我们而言要实现一个不可修改的 collection,只需扩展此类,并提供 iterator 和 size 方法的实现。但要实现可修改的 collection,就必须另外重写此类的 add 方法(否则,会抛出 UnsupportedOperationException),iterator 方法返回的迭代器还必须另外实现其 remove 方法。
Iterator:迭代器。
ListIterator:系列表迭代器,允许程序员按任一方向遍历列表、迭代期间修改列表,并获得迭代器在列表中的当前位置。
List:继承于Collection的接口。它代表着有序的队列。
AbstractList:List 接口的骨干实现,以最大限度地减少实现“随机访问”数据存储(如数组)支持的该接口所需的工作。
Queue:队列。提供队列基本的插入、获取、检查操作。
Deque:一个线性 collection,支持在两端插入和移除元素。大多数 Deque 实现对于它们能够包含的元素数没有固定限制,但此接口既支持有容量限制的双端队列,也支持没有固定大小限制的双端队列。
AbstractSequentialList:提供了 List 接口的骨干实现,从而最大限度地减少了实现受“连续访问”数据存储(如链接列表)支持的此接口所需的工作。从某种意义上说,此类与在列表的列表迭代器上实现“随机访问”方法。
LinkedList:List 接口的链接列表实现。它实现所有可选的列表操作。
ArrayList:List 接口的大小可变数组的实现。它实现了所有可选列表操作,并允许包括 null 在内的所有元素。除了实现 List 接口外,此类还提供一些方法来操作内部用来存储列表的数组的大小。
Vector:实现可增长的对象数组。与数组一样,它包含可以使用整数索引进行访问的组件。
Stack:后进先出(LIFO)的对象堆栈。它通过五个操作对类 Vector 进行了扩展 ,允许将向量视为堆栈。
Enumeration:枚举,实现了该接口的对象,它生成一系列元素,一次生成一个。连续调用 nextElement 方法将返回一系列的连续元素。

二、使用场景
学习知识的根本目的就是使用它。每个知识点都有它的使用范围。集合也是如此,在Java中集合的家族非常庞大,每个成员都有最适合的使用场景。在刚刚接触List时,LZ就说过如果涉及到“栈”、“队列”、“链表”等操作,请优先考虑用List。至于是那个List则分如下:
1、对于需要快速插入、删除元素,则需使用LinkedList。
2、对于需要快速访问元素,则需使用ArrayList。
3、对于“单线程环境”或者“多线程环境,但是List仅被一个线程操作”,需要考虑使用非同步的类,如果是“多线程环境,切List可能同时被多个线程操作”,考虑使用同步的类(如Vector)。
2.1ArrayList、LinkedList性能分析
在List中我们使用最普遍的就是LinkedList和ArrayList,同时我们也了解了他们两者之间的使用场景和区别。
public class ListTest {
private static final int COUNT = 100000;
</span><span style="color: rgb(0,0,255)">private</span> <span style="color: rgb(0,0,255)">static</span> ArrayList arrayList = <span style="color: rgb(0,0,255)">new</span> ArrayList<><span style="color: rgb(0,0,0)">();
</span><span style="color: rgb(0,0,255)">private</span> <span style="color: rgb(0,0,255)">static</span> LinkedList linkedList = <span style="color: rgb(0,0,255)">new</span> LinkedList<><span style="color: rgb(0,0,0)">();
</span><span style="color: rgb(0,0,255)">private</span> <span style="color: rgb(0,0,255)">static</span> Vector vector = <span style="color: rgb(0,0,255)">new</span> Vector<><span style="color: rgb(0,0,0)">();
</span><span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(0,0,255)">static</span> <span style="color: rgb(0,0,255)">void</span><span style="color: rgb(0,0,0)"> insertToList(List list){
</span><span style="color: rgb(0,0,255)">long</span> startTime =<span style="color: rgb(0,0,0)"> System.currentTimeMillis();
</span><span style="color: rgb(0,0,255)">for</span>(<span style="color: rgb(0,0,255)">int</span> i = 0 ; i < COUNT ; i++<span style="color: rgb(0,0,0)">){
list.add(</span>0<span style="color: rgb(0,0,0)">,i);
}
</span><span style="color: rgb(0,0,255)">long</span> endTime =<span style="color: rgb(0,0,0)"> System.currentTimeMillis();
System.out.println(</span>"插入 " + COUNT + "元素" + getName(list) + "花费 " + (endTime - startTime) + " 毫秒"<span style="color: rgb(0,0,0)">);
}
</span><span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(0,0,255)">static</span> <span style="color: rgb(0,0,255)">void</span><span style="color: rgb(0,0,0)"> deleteFromList(List list){
</span><span style="color: rgb(0,0,255)">long</span> startTime =<span style="color: rgb(0,0,0)"> System.currentTimeMillis();
</span><span style="color: rgb(0,0,255)">for</span>(<span style="color: rgb(0,0,255)">int</span> i = 0 ; i < COUNT ; i++<span style="color: rgb(0,0,0)">){
list.remove(</span>0<span style="color: rgb(0,0,0)">);
}
</span><span style="color: rgb(0,0,255)">long</span> endTime =<span style="color: rgb(0,0,0)"> System.currentTimeMillis();
System.out.println(</span>"删除" + COUNT + "元素" + getName(list) + "花费 " + (endTime - startTime) + " 毫秒"<span style="color: rgb(0,0,0)">);
}
</span><span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(0,0,255)">static</span> <span style="color: rgb(0,0,255)">void</span><span style="color: rgb(0,0,0)"> readList(List list){
</span><span style="color: rgb(0,0,255)">long</span> startTime =<span style="color: rgb(0,0,0)"> System.currentTimeMillis();
</span><span style="color: rgb(0,0,255)">for</span>(<span style="color: rgb(0,0,255)">int</span> i = 0 ; i < COUNT ; i++<span style="color: rgb(0,0,0)">){
list.get(i);
}
</span><span style="color: rgb(0,0,255)">long</span> endTime =<span style="color: rgb(0,0,0)"> System.currentTimeMillis();
System.out.println(</span>"读取" + COUNT + "元素" + getName(list) + "花费 " + (endTime - startTime) + " 毫秒"<span style="color: rgb(0,0,0)">);
}
</span><span style="color: rgb(0,0,255)">private</span> <span style="color: rgb(0,0,255)">static</span><span style="color: rgb(0,0,0)"> String getName(List list) {
String name </span>= ""<span style="color: rgb(0,0,0)">;
</span><span style="color: rgb(0,0,255)">if</span>(list <span style="color: rgb(0,0,255)">instanceof</span><span style="color: rgb(0,0,0)"> ArrayList){
name </span>= "ArrayList"<span style="color: rgb(0,0,0)">;
}
</span><span style="color: rgb(0,0,255)">else</span> <span style="color: rgb(0,0,255)">if</span>(list <span style="color: rgb(0,0,255)">instanceof</span><span style="color: rgb(0,0,0)"> LinkedList){
name </span>= "LinkedList"<span style="color: rgb(0,0,0)">;
}
</span><span style="color: rgb(0,0,255)">else</span> <span style="color: rgb(0,0,255)">if</span>(list <span style="color: rgb(0,0,255)">instanceof</span><span style="color: rgb(0,0,0)"> Vector){
name </span>= "Vector"<span style="color: rgb(0,0,0)">;
}
</span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> name;
}
</span><span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(0,0,255)">static</span> <span style="color: rgb(0,0,255)">void</span><span style="color: rgb(0,0,0)"> main(String[] args) {
insertToList(arrayList);
insertToList(linkedList);
insertToList(vector);
System.out.println(</span>"--------------------------------------"<span style="color: rgb(0,0,0)">);
readList(arrayList);
readList(linkedList);
readList(vector);
System.out.println(</span>"--------------------------------------"<span style="color: rgb(0,0,0)">);
deleteFromList(arrayList);
deleteFromList(linkedList);
deleteFromList(vector);
}
}
运行结果:
插入 100000元素ArrayList花费 3900 毫秒
插入 100000元素LinkedList花费 15 毫秒
插入 100000元素Vector花费 3933 毫秒
--------------------------------------
读取100000元素ArrayList花费 0 毫秒
读取100000元素LinkedList花费 8877 毫秒
读取100000元素Vector花费 16 毫秒
--------------------------------------
删除100000元素ArrayList花费 4618 毫秒
删除100000元素LinkedList花费 16 毫秒
删除100000元素Vector花费 4759 毫秒
从上面的运行结果我们可以清晰的看出ArrayList、LinkedList、Vector增加、删除、遍历的效率问题。下面我就插入方法add(int index, E element),delete、get方法各位如有兴趣可以研究研究。
首先我们先看三者之间的源码:
ArrayList
public void add(int index, E element) {
rangeCheckForAdd(index); //检查是否index是否合法
ensureCapacityInternal(size + 1); //扩容操作
System.arraycopy(elementData, index, elementData, index + 1, size - index); //数组拷贝
elementData[index] = element; //插入
size++;
}
rangeCheckForAdd、ensureCapacityInternal两个方法没有什么影响,真正产生影响的是System.arraycopy方法,该方法是个JNI函数,是在JVM中实现的。声明如下:
public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length);
目前LZ无法看到源码,具体的实现不是很清楚,不过System.arraycopy源码分析对其进行了比较清晰的分析。但事实上我们只需要了解该方法会移动index后面的所有元素即可,这就意味着ArrayList的add(int index, E element)方法会引起index位置之后所有元素的改变,这真是牵一处而动全身。
LinkedList
public void add(int index, E element) {
checkPositionIndex(index);
</span><span style="color: rgb(0,0,255)">if</span> (index == size) <span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">插入位置在末尾</span>
linkLast(element);
else
linkBefore(element, node(index));
}
该方法比较简单,插入位置在末尾则调用linkLast方法,否则调用linkBefore方法,其实linkLast、linkBefore都是非常简单的实现,就是在index位置插入元素,至于index具体为知则有node方法来解决,同时node对index位置检索还有一个加速作用,如下:
Node<E> node(int index) {
if (index < (size >> 1)) { //如果index 小于 size/2 则从头开始查找
Node<E> x = first;
for (int i = 0; i < index; i++)
x = x.next;
return x;
} else { //如果index 大于 size/2 则从尾部开始查找
Node<E> x = last;
for (int i = size - 1; i > index; i--)
x = x.prev;
return x;
}
}
所以linkedList的插入动作比ArrayList动作快就在于两个方面。1:linkedList不需要执行元素拷贝动作,没有牵一发而动全身的大动作。2:查找插入位置有加速动作即:若index < 双向链表长度的1/2,则从前向后查找; 否则,从后向前查找。
Vector
Vector的实现机制和ArrayList一样,同样是使用动态数组来实现的,所以他们两者之间的效率差不多,add的源码也一样,如下:
public void add(int index, E element) {
insertElementAt(element, index);
}
</span><span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(0,0,255)">synchronized</span> <span style="color: rgb(0,0,255)">void</span> insertElementAt(E obj, <span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> index) {
modCount</span>++<span style="color: rgb(0,0,0)">;
</span><span style="color: rgb(0,0,255)">if</span> (index ><span style="color: rgb(0,0,0)"> elementCount) {
</span><span style="color: rgb(0,0,255)">throw</span> <span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> ArrayIndexOutOfBoundsException(index
</span>+ " > " +<span style="color: rgb(0,0,0)"> elementCount);
}
ensureCapacityHelper(elementCount </span>+ 1<span style="color: rgb(0,0,0)">);
System.arraycopy(elementData, index, elementData, index </span>+ 1, elementCount -<span style="color: rgb(0,0,0)"> index);
elementData[index] </span>=<span style="color: rgb(0,0,0)"> obj;
elementCount</span>++<span style="color: rgb(0,0,0)">;
}</span></pre>
上面是针对ArrayList、LinkedList、Vector三者之间的add(int index,E element)方法的解释,解释了LinkedList的插入动作要比ArrayList、Vector的插入动作效率为什么要高出这么多!至于delete、get两个方法LZ就不多解释了。
同时LZ在写上面那个例子时发现了一个非常有趣的现象,就是linkedList在某些时候执行add方法时比ArrayList方法会更慢!至于在什么情况?为什么会慢LZ下篇博客解释,当然不知道这个情况各位是否也遇到过??
2.2、Vector和ArrayList的区别

四、更多
-----原文出自:http://cmsblogs.com/?p=1201,请尊重作者辛勤劳动成果,转载说明出处.
-----个人站点:http://cmsblogs.com
Java提高篇(三二)-----List总结的更多相关文章
- java提高篇(二九)-----Vector
在java提高篇(二一)-–ArrayList.java提高篇(二二)-LinkedList,详细讲解了ArrayList.linkedList的原理和实现过程,对于List接口这里还介绍一个它的实现 ...
- Java提高篇(二六)-----hashCode
在前面三篇博文中LZ讲解了(HashMap.HashSet.HashTable),在其中LZ不断地讲解他们的put和get方法,在这两个方法中计算key的hashCode应该是最重要也是最 ...
- java提高篇(二二)-----LinkedList
摘自http://blog.csdn.net/chenssy/article/details/18099417 java提高篇(二二)-----LinkedList 一.概述 LinkedList与 ...
- java提高篇(二四)-----HashSet
在前篇博文中(java提高篇(二三)-----HashMap)详细讲解了HashMap的实现过程,对于HashSet而言,它是基于HashMap来实现的,底层采用HashMap来保存元素. ...
- java提高篇(二)-----理解java的三大特性之继承
在<Think in java>中有这样一句话:复用代码是Java众多引人注目的功能之一.但要想成为极具革命性的语言,仅仅能够复制代码并对加以改变是不够的,它还必须能够做更多的事情.在这句 ...
- java提高篇(二五)-----HashTable
在java中与有两个类都提供了一个多种用途的hashTable机制,他们都可以将可以key和value结合起来构成键值对通过put(key,value)方法保存起来,然后通过get(key ...
- Java提高篇(二):IO字节流、字符流和处理流
在我们的Java语言当中,通常会有对文件进行读写,因此我们引入java的IO类来进行文件的读写. 一.字节流 下面是一个字节流的实例: import java.io.*; public class I ...
- java提高篇(二一)-----ArrayList
一.ArrayList概述 ArrayList是实现List接口的动态数组,所谓动态就是它的大小是可变的.实现了所有可选列表操作,并允许包括 null 在内的所有元素.除了实现 List ...
- java提高篇(二十)-----集合大家族
在编写java程序中,我们最常用的除了八种基本数据类型,String对象外还有一个集合类,在我们的的程序中到处充斥着集合类的身影!java中集合大家族的成员实在是太丰富了,有常用的Arra ...
随机推荐
- wget下载工具
转自于:http://www.jb51.net/LINUXjishu/86326.html 1.使用wget下载单个文件 e.g. wget http://cn.wordpress.org/word ...
- C# ToString("x2")的理解
1).转化为16进制. 2).大写X:ToString("X2")即转化为大写的16进制. 3).小写x:ToString("x2")即转化为小写的16进制. ...
- 大漠绑定测试工具-VB6
获取更新开始|版本:3.1652版 2016年12月27日|更新内容:1.取消自动更新错误的提示.\n\n友情提示:如网盘失效,请加QQ群(568073679)下载最新版|下载地址:http://ww ...
- C# 在winform或者wpf中显示控制台窗口
这儿需要使用两个系统函数: BOOL WINAPI FreeConsole(void); //// 关闭控制台窗口,参考:http://msdn.microsoft.com/en-us/library ...
- 下拉框数据的动态选择,类似级联ajax刷新数据
简单的两个下拉列表,第二个中的数据与第一个下拉框相关: --------------------var selected = $(this).children('option:selected').v ...
- .NET J2EE APP全局会话架构运用场景
.NET J2EE APP全局会话架构运用场景, 全局会话运用拓扑图代码核心架构为.NET架构开发C#语言为主代码架构分为全局会话中心.ASP.NET会话节点..NET会话节点针对WCF服务器与APP ...
- Java 第18章 多态
18 章 --> 多态 继承: extends 抽象类 abstract (限制类的实例化) 抽象方法 public abstract void show(); //抽象方法只有方法的声明,没 ...
- 【分块打表】bzoj1026 [SCOI2009]windy数
#include<cstdio> using namespace std; #define BN 380000 const int table[]={0,79595,158824,2021 ...
- 【转载】Adapter用法总结大全
下面的是看到的比较好的地址: Android各种Adapter的用法: http://my.oschina.net/u/658933/blog/372151 Andro ...
- .NET 4.5+项目迁移.NET Core的问题记录
.NET 4.5+项目迁移.NET Core的问题记录 这几天试着把目前的开发框架迁移到新的.net core平台,中间遇到的问题在这里简单记录一下. 迁移过程遇到的最大的问题IOC容器.我目前使用的 ...