LinkedList ArrayList 比较
小结:
1、不是同步的,多线程情况下的处理
List list = Collections.synchronizedList(new LinkedList(...));
2、
快速失败、并发修改异常
3、
LinkedList (Java Platform SE 8 ) https://docs.oracle.com/javase/8/docs/api/java/util/LinkedList.html
ArrayList (Java Platform SE 8 ) https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html
@0
Class ArrayList<E>
- java.lang.Object
- java.util.AbstractCollection<E>
- java.util.AbstractList<E>
- java.util.ArrayList<E>
- All Implemented Interfaces:
- Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess
- Direct Known Subclasses:
- AttributeList, RoleList, RoleUnresolvedList
public class ArrayList<E>
extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, Serializable
Class LinkedList<E>
- java.lang.Object
- java.util.AbstractCollection<E>
- java.util.AbstractList<E>
- java.util.AbstractSequentialList<E>
- java.util.LinkedList<E>
- Type Parameters:
E- the type of elements held in this collection
- All Implemented Interfaces:
- Serializable, Cloneable, Iterable<E>, Collection<E>, Deque<E>, List<E>, Queue<E>
public class LinkedList<E>
extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, Serializable
@1
Note that this implementation is not synchronized. If multiple threads access a linked list concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more elements; merely setting the value of an element is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the list. If no such object exists, the list should be "wrapped" using the Collections.synchronizedList method. This is best done at creation time, to prevent accidental unsynchronized access to the list:
List list = Collections.synchronizedList(new LinkedList(...));
Note that this implementation is not synchronized. If multiple threads access an ArrayList instance concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more elements, or explicitly resizes the backing array; merely setting the value of an element is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the list. If no such object exists, the list should be "wrapped" using the Collections.synchronizedList method. This is best done at creation time, to prevent accidental unsynchronized access to the list:
List list = Collections.synchronizedList(new ArrayList(...));
@2
The iterators returned by this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw aConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.
Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationExceptionon a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.
@3
public class LinkedList<E>
extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, Serializable
List and Deque interfaces. Implements all optional list operations, and permits all elements (including null).
All of the operations perform as could be expected for a doubly-linked list. Operations that index into the list will traverse the list from the beginning or the end, whichever is closer to the specified index.
public class ArrayList<E>
extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, Serializable
The size, isEmpty, get, set, iterator, and listIterator operations run in constant time. The add operation runs in amortized constant time, that is, adding n elements requires O(n) time. All of the other operations run in linear time (roughly speaking). The constant factor is low compared to that for the LinkedList implementation.
Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList, its capacity grows automatically. The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost.
An application can increase the capacity of an ArrayList instance before adding a large number of elements using the ensureCapacity operation. This may reduce the amount of incremental reallocation.
java集合之ArrayList源码解读 https://mp.weixin.qq.com/s/dy98Y-LyQw1BbbH3_X7nBw
// ArrayList动态扩容机制的核心
private void grow(int minCapacity) {
// 可能存在整型溢出
int oldCapacity = elementData.length;
// 容量默认扩大1.5倍
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity < 0)
// 可能1:newCapacity<0整型溢出
// 可能2:newCapacity<minCapacity
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
// 数组深拷贝
elementData = Arrays.copyOf(elementData, newCapacity);
}
Java集合源码分析(一)ArrayList - LanceToBigData - 博客园 https://www.cnblogs.com/zhangyinhua/p/7687377.html
深入分析Java的序列化与反序列化-HollisChuang's Blog https://www.hollischuang.com/archives/1140
LinkedList ArrayList 比较的更多相关文章
- Java 集合系列08之 List总结(LinkedList, ArrayList等使用场景和性能分析)
概要 前面,我们学完了List的全部内容(ArrayList, LinkedList, Vector, Stack). Java 集合系列03之 ArrayList详细介绍(源码解析)和使用示例 Ja ...
- Java 集合系列 07 List总结(LinkedList, ArrayList等使用场景和性能分析)
java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...
- JAVA之旅(十九)——ListIterator列表迭代器,List的三个子类对象,Vector的枚举,LinkedList,ArrayList和LinkedList的小练习
JAVA之旅(十九)--ListIterator列表迭代器,List的三个子类对象,Vector的枚举,LinkedList,ArrayList和LinkedList的小练习 关于数据结构,所讲的知识 ...
- Java集合类学习-LinkedList, ArrayList, Stack, Queue, Vector
Collection List 在Collection的基础上引入了有序的概念,位置精确:允许相同元素.在列表上迭代通常优于索引遍历.特殊的ListIterator迭代器允许元素插入.替换,双向访问, ...
- LintCode Reverse LinkedList (ArrayList 和 LinkedList 的区别)
1. ArrayList 和 LinkedList 的区别 http://pengcqu.iteye.com/blog/502676 2. How to reverse LinkedList http ...
- Java _ JDK _ Arrays, LinkedList, ArrayList, Vector 及Stack
(最近在看JDK源码,只是拿着它的继承图在看,但很多东西不记录仍然印象不深,所以开始记录JDK阅读系列.) (一)Arrays Arrays比较特殊,直接继承自Arrays ->List(Int ...
- 【转】Java 集合系列08之 List总结(LinkedList, ArrayList等使用场景和性能分析)
概要 前面,我们学完了List的全部内容(ArrayList, LinkedList, Vector, Stack). Java 集合系列03之 ArrayList详细介绍(源码解析)和使用示例 Ja ...
- LinkedList,ArrayList末尾插入谁效率高?
废话不多说,原因不解释.上測试代码: package com.letv.cloud.cdn.jtest; import java.io.IOException; import java.util.Ar ...
- LinkedList,ArrayList,Vector,HashMap,HashSet,HashTable之间的区别与联系
在编写java程序中,我们最常用的除了八种基本数据类型,String对象外还有一个集合类,在我们的的程序中到处充斥着集合类的身影!java中集合大家族的成员实在是太丰富了,有常用的ArrayList. ...
随机推荐
- 彻底关闭Google的安全搜索
在使用简体中文的情况下,访问Google总是会跳转到香港,这个时候的安全搜索是无法关闭的. 下面介绍一个最简单的方法,直接使用Google的中文界面:https://www.google.com/we ...
- PDF XSS
漏洞测试: 下面,我们介绍如何把 JavaScript 嵌入到 PDF 文档之中.我使用的是迅捷 PDF 编辑器未注册版本 1.启动迅捷 PDF 编辑器打开一个 PDF 文件,或者使用“创建 PDF ...
- ubuntu11.10 64bit 编译android 4.0
前言: 据说google内部使用的的ubuntu版本始终是10.4, 而我一直使用的编译2.3Gingerbread的 11.04下补充安装uuid-dev和liblzo2-dev两个库就可以直接编译 ...
- codeforces水题100道 第十二题 Codeforces Beta Round #91 (Div. 2 Only) A. Lucky Division (brute force)
题目链接:http://www.codeforces.com/problemset/problem/122/A题意:判断一个数是否能被一个lucky number整除,一个lucky number是一 ...
- iOS - UITableViewStylePlain与UITableViewStyleGroup样式的对比
一.UITableViewStylePlain 1.有多段时 段头停留(自带效果) 2.没有中间的间距和头部间距(要想有的重写UITableViewCell \UITableViewHeaderFoo ...
- windos或linux中 which命令 查看当前要执行的命令所在的路径
whereis 用来查看一个命令或者文件所在的绝对路径,而 which 用来查看当前要执行的命令所在的路径. 下面举个例子来说明.加入你的linux系统上装了多个版本的java.如果你直接在命令行敲命 ...
- [转]java中判断字符串是否为数字的三种方法
1用JAVA自带的函数public static boolean isNumeric(String str){ for (int i = str.length();--i>=0;){ ...
- Android设计和开发系列第二篇:Navigation Drawer(Develop)
Creating a Navigation Drawer THIS LESSON TEACHES YOU TO: Create a Drawer Layout Initialize the Drawe ...
- 【cs229-Lecture11】贝叶斯统计正则化
本节知识点: 贝叶斯统计及规范化 在线学习 如何使用机器学习算法解决具体问题:设定诊断方法,迅速发现问题 贝叶斯统计及规范化(防止过拟合的方法) 就是要找更好的估计方法来减少过度拟合情况的发生. 回顾 ...
- 七、K3 WISE 开发插件《Update字段级更新触发器 - BOS单审核后反写源单》
审核成功触发,是一个比较典型的场景.需要用到update触发器,跟踪到审核状态的变化. 引用的源码<采购检验单审核后反写收料通知单>,其中采购检验单是BOS自定义单据. if (objec ...