詳解Java中PriorityQueue的作用和源碼實現(xiàn)
引言
前面文章我們講解了ArrayBlockingQueue
和LinkedBlockingQueue
源碼,這篇文章開始講解PriorityQueue源碼。從名字上就能看到ArrayBlockingQueue
是基于數(shù)組實現(xiàn)的,而LinkedBlockingQueue
是基于鏈表實現(xiàn),而PriorityQueue
是基于什么數(shù)據(jù)結構實現(xiàn)的,看不出來,好像是實現(xiàn)了優(yōu)先級的隊列。
由于PriorityQueue
跟前幾個阻塞隊列不一樣,并沒有實現(xiàn)BlockingQueue
接口,只是實現(xiàn)了Queue
接口,Queue
接口中定義了幾組放數(shù)據(jù)和取數(shù)據(jù)的方法,來滿足不同的場景。
操作 | 拋出異常 | 返回特定值 |
---|---|---|
放數(shù)據(jù) | add() | offer() |
取數(shù)據(jù)(同時刪除數(shù)據(jù)) | remove() | poll() |
查看數(shù)據(jù)(不刪除) | element() | peek() |
這兩組方法的區(qū)別是:
- 當隊列滿的時候,再次添加數(shù)據(jù),add()會拋出異常,offer()會返回false。
- 當隊列為空的時候,再次取數(shù)據(jù),remove()會拋出異常,poll()會返回null。
PriorityQueue
也會有針對這幾組放數(shù)據(jù)和取數(shù)據(jù)方法的具體實現(xiàn)。
類結構
先看一下PriorityQueue
類里面有哪些屬性:
public class PriorityQueue<E> extends AbstractQueue<E> implements java.io.Serializable { /** * 數(shù)組初始容量大小 */ private static final int DEFAULT_INITIAL_CAPACITY = 11; /** * 數(shù)組,用于存儲元素 */ transient Object[] queue; // non-private to simplify nested class access /** * 元素個數(shù) */ private int size = 0; /** * 比較器,用于排序元素優(yōu)先級 */ private final Comparator<? super E> comparator; }
可以看出PriorityQueue
底層是基于數(shù)組實現(xiàn)的,使用Object[]
數(shù)組存儲元素,并且定義了比較器comparator
,用于排序元素的優(yōu)先級。
初始化
PriorityQueue
常用的初始化方法有4個:
- 無參構造方法
- 指定容量大小的有參構造方法
- 指定比較器的有參構造方法
- 同時指定容量和比較器的有參構造方法
/** * 無參構造方法 */ PriorityQueue<Integer> blockingQueue1 = new PriorityQueue<>(); /** * 指定容量大小的構造方法 */ PriorityQueue<Integer> blockingQueue2 = new PriorityQueue<>(10); /** * 指定比較器的有參構造方法 */ PriorityQueue<Integer> blockingQueue3 = new PriorityQueue<>(Integer::compareTo); /** * 同時指定容量和比較器的有參構造方法 */ PriorityQueue<Integer> blockingQueue4 = new PriorityQueue<>(10, Integer::compare);
再看一下對應的源碼實現(xiàn):
/** * 無參構造方法 */ public PriorityQueue() { // 使用默認容量大小11,不指定比較器 this(DEFAULT_INITIAL_CAPACITY, null); } /** * 指定容量大小的構造方法 */ public PriorityQueue(int initialCapacity) { this(initialCapacity, null); } /** * 指定比較器的有參構造方法 */ public PriorityQueue(Comparator<? super E> comparator) { this(DEFAULT_INITIAL_CAPACITY, comparator); } /** * 同時指定容量和比較器的有參構造方法 */ public PriorityQueue(int initialCapacity, Comparator<? super E> comparator) { if (initialCapacity < 1) { throw new IllegalArgumentException(); } this.queue = new Object[initialCapacity]; this.comparator = comparator; }
可以看出PriorityQueue
的無參構造方法使用默認的容量大小11,直接初始化數(shù)組,并且沒有指定比較器。
放數(shù)據(jù)源碼
放數(shù)據(jù)的方法有2個:
操作 | 拋出異常 | 返回特定值 |
---|---|---|
放數(shù)據(jù) | add() | offer() |
offer方法源碼
先看一下offer()方法源碼,其他放數(shù)據(jù)方法邏輯也是大同小異,都是在鏈表尾部插入。 offer()方法在隊列滿的時候,會直接返回false,表示插入失敗。
/** * offer方法入口 * * @param e 元素 * @return 是否插入成功 */ public boolean offer(E e) { // 1. 判空,傳參不允許為null if (e == null) { throw new NullPointerException(); } modCount++; int i = size; // 2. 當數(shù)組滿的時候,執(zhí)行擴容 if (i >= queue.length) { grow(i + 1); } size = i + 1; // 3. 如果是第一次插入,就直接把元素插入到數(shù)組頭部 if (i == 0) { queue[0] = e; } else { // 4. 如果不是第一次插入,就找個合適的位置插入(需要保證插入后數(shù)組有序) siftUp(i, e); } return true; }
offer()方法邏輯也很簡單,先判斷是否需要擴容,如果需要擴容先執(zhí)行擴容邏輯,然后把元素插入到數(shù)組中。如果是第一次插入,就直接把元素插入到數(shù)組頭部。如果不是,就找個合適的位置插入,需要保證插入后數(shù)組仍是有序的。 再看一下擴容的源碼:
/** * 擴容 */ private void grow(int minCapacity) { int oldCapacity = queue.length; // 1. 如果原數(shù)組容量小于64,就執(zhí)行2倍擴容,否則執(zhí)行1.5擴容 int newCapacity = oldCapacity + ((oldCapacity < 64) ? (oldCapacity + 2) : (oldCapacity >> 1)); // 2. 校驗最大容量不能超過Integer最大值 if (newCapacity - MAX_ARRAY_SIZE > 0) { newCapacity = hugeCapacity(minCapacity); } // 3. 直接擴容后新數(shù)組賦值給原數(shù)組 queue = Arrays.copyOf(queue, newCapacity); }
擴容的源碼設計充滿了作者的巧思,在數(shù)組容量較小的時候,為了避免頻繁擴容,就采用2倍擴容法。在數(shù)組容量較大的時候,為了避免擴容后浪費空間,就采用1.5倍擴容法。
PriorityQueue
為了快速的插入和刪除,采用了最小堆
,而不是直接使用有序數(shù)組,這樣既可以保證插入和刪除的時間復雜度都是O(logn),又能避免移動過多元素。
最小堆的定義: 除葉子節(jié)點外,每個節(jié)點的值都小于等于左右子節(jié)點的值。
下面就是一個簡單的最小堆和映射數(shù)組:
再看一下siftUp()方法源碼,是怎么保證插入元素,數(shù)組仍是有序的? 其實就是循環(huán)跟父節(jié)點比較元素大小,找個合適的位置插入。
// 把元素插入到合適的位置 private void siftUp(int k, E x) { // 1. 如果初始化的時候,自定義了比較器,就使用自定義比較器的插入方法,否則使用默認的。 if (comparator != null) { siftUpUsingComparator(k, x); } else { siftUpComparable(k, x); } } // 自定義比較器的插入方法 private void siftUpUsingComparator(int k, E x) { while (k > 0) { // 1. 找到父節(jié)點 int parent = (k - 1) >>> 1; Object e = queue[parent]; // 2. 如果當前節(jié)點元素比父節(jié)點的元素小,就把父節(jié)點元素向下移動(給當前元素騰出位置) if (comparator.compare(x, (E) e) >= 0) { break; } queue[k] = e; k = parent; } // 3. 把當前元素插入到父節(jié)點的位置 queue[k] = x; } // 默認的插入方法 private void siftUpComparable(int k, E x) { // 1. 使用默認比較器 Comparable<? super E> key = (Comparable<? super E>) x; while (k > 0) { // 2. 找到父節(jié)點 int parent = (k - 1) >>> 1; Object e = queue[parent]; // 3. 如果當前節(jié)點元素比父節(jié)點的元素小,就把父節(jié)點元素向下移動(給當前元素騰出位置) if (key.compareTo((E) e) >= 0) { break; } queue[k] = e; k = parent; } // 4. 把當前元素插入到父節(jié)點的位置 queue[k] = key; }
再看一下add()方法源碼:
add方法源碼
add()方法底層直接調(diào)用的是offer()方法,作用相同。
/** * add方法入口 * * @param e 元素 * @return 是否添加成功 */ public boolean add(E e) { return offer(e); }
彈出數(shù)據(jù)源碼
彈出數(shù)據(jù)(取出數(shù)據(jù)并刪除)的方法有2個:
操作 | 拋出異常 | 返回特定值 |
---|---|---|
取數(shù)據(jù)(同時刪除數(shù)據(jù)) | remove() | poll() |
poll方法源碼
看一下poll()方法源碼,其他方取數(shù)據(jù)法邏輯大同小異,都是從數(shù)組頭部彈出元素。 poll()方法在彈出元素的時候,如果隊列為空,直接返回null,表示彈出失敗。
/** * poll方法入口 */ public E poll() { // 1. 如果數(shù)組為空,返回null if (size == 0) { return null; } int s = --size; modCount++; // 2. 暫存數(shù)組頭節(jié)點,最后返回 E result = (E) queue[0]; // 3. 暫存數(shù)組尾節(jié)點,調(diào)整最小堆的時候,需要上移 E x = (E) queue[s]; // 4. 刪除尾節(jié)點 queue[s] = null; // 5. 調(diào)整最小堆 if (s != 0) { siftDown(0, x); } return result; }
remove方法源碼
再看一下remove()方法源碼,如果隊列為空,remove()會拋出異常。
/** * remove方法入口 */ public E remove() { // 1. 直接調(diào)用poll方法 E x = poll(); // 2. 如果取到數(shù)據(jù),直接返回,否則拋出異常 if (x != null) { return x; } else { throw new NoSuchElementException(); } }
查看數(shù)據(jù)源碼
再看一下查看數(shù)據(jù)源碼,查看數(shù)據(jù),并不刪除數(shù)據(jù)。
操作 | 拋出異常 | 返回特定值 |
---|---|---|
查看數(shù)據(jù)(不刪除) | element() | peek() |
peek方法源碼
先看一下peek()方法源碼,如果數(shù)組為空,直接返回null。
/** * peek方法入口 */ public E peek() { // 返回數(shù)組頭節(jié)點 return (size == 0) ? null : (E) queue[0]; }
element方法源碼
再看一下element()方法源碼,如果隊列為空,則拋出異常。
/** * element方法入口 */ public E element() { // 1. 調(diào)用peek方法查詢數(shù)據(jù) E x = peek(); // 2. 如果查到數(shù)據(jù),直接返回 if (x != null) { return x; } else { // 3. 如果沒找到,則拋出異常 throw new NoSuchElementException(); } }
總結
這篇文章講解了PriorityQueue
阻塞隊列的核心源碼,了解到PriorityQueue
隊列具有以下特點:
PriorityQueue
實現(xiàn)了Queue
接口,提供了兩組放數(shù)據(jù)和讀數(shù)據(jù)的方法,來滿足不同的場景。PriorityQueue
底層基于數(shù)組實現(xiàn),按照最小堆存儲,實現(xiàn)了高效的插入和刪除。PriorityQueue
初始化的時候,可以指定數(shù)組長度和自定義比較器。PriorityQueue
初始容量是11,當數(shù)組容量小于64,采用2倍擴容,否則采用1.5擴容。PriorityQueue
每次都是從數(shù)組頭節(jié)點取元素,取之后需要調(diào)整最小堆。
今天一起分析了PriorityQueue
隊列的源碼,可以看到PriorityQueue
的源碼非常簡單,沒有什么神秘復雜的東西,下篇文章再一起接著分析其他的阻塞隊列源碼。
以上就是詳解Java中PriorityQueue的作用和源碼實現(xiàn)的詳細內(nèi)容,更多關于Java PriorityQueue的資料請關注腳本之家其它相關文章!
相關文章
Spring Boot中的JdbcTemplate是什么及用法小結
Spring Boot中的JdbcTemplate是一個強大的數(shù)據(jù)庫訪問工具,它簡化了數(shù)據(jù)庫操作的過程,在本文中,我們了解了JdbcTemplate的基本概念,并演示了如何在Spring Boot應用程序中使用它,感興趣的朋友跟隨小編一起看看吧2023-10-10Spring Security permitAll()不允許匿名訪問的操作
這篇文章主要介紹了Spring Security permitAll()不允許匿名訪問的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06JDK1.7中HashMap的死循環(huán)問題及解決方案
這篇文章主要為大家介紹了JDK1.7中HashMap的死循環(huán)問題及解決方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10SpringBoot集成Mybatis+xml格式的sql配置文件操作
這篇文章主要介紹了SpringBoot集成Mybatis+xml格式的sql配置文件操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07