Java Collection - ArrayList LinkedList

2024-06-08 08:38

本文主要是介绍Java Collection - ArrayList LinkedList,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

ArrayList

基础属性:

transient Object[] elementData; 存储数据的数组

private static final int DEFAULT_CAPACITY = 10; 默认长度

private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {}; default sized empty instances

1. new ArrayList<>()

/*** Constructs an empty list with an initial capacity of ten.默认的数组长度是10*/
public ArrayList() {this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}

2. add方法 - 增

    public boolean add(E e) {ensureCapacityInternal(size + 1);  // Increments modCount!!//放在数组的最后elementData[size++] = e;return true;}private void ensureCapacityInternal(int minCapacity) {ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));}private void ensureExplicitCapacity(int minCapacity) {//并发标识位modCount++;// overflow-conscious codeif (minCapacity - elementData.length > 0)//扩容!!grow(minCapacity);}private void grow(int minCapacity) {// overflow-conscious codeint oldCapacity = elementData.length;//扩容为原始长度的1.5倍。 >>右移位符号,移动一位相当于除以2。int newCapacity = oldCapacity + (oldCapacity >> 1);if (newCapacity - minCapacity < 0)newCapacity = minCapacity;if (newCapacity - MAX_ARRAY_SIZE > 0)newCapacity = hugeCapacity(minCapacity);// minCapacity is usually close to size, so this is a win:elementData = Arrays.copyOf(elementData, newCapacity);}//使用Arrays工具类的copyOf方法,底层使用的是System.arraycopy的native方法。可能会造成Heap space OOM

3. remove方法 - 删(有多个方法)

     public E remove(int index) {//检查下标,防止越界rangeCheck(index);modCount++;E oldValue = elementData(index);int numMoved = size - index - 1;if (numMoved > 0)//index之后的数据全部复制到index的位置。这一步较为费时,频繁的删除时不应该使用ArrayList!System.arraycopy(elementData, index+1, elementData, index,numMoved);//将数组指向null,方便GCelementData[--size] = null; // clear to let GC do its workreturn oldValue;}

4. set方法 - 改

public E set(int index, E element) {//检查下标后直接更新rangeCheck(index);E oldValue = elementData(index);elementData[index] = element;return oldValue;
}

5. get方法 -查

	//1、知道下标时  public E get(int index) {//大于等于size时,抛出IndexOutOfBoundsExceptionrangeCheck(index);//返回对应下标下的数据return elementData(index);}

LinkedList

基础属性:

transient Node first; 头结点

transient Node last; 尾节点

Node是一个内部类,定义如下:

	private static class Node<E> {E item;Node<E> next;Node<E> prev;Node(Node<E> prev, E element, Node<E> next) {this.item = element;this.next = next;this.prev = prev;}}

1. new LinkedList<>();

/*** Constructs an empty list.*/
public LinkedList() {
}

2. 增 - add

    public boolean add(E e) {linkLast(e);return true;}//采用尾插法,将插入的节点作为last节点void linkLast(E e) {final Node<E> l = last;final Node<E> newNode = new Node<>(l, e, null);last = newNode;if (l == null)first = newNode;elsel.next = newNode;size++;modCount++;}

3. 删 - remove

   //remove指定index的public E remove(int index) {//检查下标,越界则抛出异常checkElementIndex(index);return unlink(node(index));}//获取指定index下的NodeNode<E> node(int index) {// assert isElementIndex(index);//可以看到此处作了优化,根据index在size的前半段或后半段,分别从前往后或从后往前查找if (index < (size >> 1)) {Node<E> x = first;for (int i = 0; i < index; i++)x = x.next;return x;} else {Node<E> x = last;for (int i = size - 1; i > index; i--)x = x.prev;return x;}}//释放指定的NodeE unlink(Node<E> x) {// assert x != null;final E element = x.item;final Node<E> next = x.next;    //Node的前置节点nextfinal Node<E> prev = x.prev;    //Node的后置节点prev//Node是初始节点,将next置为first节点if (prev == null) {first = next;} else {//Node不是初始节点prev.next = next;x.prev = null;}//Node是尾节点,将前置节点作为尾节点if (next == null) {last = prev;} else {//Node不是尾节点,设置其前置节点next.prev = prev;x.next = null;}//Node变为了null,方便GC回收x.item = null;size--;modCount++;return element;}

可以看到删除时仅仅移动了链表,不像ArrayList一样对数组进行了Copy。

4. 改 - set

改特定位置的Node的数据

public E set(int index, E element) {//下面这两个方法很眼熟吧 :)checkElementIndex(index);Node<E> x = node(index);E oldVal = x.item;x.item = element;return oldVal;
}

5. 查 - get

public E get(int index) {checkElementIndex(index);return node(index).item;
}
  • 查找时ArrayList比LinkedList效率高,因为数组结构的遍历只需将下标加一,而链表结构则从头查找;
  • 对元素进行增删时LinedList会比ArrayList好用,链表结构可以很轻松的在链表中间去增删元素,而不需要改变插入节点后面元素的任何数据。

遍历

  • for循环遍历,实际上是使用get(int index)方法进行查找元素,最基础的

  • 迭代器,ArrayList和LinkedList均有内部类实现了ListIterator接口,如LinkedList:

      private class ListItr implements ListIterator<E> {private Node<E> lastReturned;private Node<E> next;private int nextIndex;private int expectedModCount = modCount;...省略部分代码public boolean hasNext() {return nextIndex < size;}public E next() {checkForComodification();if (!hasNext())throw new NoSuchElementException();lastReturned = next;next = next.next;nextIndex++;return lastReturned.item;}...省略public void remove() {checkForComodification();if (lastReturned == null)throw new IllegalStateException();Node<E> lastNext = lastReturned.next;unlink(lastReturned);if (next == lastReturned)next = lastNext;elsenextIndex--;lastReturned = null;expectedModCount++;}public void set(E e) {if (lastReturned == null)throw new IllegalStateException();checkForComodification();lastReturned.item = e;}public void add(E e) {checkForComodification();lastReturned = null;if (next == null)linkLast(e);elselinkBefore(e, next);nextIndex++;expectedModCount++;}
    

如果想一边迭代一边用集合自带的方法进行删除或者新增操作,都会抛出ConcurrentModificationException异常(在增加和删除元素的时候,都会进行自增操作 modCount)。所以在遍历时需要增删list元素的,则需要使用迭代器。

  • forEach,ArrayList使用的是数组下标:
    @Overridepublic void forEach(Consumer<? super E> action) {Objects.requireNonNull(action);final int expectedModCount = modCount;@SuppressWarnings("unchecked")final E[] elementData = (E[]) this.elementData;final int size = this.size;for (int i=0; modCount == expectedModCount && i < size; i++) {action.accept(elementData[i]);}if (modCount != expectedModCount) {throw new ConcurrentModificationException();}}

Best Practice:

  • 实现了RadmoAcces接口的list(如ArrayList),优先选择普通for循环(使用下标直接随机读取) ,其次list.foreach方法或增强型的for循环
  • 未实现RadmoAcces接口的list(如LinkedList), 优先选择iterator遍历,移动指针(foreach遍历底层也是通过iterator实现的),大size的数据,千万不要使用普通for循环(每次查找都要移动大量数据)

这篇关于Java Collection - ArrayList LinkedList的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1041691

相关文章

Java中ArrayList和LinkedList有什么区别举例详解

《Java中ArrayList和LinkedList有什么区别举例详解》:本文主要介绍Java中ArrayList和LinkedList区别的相关资料,包括数据结构特性、核心操作性能、内存与GC影... 目录一、底层数据结构二、核心操作性能对比三、内存与 GC 影响四、扩容机制五、线程安全与并发方案六、工程

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

Java调用DeepSeek API的最佳实践及详细代码示例

《Java调用DeepSeekAPI的最佳实践及详细代码示例》:本文主要介绍如何使用Java调用DeepSeekAPI,包括获取API密钥、添加HTTP客户端依赖、创建HTTP请求、处理响应、... 目录1. 获取API密钥2. 添加HTTP客户端依赖3. 创建HTTP请求4. 处理响应5. 错误处理6.

Spring AI集成DeepSeek的详细步骤

《SpringAI集成DeepSeek的详细步骤》DeepSeek作为一款卓越的国产AI模型,越来越多的公司考虑在自己的应用中集成,对于Java应用来说,我们可以借助SpringAI集成DeepSe... 目录DeepSeek 介绍Spring AI 是什么?1、环境准备2、构建项目2.1、pom依赖2.2

Spring Cloud LoadBalancer 负载均衡详解

《SpringCloudLoadBalancer负载均衡详解》本文介绍了如何在SpringCloud中使用SpringCloudLoadBalancer实现客户端负载均衡,并详细讲解了轮询策略和... 目录1. 在 idea 上运行多个服务2. 问题引入3. 负载均衡4. Spring Cloud Load

Springboot中分析SQL性能的两种方式详解

《Springboot中分析SQL性能的两种方式详解》文章介绍了SQL性能分析的两种方式:MyBatis-Plus性能分析插件和p6spy框架,MyBatis-Plus插件配置简单,适用于开发和测试环... 目录SQL性能分析的两种方式:功能介绍实现方式:实现步骤:SQL性能分析的两种方式:功能介绍记录

在 Spring Boot 中使用 @Autowired和 @Bean注解的示例详解

《在SpringBoot中使用@Autowired和@Bean注解的示例详解》本文通过一个示例演示了如何在SpringBoot中使用@Autowired和@Bean注解进行依赖注入和Bean... 目录在 Spring Boot 中使用 @Autowired 和 @Bean 注解示例背景1. 定义 Stud

如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别详解

《如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别详解》:本文主要介绍如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别的相关资料,描述了如何使用海康威视设备网络SD... 目录前言开发流程问题和解决方案dll库加载不到的问题老旧版本sdk不兼容的问题关键实现流程总结前言作为

SpringBoot中使用 ThreadLocal 进行多线程上下文管理及注意事项小结

《SpringBoot中使用ThreadLocal进行多线程上下文管理及注意事项小结》本文详细介绍了ThreadLocal的原理、使用场景和示例代码,并在SpringBoot中使用ThreadLo... 目录前言技术积累1.什么是 ThreadLocal2. ThreadLocal 的原理2.1 线程隔离2