前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >JDK1.8 LinkedList 源码解析

JDK1.8 LinkedList 源码解析

作者头像
tanoak
发布2018-09-26 11:14:39
4530
发布2018-09-26 11:14:39
举报
文章被收录于专栏:java闲聊java闲聊
  1. UML类图

4a.jpg

  1. LinkedList的底层实现是基于双向链表 a. LinkedList 实现 List 接口,能对它进行队列操作 b. LinkedList 实现 Deque 接口,即能将LinkedList当作双端队列使用

以下代码是抽取JDK中LinkedList的源码简化版,便于理解

代码语言:javascript
复制
package com.tanoak.jdk.linkedlist;

import java.io.Serializable;
import java.util.NoSuchElementException;

/**
 * @author tanoak@qq.com
 * @date 2018/8/23 0:29
 * @Desc
 */
public class TkSingleLinkedList<E>  implements Serializable {
    /**
     * transient 反序列化
     *
     */
     
    /**
     * 修改次数
     */
    
    protected transient int modCount = 0;
    /**
     * 集合大小
     */
    transient int size = 0;
    
    /**
     * Pointer to first node.
     * 首节点
     */
    transient Node<E> first;
    
    /**
     * 尾节点
     */
    transient Node<E> last;
    
    /**
    * 私有静态内部类
    *
    */
    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;
        }
    }
    /**
     * Constructs an empty list.
     */
    public TkSingleLinkedList() {
    }
    
    /**
     * 将一个元素添加至list尾部
     * @param e 泛型
     * @return
     */
    public boolean add(E e) {
        linkLast(e);
        return true;
    }
 
    /**
     * Links e as last element.
     * @param e  泛型
     */
    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;
        }
        else{
            l.next = newNode;
        }
        /*
        *计数
        */
        size++;
        modCount++;
    }
    
    /**
     * Returns the (non-null) Node at the specified element index.
     */
    private Node<E> node(int index) {
        // assert isElementIndex(index);
        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;
        }
    }
    
    /**
     * 根据下标获值
     *  @param index
     * @return
     */
    public E get(int index) {
        checkElementIndex(index);
        return node(index).item;
    }
    
    /**
     * 检查下标是否越界
     * @param index
     */
    private void checkElementIndex(int index) {
        if (!isElementIndex(index)){
            throw new IndexOutOfBoundsException("下标越界");
        }
    }
    /**
     * 判断参数是否有对应索引.
     */
    private boolean isElementIndex(int index) {
        return index >= 0 && index < size;
    }
    
    /**
     *根据下标删除
     * @param index
     * @return
     */
    public E remove(int index) {
        checkElementIndex(index);
        return unlink(node(index));
    }
    
    /**
     *  删除第一个匹配的指定元素
     * @param o
     * @return
     */
    public boolean remove(Object o) {
        if (o == null) {
            for (Node<E> x = first; x != null; x = x.next) {
                if (x.item == null) {
                    unlink(x);
                    return true;
                }
            }
        } else {
            for (Node<E> x = first; x != null; x = x.next) {
                if (o.equals(x.item)) {
                    unlink(x);
                    return true;
                }
            }
        }
        return false;
    }
    /**
     * Unlinks non-null node x.
     */
    E unlink(Node<E> x) {
        // assert x != null;
        final E element = x.item;
        final Node<E> next = x.next;
        final Node<E> prev = x.prev;
        
        if (prev == null) {
            first = next;
        } else {
            prev.next = next;
            x.prev = null;
        }
        if (next == null) {
            last = prev;
        } else {
            next.prev = prev;
            x.next = null;
        }
        
        x.item = null;
        size--;
        modCount++;
        return element;
    }
    /**
     * 集合大小
     * @return 集合当前容量
     */
    public int size() {
        return size;
    }
    
    /**
     * 无参 删除节点
     * @return
     */
    public E remove() {
        //调用删除第一个节点的方法
        return removeFirst();
    }
    
    /**
     * 删除第一个节点
     * @return
     */
    public E removeFirst() {
        final Node<E> f = first;
        if (f == null)
            throw new NoSuchElementException();
        return unlinkFirst(f);
    }
    
    /**
     * 删除第一个节点
     * 实际执行的方法
     */
    private E unlinkFirst(Node<E> f) {
        // assert f == first && f != null;
        final E element = f.item;
        final Node<E> next = f.next;
        f.item = null;
        f.next = null; 
        first = next;
        if (next == null)
            last = null;
        else
            next.prev = null;
        size--;
        modCount++;
        return element;
    }
}

LinkedList的源码相对比较简单,对于方法的解读在代码中有注释。

如理解有误,请指正

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.08.24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档