java link 使用_java集合之Link的比较

news/2024/7/8 4:18:55 标签: java link 使用

第1部分 List概括

先回顾一下List的框架图

c675d5779c0eb1b7de7c2ac525382e4d.png

(01) List 是一个接口,它继承于Collection的接口。它代表着有序的队列。

(02) AbstractList 是一个抽象类,它继承于AbstractCollection。AbstractList实现List接口中除size()、get(int location)之外的函数。

(03) AbstractSequentialList 是一个抽象类,它继承于AbstractList。AbstractSequentialList 实现了“链表中,根据index索引值操作链表的全部函数”。

(04) ArrayList, LinkedList, Vector, Stack是List的4个实现类。

ArrayList 是一个数组队列,相当于动态数组。它由数组实现,随机访问效率高,随机插入、随机删除效率低。

LinkedList 是一个双向链表。它也可以被当作堆栈、队列或双端队列进行操作。LinkedList随机访问效率低,但随机插入、随机删除效率低。

Vector 是矢量队列,和ArrayList一样,它也是一个动态数组,由数组实现。但是ArrayList是非线程安全的,而Vector是线程安全的。

Stack 是栈,它继承于Vector。它的特性是:先进后出(FILO, First In Last Out)。

第2部分 List使用场景

学东西的最终目的是为了能够理解、使用它。下面先概括的说明一下各个List的使用场景,后面再分析原因。

如果涉及到“栈”、“队列”、“链表”等操作,应该考虑用List,具体的选择哪个List,根据下面的标准来取舍。

(01) 对于需要快速插入,删除元素,应该使用LinkedList。

(02) 对于需要快速随机访问元素,应该使用ArrayList。

(03) 对于“单线程环境” 或者 “多线程环境,但List仅仅只会被单个线程操作”,此时应该使用非同步的类(如ArrayList)。

对于“多线程环境,且List可能同时被多个线程操作”,此时,应该使用同步的类(如Vector)。

第4部分 Vector和ArrayList比较

相同之处

1 它们都是List

它们都继承于AbstractList,并且实现List接口。

ArrayList和Vector的类定义如下:

// ArrayList的定义

public class ArrayList extends AbstractList

implements List, RandomAccess, Cloneable, java.io.Serializable

// Vector的定义

public class Vector extends AbstractList

implements List, RandomAccess, Cloneable, java.io.Serializable {}

2 它们都实现了RandomAccess和Cloneable接口

实现RandomAccess接口,意味着它们都支持快速随机访问;

实现Cloneable接口,意味着它们能克隆自己。

3 它们都是通过数组实现的,本质上都是动态数组

ArrayList.java中定义数组elementData用于保存元素

// 保存ArrayList中数据的数组

private transient Object[] elementData;

Vector.java中也定义了数组elementData用于保存元素

// 保存Vector中数据的数组

protected Object[] elementData;

5 它们都支持Iterator和listIterator遍历

它们都继承于AbstractList,而AbstractList中分别实现了 “iterator()接口返回Iterator迭代器” 和 “listIterator()返回ListIterator迭代器”。

不同之处

1 线程安全性不一样

ArrayList是非线程安全;

而Vector是线程安全的,它的函数都是synchronized的,即都是支持同步的。

ArrayList适用于单线程,Vector适用于多线程。

2 对序列化支持不同

ArrayList支持序列化,而Vector不支持;即ArrayList有实现java.io.Serializable接口,而Vector没有实现该接口。

3 构造函数个数不同

ArrayList有3个构造函数,而Vector有4个构造函数。Vector除了包括和ArrayList类似的3个构造函数之外,另外的一个构造函数可以指定容量增加系数。

ArrayList的构造函数如下:

// 默认构造函数

ArrayList()

// capacity是ArrayList的默认容量大小。当由于增加数据导致容量不足时,容量会添加上一次容量大小的一半。

ArrayList(int capacity)

// 创建一个包含collection的ArrayList

ArrayList(Collection extends E> collection)

Vector的构造函数如下:

// 默认构造函数

Vector()

// capacity是Vector的默认容量大小。当由于增加数据导致容量增加时,每次容量会增加一倍。

Vector(int capacity)

// 创建一个包含collection的Vector

Vector(Collection extends E> collection)

// capacity是Vector的默认容量大小,capacityIncrement是每次Vector容量增加时的增量值。

Vector(int capacity, int capacityIncrement)

4 容量增加方式不同

逐个添加元素时,若ArrayList容量不足时,“新的容量”=“(原始容量x3)/2 + 1”。

而Vector的容量增长与“增长系数有关”,若指定了“增长系数”,且“增长系数有效(即,大于0)”;那么,每次容量不足时,“新的容量”=“原始容量+增长系数”。若增长系数无效(即,小于/等于0),则“新的容量”=“原始容量 x 2”。

ArrayList中容量增长的主要函数如下:

public void ensureCapacity(intminCapacity) {//将“修改统计数”+1

modCount++;int oldCapacity =elementData.length;//若当前容量不足以容纳当前的元素个数,设置 新的容量=“(原始容量x3)/2 + 1”

if (minCapacity >oldCapacity) {

Object oldData[]=elementData;int newCapacity = (oldCapacity * 3)/2 + 1;if (newCapacity

newCapacity=minCapacity;

elementData=Arrays.copyOf(elementData, newCapacity);

}

}

vector的容量增长如下:

1 private void ensureCapacityHelper(intminCapacity) {2 int oldCapacity =elementData.length;3 //当Vector的容量不足以容纳当前的全部元素,增加容量大小。4 //若 容量增量系数>0(即capacityIncrement>0),则将容量增大当capacityIncrement5 //否则,将容量增大一倍。

6 if (minCapacity >oldCapacity) {7 Object[] oldData =elementData;8 int newCapacity = (capacityIncrement > 0) ?

9 (oldCapacity + capacityIncrement) : (oldCapacity * 2);10 if (newCapacity

5 对Enumeration的支持不同。Vector支持通过Enumeration去遍历,而List不支持

Vector中实现Enumeration的代码如下:

1 public Enumerationelements() {2 //通过匿名类实现Enumeration

3 return new Enumeration() {4 int count = 0;5

6 //是否存在下一个元素

7 public booleanhasMoreElements() {8 return count

11 //获取下一个元素

12 publicE nextElement() {13 synchronized (Vector.this) {14 if (count


http://www.niftyadmin.cn/n/593952.html

相关文章

百度人为操纵搜索结果遭曝光 品牌总监离职

<iframe align"top" marginwidth"0" marginheight"0" src"http://www.zealware.com/csdnblog01.html" frameborder"0" width"728" scrolling"no" height"90"></iframe>百度人为操…

有空的时候请大家去给《Web性能测试实战》投票吧。

<iframe align"top" marginwidth"0" marginheight"0" src"http://www.zealware.com/csdnblog01.html" frameborder"0" width"728" scrolling"no" height"90"></iframe>请各位兄弟…

用flask编写自己的博客(1)

照着视频写代码&#xff01;因为底子原因&#xff0c;进度太慢&#xff0c;对其中的部分代码和知识点进行记录 一、设计models&#xff1a; ​ 本着简单的原则设计&#xff0c;只包函user、post、comments 三个表格&#xff0c;user为用户信息&#xff0c;post为文章列表&#…

Android之浮动小窗口

//创建创建全局变量类 1 public class MyApplication extends Application {2 3 /**4 * 创建全局变量 5 * 全局变量一般都比较倾向于创建一个单独的数据类文件&#xff0c;并使用static静态变量 6 * 7 * 这里使用了在Application中添加数据的方法实现全局变量 8 * 注意…

java全部小写_使Java字符串全部大写或全部小写。

的toUpperCase()使用默认语言环境的规则转换方法的所有字符的在该字符串为大写的toLowerCase()方法转换所有的在该字符串中的字符使用默认语言环境的规则为小写。示例import java.lang.*;public class StringDemo {public static void main(String[] args) {//将所有大写字母转…

Android利用VideoView实现VideoPlayer

在其他的平台上面可能VideoPlayer开发是一个比较有挑战性的工作&#xff0c;但是在Android上面VideoPlayer的开发&#xff0c;基本上可以做到傻瓜式啦。本文简单对VideoPlayer的开发进行简单的介绍。 在Android系统中&#xff0c;是通过MediaPalyer类播放媒体文件的&#xff08…

对Java多线程技术中所有方法的详细解析

一、run()和start() 这两个方法应该都比较熟悉&#xff0c;把需要并行处理的代码放在run()方法中&#xff0c;start()方法启动线程将自动调用 run()方法&#xff0c;这是由Java的内存机制规定的。并且run()方法必须是public访问权限&#xff0c;返回值类型为void。 二、关键字…

JavaScript的数组知识案例之随机点名器

本次分享JavaScript主要知识点涉及到for循环、if选择结构判断语句、数组的定义、定时器、清除定时器、日期对象的使用。 执行后效果图&#xff1a; 思路&#xff1a; 1.网页结构搭建&#xff1a; HTML 2.网页布局美化&#xff1a; CSS   3.随机功能实现: JavaScript 分析后案…