日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

死磕Java之聊聊LinkedList源码(基于JDK1.8)

發(fā)布時(shí)間:2023/12/13 综合教程 28 生活家
生活随笔 收集整理的這篇文章主要介紹了 死磕Java之聊聊LinkedList源码(基于JDK1.8) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

工作快一年了,近期打算研究一下JDK的源碼,也就因此有了死磕java系列

LinkedList 是一個(gè)繼承于AbstractSequentialList的雙向鏈表,鏈表不需要capacity的設(shè)定,它也可以被當(dāng)作堆棧、隊(duì)列或雙端隊(duì)列進(jìn)行操作。
LinkedList 實(shí)現(xiàn) List 接口,能對它進(jìn)行隊(duì)列操作,提供了相關(guān)的添加、刪除、修改、遍歷等功能。
LinkedList 實(shí)現(xiàn) Deque 接口,即能將LinkedList當(dāng)作雙端隊(duì)列使用。

LinkedList 實(shí)現(xiàn)了Cloneable接口,即覆蓋了函數(shù)clone(),能克隆。
LinkedList 實(shí)現(xiàn)java.io.Serializable接口,這意味著LinkedList支持序列化,能通過序列化去傳輸,包括網(wǎng)絡(luò)傳輸與本地文件序列化。
LinkedList 是非同步的,如若要在并發(fā)情況下使用建議選取java.util.concurrent包下的集合類型。

LinkedList的UML圖

LinkedList的成員變量及其含義


public class LinkedList<E>
    extends AbstractSequentialList<E>
    implements List<E>, Deque<E>, Cloneable, java.io.Serializable
{
    transient int size = 0;

    /**
     * 指向頭指針的節(jié)點(diǎn).
     * transient關(guān)鍵字掃盲,在實(shí)現(xiàn)Serilizable接口后,
     * 將不需要序列化的屬性前添加關(guān)鍵字transient,
     * 序列化對象的時(shí)候,這個(gè)屬性就不會(huì)序列化到指定的目的地中
     */
    transient Node<E> first;

    /**
     * 指向尾節(jié)點(diǎn)。
     */
    transient Node<E> last;

    /**
     * 構(gòu)造方法的空實(shí)現(xiàn)。
     */
    public LinkedList() {
    }

    /**
     * 按照集合迭代器返回的順序構(gòu)造包含指定集合元素的列表。
     */
    public LinkedList(Collection<? extends E> c) {
        this();
        addAll(c);
    }

    /**
     * 鏈表的節(jié)點(diǎn),私有實(shí)現(xiàn)。
     */
    private static class Node<E> {
        E item;
        Node<E> next; // 鏈表的后繼節(jié)點(diǎn)
        Node<E> prev; // 鏈表的前驅(qū)節(jié)點(diǎn)

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

聊聊LinkedList的主要方法實(shí)現(xiàn)

我們主要看研究一下下面的幾個(gè)方法,LinkedList其他方法都是通過調(diào)用這幾個(gè)方法來實(shí)現(xiàn)功能,包括LinkedList的雙端隊(duì)列的方法也是。


    /**
     * 在鏈表頭部插入元素.
     */
    private void linkFirst(E e) {
        final Node<E> f = first;
        final Node<E> newNode = new Node<>(null, e, f);
        first = newNode;
        if (f == null)
            last = newNode;
        else
            f.prev = newNode;
        size++;
        modCount++;
    }

    /**
     * 在鏈表尾部插入元素.
     */
    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++;
        // 鏈表防止并發(fā)下被修改的快速失敗策略
        modCount++;
    }

    /**
     * 在指定節(jié)點(diǎn)前面插入元素.
     */
    void linkBefore(E e, Node<E> succ) {
        // assert succ != null;
        final Node<E> pred = succ.prev;
        final Node<E> newNode = new Node<>(pred, e, succ);
        succ.prev = newNode;
        if (pred == null)
            first = newNode;
        else
            pred.next = newNode;
        size++;
        modCount++;
    }

    /**
     * 移除鏈表的頭部元素.
     */
    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; // help GC 將元素置為空,讓jvm在gc時(shí)回收資源
        first = next;
        if (next == null)
            last = null;
        else
            next.prev = null;
        size--;
        modCount++;
        return element;
    }

    /**
     * 移除鏈表的尾部元素.
     */
    private E unlinkLast(Node<E> l) {
        // assert l == last && l != null;
        final E element = l.item;
        final Node<E> prev = l.prev;
        l.item = null;
        l.prev = null; // help GC
        last = prev;
        if (prev == null)
            first = null;
        else
            prev.next = null;
        size--;
        modCount++;
        return element;
    }

    /**
     * 移除某一個(gè)節(jié)點(diǎn)元素.
     */
    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;
    }

    /**
     * 通過indexOf方法來定位給定的元素,若LinkedList中存在元素則返回該元素對應(yīng)的index,
     * 反之返回-1.
     */
    public boolean contains(Object o) {
        return indexOf(o) != -1;
    }

    /**
     * 在鏈表尾部追加元素.
     */
    public boolean add(E e) {
        linkLast(e);
        return true;
    }

    /**
     * 移除鏈表中的指定元素,分兩種情況進(jìn)行處理,當(dāng)元素為空時(shí)與元素不為空時(shí),時(shí)間復(fù)雜度為O(n).
     */
    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;
    }

    /**
     * 將一個(gè)集合的所有元素追加到鏈表的尾部.
     */
    public boolean addAll(Collection<? extends E> c) {
        return addAll(size, c);
    }

    /**
     * 將指定集合中的所有元素插入到該列表中,從指定位置開始。將當(dāng)前在該位置的元素(如果有的話)
     * 和任何后續(xù)元素向右移動(dòng)(增加它們的索引)。新元素將以指定集合的迭代器返回的順序出現(xiàn)在列表中。
     */
    public boolean addAll(int index, Collection<? extends E> c) {
        checkPositionIndex(index);

        Object[] a = c.toArray();
        int numNew = a.length;
        if (numNew == 0)
            return false;

        Node<E> pred, succ;
        if (index == size) {
            succ = null;
            pred = last;
        } else {
            succ = node(index);
            pred = succ.prev;
        }

        for (Object o : a) {
            @SuppressWarnings("unchecked") E e = (E) o;
            Node<E> newNode = new Node<>(pred, e, null);
            if (pred == null)
                first = newNode;
            else
                pred.next = newNode;
            pred = newNode;
        }

        if (succ == null) {
            last = pred;
        } else {
            pred.next = succ;
            succ.prev = pred;
        }

        size += numNew;
        modCount++;
        return true;
    }

    /**
     * 清空鏈表.
     */
    public void clear() {
        // Clearing all of the links between nodes is "unnecessary", but:
        // - helps a generational GC if the discarded nodes inhabit
        //   more than one generation
        // - is sure to free memory even if there is a reachable Iterator
        for (Node<E> x = first; x != null; ) {
            Node<E> next = x.next;
            x.item = null;
            x.next = null;
            x.prev = null;
            x = next;
        }
        first = last = null;
        size = 0;
        modCount++;
    }


    // Positional Access Operations 以下是位置訪問操作

    /**
     * 獲取鏈表中對應(yīng)索引的節(jié)點(diǎn),時(shí)間復(fù)雜度為O(n)
     */
    public E get(int index) {
        // 檢查index是否越界
        checkElementIndex(index);
        return node(index).item;
    }

    /**
     * 更新對應(yīng)index的元素,并返回舊值,時(shí)間復(fù)雜度為O(n).
     */
    public E set(int index, E element) {
        checkElementIndex(index);
        Node<E> x = node(index);
        E oldVal = x.item;
        x.item = element;
        return oldVal;
    }

    /**
     * 在指定索引添加元素,時(shí)間復(fù)雜度為O(1).
     */
    public void add(int index, E element) {
        checkPositionIndex(index);

        if (index == size)
            linkLast(element);
        else
            linkBefore(element, node(index));
    }

    /**
     * 移除指定索引的元素,時(shí)間復(fù)雜度為O(1).
     */
    public E remove(int index) {
        checkElementIndex(index);
        return unlink(node(index));
    }

     /**
     * 查找指定index位置的元素.
     */
    Node<E> node(int index) {
        // assert isElementIndex(index);

        // 在查找對應(yīng)index位置的元素的時(shí)候,java開發(fā)人員做了一層優(yōu)化
        // 當(dāng)index大于size的一半時(shí)從前向后查
        // 當(dāng)index小于size的一半時(shí)從后向前查
        // 這樣的話時(shí)間復(fù)雜度就變成了index/2
        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;
        }
    }

    // Search Operations

    /**
     * 查找指定元素的index,從前向后查
     */
    public int indexOf(Object o) {
        int index = 0;
        if (o == null) {
            for (Node<E> x = first; x != null; x = x.next) {
                if (x.item == null)
                    return index;
                index++;
            }
        } else {
            for (Node<E> x = first; x != null; x = x.next) {
                if (o.equals(x.item))
                    return index;
                index++;
            }
        }
        return -1;
    }

   /**
     * 查找指定元素的index,從后向前查
     */
    public int lastIndexOf(Object o) {
        int index = size;
        if (o == null) {
            for (Node<E> x = last; x != null; x = x.prev) {
                index--;
                if (x.item == null)
                    return index;
            }
        } else {
            for (Node<E> x = last; x != null; x = x.prev) {
                index--;
                if (o.equals(x.item))
                    return index;
            }
        }
        return -1;
    }

動(dòng)手實(shí)現(xiàn)LinkedList

由于代碼過長,故不在此處一一貼出來,感興趣的小伙伴可以到我的github查看[github]: https://github.com/haifeiWu/interview-collect/tree/master/src/main/java/com/haifeiwu/interview/structure/list

小結(jié)

與ArrayList相比,LinkedList的長處在于當(dāng)頻繁的增加或者刪除元素時(shí)效率會(huì)很高,但是LinkedList空間占用比較大,是一種典型的用空間換時(shí)間方案,在我們平時(shí)做代碼優(yōu)化時(shí)這也不失是種折中的方案。

LinkedList并發(fā)插入時(shí)節(jié)點(diǎn)覆蓋的問題,就是當(dāng)多個(gè)線程同時(shí)獲取到相同的尾節(jié)點(diǎn)的時(shí)候,然后多個(gè)線程同時(shí)在此尾節(jié)點(diǎn)后面插入數(shù)據(jù)的時(shí)候會(huì)出現(xiàn)數(shù)據(jù)覆蓋的問題,因此在并發(fā)量大的情況下應(yīng)該使用java的加鎖機(jī)制,或者采用java.util.concurrent包下的集合類型。

總結(jié)

以上是生活随笔為你收集整理的死磕Java之聊聊LinkedList源码(基于JDK1.8)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 毛片动态图 | 曰韩精品 | 日韩视频三区 | 亚洲一区二区三区高清在线 | 午夜播放 | 国产一区二区免费在线 | 9色在线| 亚洲逼图 | 在线观看中文字幕码 | 国产欧美一区二区三区另类精品 | 99久久精品国产成人一区二区 | 素人一区| 99热| 亚洲好骚综合 | 国产成人无码av在线播放dvd | 无码人妻丰满熟妇啪啪欧美 | 久久综合五月婷婷 | 一区二区三区四区国产 | 色先锋av资源 | 日韩免费网站 | 久久久中文网 | 久久嫩草视频 | 亚洲一区二区三区国产 | 色婷婷aⅴ一区二区三区 | 青青草视频免费观看 | 国产欧美日韩在线观看 | 一本毛片 | 久久字幕 | 六月丁香激情网 | 青青91| www.三区 | 亚洲 高清 成人 动漫 | 国产午夜精品免费一区二区三区视频 | 致命弯道8在线观看免费高清完整 | 免费看黄av | 69网址 | 台湾佬美性中文娱乐网 | 人妻一区二区三区免费 | 欧美日韩一区二区三区在线电影 | 日日鲁鲁鲁夜夜爽爽狠狠视频97 | 91嫩草视频在线观看 | 欧美大片一级 | 九九视频免费观看 | 一本一道久久a久久 | 狼人伊人av | 99久久婷婷| 九一在线视频 | 亚洲人成人无码网www国产 | 噜噜啪啪 | 九九热免费在线视频 | 男人日女人的网站 | 九九热在线播放 | 久久福利精品 | 国产免费啪啪 | 欧洲做受高潮欧美裸体艺术 | 色香影视 | 色播放| 亚洲乱码国产乱码精品精大量 | 日日干夜夜拍 | 久久久免费网站 | 欧洲一级黄色片 | 九九热国产视频 | 啪啪网站免费看 | 视频在线播 | 亚洲美女屁股眼交3 | av导航站 | 国产乱妇乱子 | 操操网站 | 自拍偷拍激情视频 | 久久人妻一区二区 | 日韩欧美精品在线视频 | 日韩黄色短视频 | 亚洲成年人在线 | 日韩激情床戏 | 在线视频亚洲欧美 | 国产深喉视频一区二区 | 免费看国产精品 | 美国免费黄色片 | 国产一区二区观看 | 一个人在线免费观看www | 日韩中文在线播放 | 91丝袜呻吟高潮美腿白嫩 | 女同vk| 欧美中文 | 成人久久一区二区 | 久久yy| av地址在线| 69超碰| 艳妇乳肉豪妇荡乳av | 99re在线视频免费观看 | 国产裸体无遮挡 | 国产二区三区视频 | 天天爽夜夜爽夜夜爽精品视频 | 色偷偷免费 | 日韩精品中文字 | 久久在线视频 | 国产电影一区二区三区爱妃记 | 亚洲美免无码中文字幕在线 | 国产在线视频在线观看 |