jdk list接口源码解析
一、实现类
List接口一共有三个实现类,分别是ArrayList,Vector和LinkedList。List用于存放多个元素,能够维护元素的次序,并且允许元素的重复。三个具体实现类的相关区别如下:
- ArrayList是最常用的List实现类,内部是通过数组实现的,它允许对元素进行快速随机访问。数组的缺点是每个元素之间不能有间隔,当数组大小不满足时需要增加存储能力,就要将已经有数组的数据复制到新的存储空间中。当从ArrayList的中间位置插入或者删除元素时,需要对数组进行复制、移动、代价比较高。因此,它适合随机查找和遍历,不适合插入和删除。
- Vector与ArrayList一样,也是通过数组实现的,不同的是它支持线程的同步,即某一时刻只有一个线程能够写Vector,避免多线程同时写而引起的不一致性,但实现同步需要很高的花费,因此,访问它比访问ArrayList慢。
- LinkedList是用链表结构存储数据的,很适合数据的动态插入和删除,随机访问和遍历速度比较慢。另外,他还提供了List接口中没有定义的方法,专门用于操作表头和表尾元素,可以当作堆栈、队列和双向队列使用。
- 如果集合中的元素的数目大于目前集合数组的长度时,vector增长率为目前数组长度的100%,而arraylist增长率为目前数组长度的50%.如过在集合中使用数据量比较大的数据,用vector有一定的优势。
二、方法
size
返回list中元素的个数,如果元素个数多于Integer.MAX_VALUE,则返回Integer.MAX_VALUE.
int size();
isEmpty
如果list中不包含任何元素,返回true
boolean isEmpty();
contains
如果list中包含(至少一个)特定的元素,返回true。当o为null时查找list是否存储了null。否则寻找对象e使得o.equals(e)
* @throws ClassCastException if the type of the specified element* is incompatible with this list* (<a href="Collection.html#optional-restrictions">optional</a>)* @throws NullPointerException if the specified element is null and this* list does not permit null elements* (<a href="Collection.html#optional-restrictions">optional</a>)*/boolean contains(Object o);
toArray
返回一个包含list所有元素的数组,顺序和原list存储顺序一致。
* <p>The returned array will be "safe" in that no references to it are* maintained by this list. (In other words, this method must* allocate a new array even if this list is backed by an array).* The caller is thus free to modify the returned array.Object[] toArray();
add
/*** Appends the specified element to the end of this list (optional* operation).** <p>Lists that support this operation may place limitations on what* elements may be added to this list. In particular, some* lists will refuse to add null elements, and others will impose* restrictions on the type of elements that may be added. List* classes should clearly specify in their documentation any restrictions* on what elements may be added.** @param e element to be appended to this list* @return <tt>true</tt> (as specified by {@link Collection#add})* @throws UnsupportedOperationException if the <tt>add</tt> operation* is not supported by this list* @throws ClassCastException if the class of the specified element* prevents it from being added to this list* @throws NullPointerException if the specified element is null and this* list does not permit null elements* @throws IllegalArgumentException if some property of this element* prevents it from being added to this list*/boolean add(E e);
remove
/*** Removes the first occurrence of the specified element from this list,* if it is present (optional operation). If this list does not contain* the element, it is unchanged. More formally, removes the element with* the lowest index <tt>i</tt> such that* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>* (if such an element exists). Returns <tt>true</tt> if this list* contained the specified element (or equivalently, if this list changed* as a result of the call).** @param o element to be removed from this list, if present* @return <tt>true</tt> if this list contained the specified element* @throws ClassCastException if the type of the specified element* is incompatible with this list* (<a href="Collection.html#optional-restrictions">optional</a>)* @throws NullPointerException if the specified element is null and this* list does not permit null elements* (<a href="Collection.html#optional-restrictions">optional</a>)* @throws UnsupportedOperationException if the <tt>remove</tt> operation* is not supported by this list*/boolean remove(Object o);
ListIterator
/*** Returns a list iterator over the elements in this list (in proper* sequence).** @return a list iterator over the elements in this list (in proper* sequence)*/ListIterator<E> listIterator();
/*** Returns a list iterator over the elements in this list (in proper* sequence), starting at the specified position in the list.* The specified index indicates the first element that would be* returned by an initial call to {@link ListIterator#next next}.* An initial call to {@link ListIterator#previous previous} would* return the element with the specified index minus one.** @param index index of the first element to be returned from the* list iterator (by a call to {@link ListIterator#next next})* @return a list iterator over the elements in this list (in proper* sequence), starting at the specified position in the list* @throws IndexOutOfBoundsException if the index is out of range* ({@code index < 0 || index > size()})*/ListIterator<E> listIterator(int index);
jdk list接口源码解析相关推荐
- java迭代器创建后mutx锁,java集合【5】—— Collections接口源码解析
一.Collections接口是做什么的? 用官网文档的介绍:The polymorphic algorithms described here are pieces of reusable func ...
- JDK源码解析——Object的hashCode方法
目录 前言 说明 一.源码目录结构 (1).JDK目录 (2).hotspot目录 二.基础知识 (1).Object Header(对象头) (2).Lock(锁) 1. 无锁 => 偏向锁 ...
- Spring源码解析 - AbstractBeanFactory 实现接口与父类分析
2019独角兽企业重金招聘Python工程师标准>>> 我们先来看类图吧: 除了BeanFactory这一支的接口,AbstractBeanFactory主要实现了AliasRegi ...
- JDK源码解析 迭代器模式在JAVA的很多集合类中被广泛应用,接下来看看JAVA源码中是如何使用迭代器模式的。
JDK源码解析 迭代器模式在JAVA的很多集合类中被广泛应用,接下来看看JAVA源码中是如何使用迭代器模式的. 看完这段代码是不是很熟悉,与我们上面代码基本类似.单列集合都使用到了迭代器,我们以Arr ...
- JDK源码解析 Comparator 中的策略模式
JDK源码解析 Comparator 中的策略模式.在Arrays类中有一个 sort() 方法,如下: public class Arrays{public static <T> voi ...
- JDK源码解析之Java.util.Collections
java.util.Collections 是一个包装类.它包含有各种有关集合操作的静态多态方法.此类不能实例化,就像一个工具类,服务于Java的Collection框架. 一.源码解析 1.不可实例 ...
- JDK源码解析之Java.util.Collection
Collection是单例集合的顶层接口,它表示一组对象,这些对象也称为Collection的元素,JDK 不提供此接口的任何直接实现,它提供更具体的子接口(如Set和List)实现 一.源码解析 1 ...
- JDK源码解析--Object类
作为一名java开发,肯定会知道object类,object类是所有类的基类,当一个类没有直接继承任何类时,默认继承object类,所以也被称之为"上帝类". 目录 一.继承Obj ...
- aop实现原理 - JDK动态代理(实例+源码解析)
动态代理: jdk代理-基于接口代理 通过 类:java.lang.reflect.Proxy 生成动态代理类 实现 接口:InvocationHandler 只能基于接口进行动态代理 代码实现: 1 ...
- 深入探究JDK中Timer的使用方式与源码解析
导言 定时器Timer的使用 构造方法 实例方法 使用方式 1. 执行时间晚于当前时间 2. 执行时间早于当前时间 3. 向Timer中添加多个任务 4. 周期性执行任务 5. 停止任务 源码解析 T ...
最新文章
- 数据库维护优化及后期改进约定.实践篇
- Dubbo源码分析(六)服务引用的具体流程
- 数据特征分析(学习笔记)
- mysql制作html静态网页6_将数据库中的所有内容生成html静态页面的代码
- Py之av:av库的简介、安装、使用方法之详细攻略
- fx2n4ad模块中文手册_三菱特殊模块FX2N-4AD-PT详细说明及编程应用
- Qt 中 QXml/QDom*** api设计吐槽
- sonar 集群环境工作机制的深入理解
- 深入理解redis数据类型
- checkbox保存和赋值
- 怎么通过id渲染页面_完全理解Vue的渲染watcher、computed和user watcher
- cocos2dx 3.0研究(1)-- hello world程序
- 【codevs2098】【Tyvj1625】化工厂装箱员,煞笔的人打煞笔的DP
- BZOJ1096[ZJOI2007] 仓库建设
- python 复制到剪贴板_Python脚本将文本复制到剪贴板
- CCSK云计算安全认证
- ifconfig创建sit隧道
- 4.5.2 地址变换机构 4.5.3 访问内存的有效时间
- 8弦金属摇滚电吉他音源 Orange Tree Samples Evolution Dracus Kontakt
- 揭秘Mysql事务隔离级别之可重复读
热门文章
- PHP递归写入MySQL无限级分类数据
- linux boot 空间不足,解决Ubuntu 提示boot分区空间不足办法
- java componentorientation_Java JLabel.applyComponentOrientation方法代码示例
- C语言多线程基础-01-线程的创建与销毁
- Glide 4.x工作总体执行流程概述
- 用了这么久的 Chrome,你不会还没掌握这个功能吧?
- 爱奇艺Android移动客户端app瘦身经验
- html盒子中盒子排列,解析CSS的box model盒模型及其内的子元素布局控制
- 本html添加可信站点,js实现添加可信站点、修改activex安全设置,禁用弹出窗口阻止程序...
- python最小生成树算法_最小生成树:Kruskal算法及python实现