不必東奔西走,Java 集合框架看這一篇就夠了

{"type":"doc","content":[{"type":"heading","attrs":{"align":null,"level":1}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/2f/2fd5b329f34282e71ec45dbbf1cb3ce3.jpeg","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Java 集合,也稱作容器,主要是由兩大接口 (Interface) 派生出來的:Collection 和 Map"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"顧名思義,容器就是用來存放數據的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"那麼這兩大接口的不同之處在於:"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Collection 存放單一元素;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Map 存放 key-value 鍵值對。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"就是單身狗放 Collection 裏面,couple 就放 Map 裏。(所以你屬於哪裏?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"學習這些集合框架,我認爲有 4 個目標:"}]},{"type":"numberedlist","attrs":{"start":"1","normalizeStart":1},"content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"明確每個接口和類的對應關係;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"對每個接口和類,熟悉常用的 API;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"對不同的場景,能夠選擇合適的數據結構並分析優缺點;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":4,"align":null,"origin":null},"content":[{"type":"text","text":"學習源碼的設計,面試要會答啊。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"關於 Map,之前那篇 HashMap 的文章已經講的非常透徹詳盡了,所以本文不再贅述。如果還沒看過那篇文章的小夥伴,快去公衆號內回覆「"},{"type":"text","marks":[{"type":"strong"}],"text":"HashMap"},{"type":"text","text":"」看文章吧~"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"Collection"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"先來看最上層的 Collection."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/42/42cda8bc6653e542aafe2ceeb12953ff.jpeg","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Collection 裏還定義了很多方法,這些方法也都會繼承到各個子接口和實現類裏,而這些 API 的使用也是日常工作和麪試常見常考的,所以我們先來看下這些方法。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"操作集合,無非就是「增刪改查」四大類,也叫 CRUD:"}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Create, Read, Update, and Delete."}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"那我也把這些 API 分爲這四大類:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"功能方法增add()/addAll()刪remove()/ removeAll()改Collection Interface 裏沒有查contains()/ containsAll()其他isEmpty()/size()/toArray()"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下面具體來看:"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"增:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"boolean add(E e);"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"add() 方法傳入的數據類型必須是 Object,所以當寫入基本數據類型的時候,會做自動裝箱 auto-boxing 和自動拆箱 unboxing。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"還有另外一個方法 addAll(),可以把另一個集合裏的元素加到此集合中。"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"boolean addAll(Collection extends E> c);"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"刪:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"boolean remove(Object o);"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"remove()是刪除的指定元素。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"那和 addAll() 對應的,自然就有removeAll(),就是把集合 B 中的所有元素都刪掉。"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"boolean removeAll(Collection> c);"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"改:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Collection Interface 裏並沒有直接改元素的操作,反正刪和增就可以完成改了嘛!"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"查:"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"查下集合中有沒有某個特定的元素:"}]}]}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"boolean contains(Object o);"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"查集合 A 是否包含了集合 B:"}]}]}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"boolean containsAll(Collection> c);"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"還有一些對集合整體的操作:"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"判斷集合是否爲空:"}]}]}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"boolean isEmpty();"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"集合的大小:"}]}]}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"int size();"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"把集合轉成數組:"}]}]}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"Object[] toArray();"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"以上就是 Collection 中常用的 API 了。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在接口裏都定義好了,子類不要也得要。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當然子類也會做一些自己的實現,這樣就有了不同的數據結構。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"那我們一個個來看。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"List"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/af/af9915995e82ab2f487c5e3be72acd88.jpeg","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"List 最大的特點就是:有序,可重複。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"看官網說的:"}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"An ordered collection (also known as a sequence)."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Unlike sets, lists typically allow duplicate elements."}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這一下把 Set 的特點也說出來了,和 List 完全相反,Set 是 無序,不重複的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"List 的實現方式有 LinkedList 和 ArrayList 兩種,那面試時最常問的就是這兩個數據結構如何選擇。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"對於這類選擇問題:一是考慮數據結構是否能"},{"type":"text","marks":[{"type":"strong"}],"text":"完成需要的功能"},{"type":"text","text":";如果都能完成,二是考慮哪種"},{"type":"text","marks":[{"type":"strong"}],"text":"更高效"},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"(萬事都是如此啊。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"那具體來看這兩個 classes 的 API 和它們的時間複雜度:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"功能方法ArrayListLinkedList增add(E e)O(1)O(1)增add(int index, E e)O(n)O(n)刪remove(int index)O(n)O(n)刪remove(E e)O(n)O(n)改set(int index, E e)O(1)O(n)查get(int index)O(1)O(n)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"稍微解釋幾個:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"add(E e) 是在尾巴上加元素,雖然 ArrayList 可能會有擴容的情況出現,但是均攤複雜度(amortized time complexity)還是 O(1) 的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"add(int index, E e)是在特定的位置上加元素,LinkedList 需要先找到這個位置,再加上這個元素,雖然單純的「加」這個動作是 O(1) 的,但是要找到這個位置還是 O(n) 的。(這個有的人就認爲是 O(1),和麪試官解釋清楚就行了,拒絕扛精。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"remove(int index)是 remove 這個 index 上的元素,所以"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"ArrayList 找到這個元素的過程是 O(1),但是 remove 之後,後續元素都要往前移動一位,所以均攤複雜度是 O(n);"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"LinkedList 也是要先找到這個 index,這個過程是 O(n) 的,所以整體也是 O(n)。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"remove(E e)是 remove 見到的第一個這個元素,那麼"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"ArrayList 要先找到這個元素,這個過程是 O(n),然後移除後還要往前移一位,這個更是 O(n),總的還是 O(n);"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"LinkedList 也是要先找,這個過程是 O(n),然後移走,這個過程是 O(1),總的是 O(n)."}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"那造成時間複雜度的區別的原因是什麼呢?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"答"},{"type":"text","text":":"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"因爲 ArrayList 是用數組來實現的。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"而數組和鏈表的最大區別就是"},{"type":"text","marks":[{"type":"strong"}],"text":"數組是可以隨機訪問的(random access)"},{"type":"text","text":"。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這個特點造成了在數組裏可以通過下標用 O(1) 的時間拿到任何位置的數,而鏈表則做不到,只能從頭開始逐個遍歷。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"也就是說在「改查」這兩個功能上,因爲數組能夠隨機訪問,所以 ArrayList 的效率高。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"那「增刪」呢?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果不考慮找到這個元素的時間,"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"數組因爲物理上的連續性,當要增刪元素時,在尾部還好,但是其他地方就會導致後續元素都要移動,所以效率較低;而鏈表則可以輕鬆的斷開和下一個元素的連接,直接插入新元素或者移除舊元素。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"但是呢,實際上你不能不考慮找到元素的時間啊。。。而且如果是在尾部操作,數據量大時 ArrayList 會更快的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"所以說:"}]},{"type":"numberedlist","attrs":{"start":"1","normalizeStart":1},"content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"改查選擇 ArrayList;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"增刪在尾部的選擇 ArrayList;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"其他情況下,如果時間複雜度一樣,推薦選擇 ArrayList,因爲 overhead 更小,或者說內存使用更有效率。"}]}]}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"Vector"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"那作爲 List 的最後一個知識點,我們來聊一下 Vector。這也是一個年齡暴露帖,用過的都是大佬。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"那 Vector 和 ArrayList 一樣,也是繼承自 java.util.AbstractList,底層也是用數組來實現的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"但是現在已經被棄用了,因爲...它加了太多的 synchronized!"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"任何好處都是有代價的,線程安全的成本就是效率低,在某些系統裏很容易成爲瓶頸,所以現在大家不再在數據結構的層面加 synchronized,而是把這個任務轉移給我們程序員=="}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"那麼面試常問題:Vector 和 ArrayList 的區別是什麼,只答出來這個還還不太全面。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"來看 stack overflow 上的高票回答:"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/94/948a096e64ffc05730c6a4af39818221.jpeg","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一是剛纔已經說過的線程安全問題;二是擴容時擴多少的區別。"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這個得看看源碼:"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/9d/9d41808847e0823e5c5ee00014714e33.jpeg","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這是 ArrayList 的擴容實現,這個"},{"type":"text","marks":[{"type":"strong"}],"text":"算術右移"},{"type":"text","text":"操作是把這個數的二進制往右移動一位,最左邊"},{"type":"text","marks":[{"type":"strong"}],"text":"補符號位"},{"type":"text","text":",但是因爲容量沒有負數,所以還是補 0."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"那右移一位的效果就是除以 2,那麼定義的新容量就是原容量的 "},{"type":"text","marks":[{"type":"strong"}],"text":"1.5 倍"},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"再來看 Vector 的:"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/54/54a8ec9117e23a381cec7773530b60ec.jpeg","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"因爲通常 capacityIncrement 我們並不定義,所以默認情況下它是"},{"type":"text","marks":[{"type":"strong"}],"text":"擴容兩倍"},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"答出來這兩點,就肯定沒問題了。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"Queue & Deque"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Queue 是一端進另一端出的線性數據結構;而 Deque 是兩端都可以進出的。"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/72/720c5d9025c5db632b919bc729a450c0.jpeg","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"Queue"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Java 中的 這個 Queue 接口稍微有點坑,一般來說隊列的語義都是"},{"type":"text","marks":[{"type":"strong"}],"text":"先進先出"},{"type":"text","text":"(FIFO)的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"但是這裏有個例外,就是 PriorityQueue,也叫 heap,並不按照進去的時間順序出來,而是按照規定的優先級出去,並且它的操作並不是 O(1) 的,時間複雜度的計算稍微有點複雜,我們之後單獨開一篇來講。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"那 Queue 的方法"},{"type":"text","marks":[{"type":"strong"}],"text":"官網[1]"},{"type":"text","text":"都總結好了,它有兩組 API,基本功能是一樣的,但是呢:"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一組是會拋異常的;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"另一組會返回一個特殊值。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"功能拋異常返回值增add(e)offer(e)刪remove()poll()瞧element()peek()"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"爲什麼會拋異常呢?"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"比如隊列空了,那 remove() 就會拋異常,但是 poll() 就返回 null;element() 就會拋異常,而 peek() 就返回 null 就好了。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"那 add(e) 怎麼會拋異常呢?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"有些 Queue 它會有容量的限制,比如 "},{"type":"text","marks":[{"type":"strong"}],"text":"BlockingQueue"},{"type":"text","text":",那如果已經達到了它最大的容量且不會擴容的,就會拋異常;但如果 offer(e),就會 return false."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"那怎麼選擇呢?:"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先,要用就用"},{"type":"text","marks":[{"type":"strong"}],"text":"同一組 API"},{"type":"text","text":",前後要統一;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"其次,根據需求。如果你需要它拋異常,那就是用拋異常的;不過做算法題時基本不用,所以選那組返回特殊值的就好了。"}]}]}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"Deque"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Deque 是兩端都可以進出的,那自然是有針對 First 端的操作和對 Last 端的操作,那每端都有兩組,一組拋異常,一組返回特殊值:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"功能拋異常返回值增addFirst(e)/ addLast(e)offerFirst(e)/ offerLast(e)刪removeFirst()/ removeLast()pollFirst()/ pollLast()瞧getFirst()/ getLast()peekFirst()/ peekLast()"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用時同理,要用就用同一組。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Queue 和 Deque 的這些 API 都是 O(1) 的時間複雜度,準確來說是均攤時間複雜度。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"實現類"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"它們的實現類有這三個:"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/05/05cb8e72b663e70ab6fa6d11d59f0f00.jpeg","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"所以說,"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果想實現「普通隊列 - 先進先出」的語義,就使用 LinkedList 或者 ArrayDeque 來實現;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果想實現「優先隊列」的語義,就使用 PriorityQueue;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果想實現「棧」的語義,就使用 ArrayDeque。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們一個個來看。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在實現普通隊列時,"},{"type":"text","marks":[{"type":"strong"}],"text":"如何選擇用 LinkedList 還是 ArrayDeque 呢?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"來看一下 "},{"type":"text","marks":[{"type":"strong"}],"text":"StackOverflow[2]"},{"type":"text","text":" 上的高票回答:"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/51/51ed80388e2045d3c648e28689e31ab1.jpeg","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"總結來說就是推薦使用 ArrayDeque,因爲效率高,而 LinkedList 還會有其他的額外開銷(overhead)。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"那 ArrayDeque 和 LinkedList 的區別有哪些呢?"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/b3/b3d4bdaba496ebfda6a1c1b2b1901eba.jpeg","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"還是在剛纔的同一個問題下,這是我認爲總結的最好的:"}]},{"type":"numberedlist","attrs":{"start":"1","normalizeStart":1},"content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"ArrayDeque 是一個可擴容的數組,LinkedList 是鏈表結構;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"ArrayDeque 裏不可以存 null 值,但是 LinkedList 可以;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"ArrayDeque 在操作頭尾端的增刪操作時更高效,但是 LinkedList 只有在當要移除中間某個元素且已經找到了這個元素後的移除纔是 O(1) 的;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":4,"align":null,"origin":null},"content":[{"type":"text","text":"ArrayDeque 在內存使用方面更高效。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"所以,只要不是必須要存 null 值,就選擇 ArrayDeque 吧!"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"那如果是一個很資深的面試官問你,什麼情況下你要選擇用 LinkedList 呢?"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"答:Java 6 以前。。。因爲 ArrayDeque 在 Java 6 之後纔有的。。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"爲了版本兼容的問題,實際工作中我們不得不做一些妥協。。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"那最後一個問題,就是關於 Stack 了。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"Stack"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Stack 在語義上是 "},{"type":"text","marks":[{"type":"strong"}],"text":"後進先出(LIFO)"},{"type":"text","text":" 的線性數據結構。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"有很多高頻面試題都是要用到棧的,比如接水問題,雖然最優解是用雙指針,但是用棧是最直觀的解法也是需要了解的,之後有機會再專門寫吧。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"那在 Java 中是怎麼實現棧的呢?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"雖然 Java 中有 Stack 這個類,但是呢,官方文檔都說不讓用了!"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/de/deafd2f1896bb52c8f52844085d4bde4.jpeg","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"原因也很簡單,因爲 Vector 已經過被棄用了,而 Stack 是繼承 Vector 的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"那麼想實現 Stack 的語義,就用 ArrayDeque 吧:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"Deque stack = new ArrayDeque<>();"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"Set"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"最後一個 Set,剛纔已經說過了 Set 的特定是無序,不重複的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"就和數學裏學的「集合」的概念一致。"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/6c/6cd04f18e9037931e443ee09f13ccc97.jpeg","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Set 的常用實現類有三個:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"HashSet"},{"type":"text","text":": 採用 Hashmap 的 key 來儲存元素,主要特點是無序的,基本操作都是 O(1) 的時間複雜度,很快。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"LinkedHashSet"},{"type":"text","text":": 這個是一個 HashSet + LinkedList 的結構,特點就是既擁有了 O(1) 的時間複雜度,又能夠保留插入的順序。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"TreeSet"},{"type":"text","text":": 採用紅黑樹結構,特點是可以有序,可以用自然排序或者自定義比較器來排序;缺點就是查詢速度沒有 HashSet 快。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"那每個 Set 的"},{"type":"text","marks":[{"type":"strong"}],"text":"底層實現"},{"type":"text","text":"其實就是對應的 Map:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"數值放在 map 中的 key 上,value 上放了個 PRESENT,是一個靜態的 Object,相當於 place holder,每個 key 都指向這個 object。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"那麼具體的"},{"type":"text","marks":[{"type":"strong"}],"text":"實現原理"},{"type":"text","text":"、"},{"type":"text","marks":[{"type":"strong"}],"text":"增刪改查"},{"type":"text","text":"四種操作,以及"},{"type":"text","marks":[{"type":"strong"}],"text":"哈希衝突"},{"type":"text","text":"、"},{"type":"text","marks":[{"type":"strong"}],"text":"hashCode()/equals()"},{"type":"text","text":" 等問題都在 HashMap 那篇文章裏講過了,這裏就不贅述了,沒有看過的小夥伴可以在公衆號後臺回覆「HashMap」獲取文章哦~"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"總結"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/2f/2fd5b329f34282e71ec45dbbf1cb3ce3.jpeg","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"再回到開篇的這張圖,有沒有清楚了一些呢?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"每個數據結構下面其實都有很多內容,比如 PriorityQueue 本文沒有細說,因爲這傢伙一說又要半天。。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你覺得文章不錯,"},{"type":"text","marks":[{"type":"strong"}],"text":"記得給我「關注」和「轉發」哦~"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"每個數據結構下面其實都有很多內容,最近整理了java集合框架所有知識點。有需要的可以加V:xuanwo008獲取領取方式"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/4f/4ff83b870d8205806eacc3f2e3f70ea5.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"史上最全java集合框架資料整理,有需要的可以加V:xuanwo008獲取領取方式"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"​"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/0d/0d2dd0c7be9e6a79ba5c553e9cd8f5e2.png","alt":"","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"​​"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章