日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
數(shù)據(jù)結(jié)構(gòu)之LinkedList底層實(shí)現(xiàn)和原理詳解

前言

日常開發(fā)中,集合是我們經(jīng)常用到的一種數(shù)據(jù)結(jié)構(gòu),當(dāng)然,集合也并不是一種,也沒有所謂的最好的集合,只有最適合的;

成都創(chuàng)新互聯(lián)是一家專注網(wǎng)站建設(shè)、網(wǎng)絡(luò)營(yíng)銷策劃、微信小程序定制開發(fā)、電子商務(wù)建設(shè)、網(wǎng)絡(luò)推廣、移動(dòng)互聯(lián)開發(fā)、研究、服務(wù)為一體的技術(shù)型公司。公司成立10年以來,已經(jīng)為上1000家成都純水機(jī)各業(yè)的企業(yè)公司提供互聯(lián)網(wǎng)服務(wù)?,F(xiàn)在,服務(wù)的上1000家客戶與我們一路同行,見證我們的成長(zhǎng);未來,我們一起分享成功的喜悅。

當(dāng)然作為高級(jí)程序員,我們不僅僅要會(huì)用,還要了解其中的原理;

今天我們就來聊聊LinkedList底層實(shí)現(xiàn)和原理

一、LinkedList介紹

 
 
 
 
  1. public class LinkedList 
  2.     extends AbstractSequentialList 
  3.     implements List, Deque, Cloneable, java.io.Serializable 
  4.     //長(zhǎng)度 
  5.     transient int size = 0; 
  6.     //指向頭結(jié)點(diǎn) 
  7.     transient Node first; 
  8.     //指向尾結(jié)點(diǎn) 
  9.     transient Node last; 

1、LinkedList概述

LinkedList同時(shí)實(shí)現(xiàn)了List接口和Deque對(duì)口,也就是收它既可以看作一個(gè)順序容器,又可以看作一個(gè)隊(duì)列(Queue),同時(shí)又可以看作一個(gè)棧(stack);

這樣看來,linkedList簡(jiǎn)直就是無敵的,當(dāng)你需要使用棧或者隊(duì)列時(shí),可以考慮用LinkedList,一方面是因?yàn)镴ava官方已經(jīng)聲明不建議使用Stack類,更遺憾的是,Java里根本沒有一個(gè)叫做Queue的類(只是一個(gè)接口的名字);

關(guān)于?;蜿?duì)列,現(xiàn)在首選是ArrayDeque,它有著比LinkedList(當(dāng)作?;蜿?duì)列使用時(shí))更好的性能;

LinkedList的實(shí)現(xiàn)方式?jīng)Q定了所有跟下表有關(guān)的操作都是線性時(shí)間,而在首段或者末尾刪除元素只需要常數(shù)時(shí)間。為追求效率LinkedList沒有實(shí)現(xiàn)同步(synchronized),如果需要多個(gè)線程并發(fā)訪問,可以先采用Collections.synchronizedList()方法對(duì)其進(jìn)行包裝

2、數(shù)據(jù)結(jié)構(gòu)

繼承關(guān)系

 
 
 
 
  1. java.lang.Object  
  2.     java.util.AbstractCollection  
  3.         java.util.AbstractList  
  4.             java.util.AbstractSequentialList  
  5.                 java.util.LinkedList  

實(shí)現(xiàn)接口

 
 
 
 
  1. Serializable, Cloneable, Iterable, Collection, Deque, List, Queue  

基本屬性

  • transient int size = 0; //LinkedList中存放的元素個(gè)數(shù)
  • transient Node first; //頭節(jié)點(diǎn)
  • transient Node last; //尾節(jié)點(diǎn)
  • Collection 接口:Collection接口是所有集合類的根節(jié)點(diǎn),Collection表示一種規(guī)則,所有實(shí)現(xiàn)了Collection接口的類遵循這種規(guī)則

繼承的類與實(shí)現(xiàn)的接口

  • List 接口:List是Collection的子接口,它是一個(gè)元素有序(按照插入的順序維護(hù)元素順序)、可重復(fù)、可以為null的集合
  • AbstractCollection 類:Collection接口的骨架實(shí)現(xiàn)類,最小化實(shí)現(xiàn)了Collection接口所需要實(shí)現(xiàn)的工作量
  • AbstractList 類:List接口的骨架實(shí)現(xiàn)類,最小化實(shí)現(xiàn)了List接口所需要實(shí)現(xiàn)的工作量
  • Cloneable 接口:實(shí)現(xiàn)了該接口的類可以顯示的調(diào)用Object.clone()方法,合法的對(duì)該類實(shí)例進(jìn)行字段復(fù)制,如果沒有實(shí)現(xiàn)Cloneable接口的實(shí)例上調(diào)用Obejct.clone()方法,會(huì)拋出CloneNotSupportException異常。正常情況下,實(shí)現(xiàn)了Cloneable接口的類會(huì)以公共方法重寫Object.clone()
  • Serializable 接口:實(shí)現(xiàn)了該接口標(biāo)示了類可以被序列化和反序列化,具體的 查詢序列化詳解
  • Deque 接口:Deque定義了一個(gè)線性Collection,支持在兩端插入和刪除元素,Deque實(shí)際是“double ended queue(雙端隊(duì)列)”的簡(jiǎn)稱,大多數(shù)Deque接口的實(shí)現(xiàn)都不會(huì)限制元素的數(shù)量,但是這個(gè)隊(duì)列既支持有容量限制的實(shí)現(xiàn),也支持沒有容量限制的實(shí)現(xiàn),比如LinkedList就是有容量限制的實(shí)現(xiàn),其最大的容量為Integer.MAX_VALUE
  • AbstractSequentialList 類:提供了List接口的骨干實(shí)現(xiàn),最大限度地減少了實(shí)現(xiàn)受“連續(xù)訪問”數(shù)據(jù)存儲(chǔ)(如鏈表)支持的此接口所需的工作,對(duì)于隨機(jī)訪問數(shù)據(jù)(如數(shù)組),應(yīng)該優(yōu)先使用 AbstractList,而不是使用AbstractSequentailList類

二、底層源碼分析

LinkedList是通過雙向鏈表去實(shí)現(xiàn)的,既然是鏈表實(shí)現(xiàn)那么它的隨機(jī)訪問效率比ArrayList要低,順序訪問的效率要比較的高。每個(gè)節(jié)點(diǎn)都有一個(gè)前驅(qū)(之前前面節(jié)點(diǎn)的指針)和一個(gè)后繼(指向后面節(jié)點(diǎn)的指針)

效果如下圖:

源碼標(biāo)注如下:

 
 
 
 
  1. public class LinkedListextends AbstractSequentialList 
  2.     implements List, Deque, Cloneable, java.io.Serializable 
  3.     transient int size = 0;   //LinkedList中存放的元素個(gè)數(shù) 
  4.     transient Node first;  //頭節(jié)點(diǎn) 
  5.     transient Node last;   //尾節(jié)點(diǎn) 
  6.     //構(gòu)造方法,創(chuàng)建一個(gè)空的列表 
  7.     public LinkedList() { 
  8.     } 
  9.     //將一個(gè)指定的集合添加到LinkedList中,先完成初始化,在調(diào)用添加操作 
  10.     public LinkedList(Collection c) { 
  11.         this(); 
  12.         addAll(c); 
  13.     } 
  14.     //插入頭節(jié)點(diǎn) 
  15.     private void linkFirst(E e) { 
  16.         final Node f = first;  //將頭節(jié)點(diǎn)賦值給f節(jié)點(diǎn) 
  17.         //new 一個(gè)新的節(jié)點(diǎn),此節(jié)點(diǎn)的data = e , pre = null , next - > f  
  18.         final Node newNode = new Node<>(null, e, f); 
  19.         first = newNode; //將新創(chuàng)建的節(jié)點(diǎn)地址復(fù)制給first 
  20.         if (f == null)  //f == null,表示此時(shí)LinkedList為空 
  21.             last = newNode;  //將新創(chuàng)建的節(jié)點(diǎn)賦值給last 
  22.         else 
  23.             f.prev = newNode;  //否則f.前驅(qū)指向newNode 
  24.         size++; 
  25.         modCount++; 
  26.     } 
  27.     //插入尾節(jié)點(diǎn) 
  28.     void linkLast(E e) { 
  29.         final Node l = last;  
  30.         final Node newNode = new Node<>(l, e, null); 
  31.         last = newNode; 
  32.         if (l == null) 
  33.             first = newNode; 
  34.         else 
  35.             l.next = newNode; 
  36.         size++; 
  37.         modCount++; 
  38.     } 
  39.     //在succ節(jié)點(diǎn)前插入e節(jié)點(diǎn),并修改各個(gè)節(jié)點(diǎn)之間的前驅(qū)后繼 
  40.     void linkBefore(E e, Node succ) { 
  41.         // assert succ != null; 
  42.         final Node pred = succ.prev; 
  43.         final Node newNode = new Node<>(pred, e, succ); 
  44.         succ.prev = newNode; 
  45.         if (pred == null) 
  46.             first = newNode; 
  47.         else 
  48.             pred.next = newNode; 
  49.         size++; 
  50.         modCount++; 
  51.     } 
  52.     //刪除頭節(jié)點(diǎn) 
  53.     private E unlinkFirst(Node f) { 
  54.         // assert f == first && f != null; 
  55.         final E element = f.item; 
  56.         final Node next = f.next; 
  57.         f.item = null; 
  58.         f.next = null; // help GC 
  59.         first = next; 
  60.         if (next == null) 
  61.             last = null; 
  62.         else 
  63.             next.prev = null; 
  64.         size--; 
  65.         modCount++; 
  66.         return element; 
  67.     } 
  68.     //刪除尾節(jié)點(diǎn) 
  69.     private E unlinkLast(Node l) { 
  70.         // assert l == last && l != null; 
  71.         final E element = l.item; 
  72.         final Node prev = l.prev; 
  73.         l.item = null; 
  74.         l.prev = null; // help GC 
  75.         last = prev; 
  76.         if (prev == null) 
  77.             first = null; 
  78.         else 
  79.             prev.next = null; 
  80.         size--; 
  81.         modCount++; 
  82.         return element; 
  83.     } 
  84.     //刪除指定節(jié)點(diǎn) 
  85.     E unlink(Node x) { 
  86.         // assert x != null; 
  87.         final E element = x.item; 
  88.         final Node next = x.next;  //獲取指定節(jié)點(diǎn)的前驅(qū) 
  89.         final Node prev = x.prev;  //獲取指定節(jié)點(diǎn)的后繼 
  90.         if (prev == null) { 
  91.             first = next;   //如果前驅(qū)為null, 說明此節(jié)點(diǎn)為頭節(jié)點(diǎn) 
  92.         } else { 
  93.             prev.next = next;  //前驅(qū)結(jié)點(diǎn)的后繼節(jié)點(diǎn)指向當(dāng)前節(jié)點(diǎn)的后繼節(jié)點(diǎn) 
  94.             x.prev = null;     //當(dāng)前節(jié)點(diǎn)的前驅(qū)置空 
  95.         } 
  96.         if (next == null) {    //如果當(dāng)前節(jié)點(diǎn)的后繼節(jié)點(diǎn)為null ,說明此節(jié)點(diǎn)為尾節(jié)點(diǎn) 
  97.             last = prev; 
  98.         } else { 
  99.             next.prev = prev;  //當(dāng)前節(jié)點(diǎn)的后繼節(jié)點(diǎn)的前驅(qū)指向當(dāng)前節(jié)點(diǎn)的前驅(qū)節(jié)點(diǎn) 
  100.             x.next = null;     //當(dāng)前節(jié)點(diǎn)的后繼置空 
  101.         } 
  102.         x.item = null;     //當(dāng)前節(jié)點(diǎn)的元素設(shè)置為null ,等待垃圾回收 
  103.         size--; 
  104.         modCount++; 
  105.         return element; 
  106.     } 
  107.     //獲取LinkedList中的第一個(gè)節(jié)點(diǎn)信息 
  108.     public E getFirst() { 
  109.         final Node f = first; 
  110.         if (f == null) 
  111.             throw new NoSuchElementException(); 
  112.         return f.item; 
  113.     } 
  114.     //獲取LinkedList中的最后一個(gè)節(jié)點(diǎn)信息 
  115.     public E getLast() { 
  116.         final Node l = last; 
  117.         if (l == null) 
  118.             throw new NoSuchElementException(); 
  119.         return l.item; 
  120.     } 
  121.     //刪除頭節(jié)點(diǎn) 
  122.     public E removeFirst() { 
  123.         final Node f = first; 
  124.         if (f == null) 
  125.             throw new NoSuchElementException(); 
  126.         return unlinkFirst(f); 
  127.     } 
  128.     //刪除尾節(jié)點(diǎn) 
  129.     public E removeLast() { 
  130.         final Node l = last; 
  131.         if (l == null) 
  132.             throw new NoSuchElementException(); 
  133.         return unlinkLast(l); 
  134.     } 
  135.     //將添加的元素設(shè)置為L(zhǎng)inkedList的頭節(jié)點(diǎn) 
  136.     public void addFirst(E e) { 
  137.         linkFirst(e); 
  138.     } 
  139.     //將添加的元素設(shè)置為L(zhǎng)inkedList的尾節(jié)點(diǎn) 
  140.     public void addLast(E e) { 
  141.         linkLast(e); 
  142.     } 
  143.     //判斷LinkedList是否包含指定的元素 
  144.     public boolean contains(Object o) { 
  145.         return indexOf(o) != -1; 
  146.     } 
  147.     //返回List中元素的數(shù)量 
  148.     public int size() { 
  149.         return size; 
  150.     } 
  151.     //在LinkedList的尾部添加元素 
  152.     public boolean add(E e) { 
  153.         linkLast(e); 
  154.         return true; 
  155.     } 
  156.     //刪除指定的元素 
  157.     public boolean remove(Object o) { 
  158.         if (o == null) { 
  159.             for (Node x = first; x != null; x = x.next) { 
  160.                 if (x.item == null) { 
  161.                     unlink(x); 
  162.                     return true; 
  163.                 } 
  164.             } 
  165.         } else { 
  166.             for (Node x = first; x != null; x = x.next) { 
  167.                 if (o.equals(x.item)) { 
  168.                     unlink(x); 
  169.                     return true; 
  170.                 } 
  171.             } 
  172.         } 
  173.         return false; 
  174.     } 
  175.     //將集合中的元素添加到List中 
  176.     public boolean addAll(Collection c) { 
  177.         return addAll(size, c); 
  178.     } 
  179.     //將集合中的元素全部插入到List中,并從指定的位置開始 
  180.     public boolean addAll(int index, Collection c) { 
  181.         checkPositionIndex(index); 
  182.         Object[] a = c.toArray();  //將集合轉(zhuǎn)化為數(shù)組 
  183.         int numNew = a.length;  //獲取集合中元素的數(shù)量 
  184.         if (numNew == 0)   //集合中沒有元素,返回false 
  185.             return false; 
  186.         Node pred, succ; 
  187.         if (index == size) { 
  188.             succ = null; 
  189.             pred = last; 
  190.         } else { 
  191.             succ = node(index); //獲取位置為index的結(jié)點(diǎn)元素,并賦值給succ 
  192.             pred = succ.prev; 
  193.         } 
  194.         for (Object o : a) {  //遍歷數(shù)組進(jìn)行插入操作。修改節(jié)點(diǎn)的前驅(qū)后繼 
  195.             @SuppressWarnings("unchecked") E e = (E) o; 
  196.             Node newNode = new Node<>(pred, e, null); 
  197.             if (pred == null) 
  198.                 first = newNode; 
  199.             else 
  200.                 pred.next = newNode; 
  201.             pred = newNode; 
  202.         } 
  203.         if (succ == null) { 
  204.             last = pred; 
  205.         } else { 
  206.             pred.next = succ; 
  207.             succ.prev = pred; 
  208.         } 
  209.         size += numNew; 
  210.         modCount++; 
  211.         return true; 
  212.     } 
  213.     //刪除List中所有的元素 
  214.     public void clear() { 
  215.         // Clearing all of the links between nodes is "unnecessary", but: 
  216.         // - helps a generational GC if the discarded nodes inhabit 
  217.         //   more than one generation 
  218.         // - is sure to free memory even if there is a reachable Iterator 
  219.         for (Node x = first; x != null; ) { 
  220.             Node next = x.next; 
  221.             x.item = null; 
  222.             x.next = null; 
  223.             x.prev = null; 
  224.             x = next; 
  225.         } 
  226.         first = last = null; 
  227.         size = 0; 
  228.         modCount++; 
  229.     } 
  230.     //獲取指定位置的元素 
  231.     public E get(int index) { 
  232.         checkElementIndex(index); 
  233.         return node(index).item; 
  234.     } 
  235.     //將節(jié)點(diǎn)防止在指定的位置 
  236.     public E set(int index, E element) { 
  237.         checkElementIndex(index); 
  238.         Node x = node(index); 
  239.         E oldVal = x.item; 
  240.         x.item = element; 
  241.         return oldVal; 
  242.     } 
  243.     //將節(jié)點(diǎn)放置在指定的位置 
  244.     public void add(int index, E element) { 
  245.         checkPositionIndex(index); 
  246.         if (index == size) 
  247.             linkLast(element); 
  248.         else 
  249.             linkBefore(element, node(index)); 
  250.     } 
  251.     //刪除指定位置的元素 
  252.     public E remove(int index) { 
  253.         checkElementIndex(index); 
  254.         return unlink(node(index)); 
  255.     } 
  256.     //判斷索引是否合法 
  257.     private boolean isElementIndex(int index) { 
  258.         return index >= 0 && index < size; 
  259.     } 
  260.     //判斷位置是否合法 
  261.     private boolean isPositionIndex(int index) { 
  262.         return index >= 0 && index <= size; 
  263.     } 
  264.     //索引溢出信息 
  265.     private String outOfBoundsMsg(int index) { 
  266.         return "Index: "+index+", Size: "+size; 
  267.     } 
  268.     //檢查節(jié)點(diǎn)是否合法 
  269.     private void checkElementIndex(int index) { 
  270.         if (!isElementIndex(index)) 
  271.             throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); 
  272.     } 
  273.     //檢查位置是否合法 
  274.     private void checkPositionIndex(int index) { 
  275.         if (!isPositionIndex(index)) 
  276.             throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); 
  277.     } 
  278.     //返回指定位置的節(jié)點(diǎn)信息 
  279.     //LinkedList無法隨機(jī)訪問,只能通過遍歷的方式找到相應(yīng)的節(jié)點(diǎn) 
  280.     //為了提高效率,當(dāng)前位置首先和元素?cái)?shù)量的中間位置開始判斷,小于中間位置, 
  281.     //從頭節(jié)點(diǎn)開始遍歷,大于中間位置從尾節(jié)點(diǎn)開始遍歷 
  282.     Node node(int index) { 
  283.         // assert isElementIndex(index); 
  284.         if (index < (size >> 1)) { 
  285.             Node x = first; 
  286.             for (int i = 0; i < index; i++) 
  287.                 x = x.next; 
  288.             return x; 
  289.         } else { 
  290.             Node x = last; 
  291.             for (int i = size - 1; i > index; i--) 
  292.                 x = x.prev; 
  293.             return x; 
  294.         } 
  295.     } 
  296.     //返回第一次出現(xiàn)指定元素的位置 
  297.     public int indexOf(Object o) { 
  298.         int index = 0; 
  299.         if (o == null) { 
  300.             for (Node x = first; x != null; x = x.next) { 
  301.                 if (x.item == null) 
  302.                     return index; 
  303.                 index++; 
  304.             } 
  305.         } else { 
  306.             for (Node x = first; x != null; x = x.next) { 
  307.                 if (o.equals(x.item)) 
  308.                     return index; 
  309.                 index++; 
  310.             } 
  311.         } 
  312.         return -1; 
  313.     } 
  314.     //返回最后一次出現(xiàn)元素的位置 
  315.     public int lastIndexOf(Object o) { 
  316.         int index = size; 
  317.         if (o == null) { 
  318.             for (Node x = last; x != null; x = x.prev) { 
  319.                 index--; 
  320.                 if (x.item == null) 
  321.                     return index; 
  322.             } 
  323.         } else { 
  324.             for (Node x = last; x != null; x = x.prev) { 
  325.                 index--; 
  326.                 if (o.equals(x.item)) 
  327.                     return index; 
  328.             } 
  329.         } 
  330.         return -1; 
  331.     } 
  332.     //彈出第一個(gè)元素的值 
  333.     public E peek() { 
  334.         final Node f = first; 
  335.         return (f == null) ? null : f.item; 
  336.     } 
  337.     //獲取第一個(gè)元素 
  338.     public E element() { 
  339.         return getFirst(); 
  340.     } 
  341.     //彈出第一元素,并刪除 
  342.     public E poll() { 
  343.         final Node f = first; 
  344.         return (f == null) ? null : unlinkFirst(f); 
  345.     } 
  346.     //刪除第一個(gè)元素 
  347.     public E remove() { 
  348.         return removeFirst(); 
  349.     } 
  350.     //添加到尾部 
  351.     public boolean offer(E e) { 
  352.         return add(e); 
  353.     } 
  354.     //添加到頭部 
  355.     public boolean offerFirst(E e) { 
  356.         addFirst(e); 
  357.         return true; 
  358.     } 
  359.     //插入到最后一個(gè)元素 
  360.     public boolean offerLast(E e) { 
  361.         addLast(e); 
  362.         return true; 
  363.     } 
  364.     //隊(duì)列操作 
  365.     //嘗試彈出第一個(gè)元素,但是不刪除元素 
  366.     public E peekFirst() { 
  367.         final Node f = first; 
  368.         return (f == null) ? null : f.item; 
  369.      } 
  370.     //隊(duì)列操作 
  371.     //嘗試彈出最后一個(gè)元素,不刪除 
  372.     public E peekLast() { 
  373.         final Node l = last; 
  374.         return (l == null) ? null : l.item; 
  375.     } 
  376.     //彈出第一個(gè)元素,并刪除 
  377.     public E pollFirst() { 
  378.         final Node f = first; 
  379.         return (f == null) ? null : unlinkFirst(f); 
  380.     } 
  381.     //彈出最后一個(gè)元素,并刪除 
  382.     public E pollLast() { 
  383.         final Node l = last; 
  384.         return (l == null) ? null : unlinkLast(l); 
  385.     } 
  386.     //如隊(duì)列,添加到頭部 
  387.     public void push(E e) { 
  388.         addFirst(e); 
  389.     } 
  390.     //出隊(duì)列刪除第一個(gè)節(jié)點(diǎn) 
  391.     public E pop() { 
  392.         return removeFirst(); 
  393.     } 
  394.    //刪除指定元素第一次出現(xiàn)的位置 
  395.     public boolean removeFirstOccurrence(Object o) { 
  396.         return remove(o); 
  397.     } 
  398.     //刪除指定元素最后一次出現(xiàn)的位置 
  399.     public boolean removeLastOccurrence(Object o) { 
  400.         if (o == null) { 
  401.             for (Node x = last; x != null; x = x.prev) { 
  402.                 if (x.item == null) { 
  403.                     unlink(x); 
  404.                     return true; 
  405.                 } 
  406.             } 
  407.         } else { 
  408.             for (Node x = last; x != null; x = x.prev) { 
  409.  
    文章題目:數(shù)據(jù)結(jié)構(gòu)之LinkedList底層實(shí)現(xiàn)和原理詳解
    本文來源:http://m.5511xx.com/article/cdicepo.html