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 ...
随机推荐
- ue4 重新生成ide project文件的命令行
有时候换了机器,工程文件没了,通常是在编辑器里有个菜单项可以生成 但是有时编辑器自身都编不过,没法运行 这时需要调试代码,可以用命令行生成相应的工程文件: ../UnrealEngine/Genera ...
- VC++修改电脑系统时间
我们可以通过代码,对系统时间进行获取和修改. 我将介绍两种方法.第一种,通过SYSTEMTIME 代码实现: #include <windows.h> #include <iostr ...
- AndroidLinker与SO加壳技术之上篇
1. 前言 Android 系统安全愈发重要,像传统pc安全的可执行文件加固一样,应用加固是Android系统安全中非常重要的一环.目前Android 应用加固可以分为dex加固和Native加固,N ...
- Android安全开发之UXSS漏洞分析
0X01 前言 XSS是我们比较熟悉的一种攻击方式,包括存储型XSS.反射型XSS.DOM XSS等,但UXSS(通用型XSS)另外一种不同的漏洞类型,主要体现在漏洞的载体和影响范围上. XSS问题源 ...
- C#方法中三个重要的参数:out、ref、params
备注:适用于初学者,自学于传智播客. 1.out参数. 概念:如果在一个方法中,返回多个相同类型值的时候,可以考虑返回一数组.但是返回多个不同类型值的时候,返回数组显然不能解决问题,这时就引入out参 ...
- 判断AngularJS渲染页面完成
$scope.$on('$viewContentLoaded', function(){ }); // 或者 $scope.$watch('$viewContentLoaded', function( ...
- 高流量站点NGINX与PHP-fpm配置优化(译)
使用Nginx搭配PHP已有7年的这份经历让我们学会如何为高流量站点优化NGINX和PHP-fpm配置. 以下正是这方面的一些提示和建议: 1. 将TCP切换为UNIX域套接字 UNIX域套接字相比T ...
- URAL1132_Square Root
求解方程,x^2=n (mod P). 解二次同余方程的步骤: 1.首先判断勒让德符号(n,p)是否的等于1,即n^((p-1/2)=1 (mod p)是否成立.不成立显然无解.(略) 2.任取0-( ...
- 【Java学习笔记】Map接口的子接口---TreeMap
TreeMap,与TreeSet类似,可以对集合中的元素进行排序,同时保持元素的唯一性. 应注意,Comparable(实现接口,记得覆盖comparaTo方法),Comparator的使用. imp ...
- Android中的桌面快捷方式
一.判断是否已有快捷方式 private String getAuthorityFromPermission(Context context, String permission){ if (perm ...