<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Java `PriorityQueue`類 > 原文: [https://howtodoinjava.com/java/collections/java-priorityqueue/](https://howtodoinjava.com/java/collections/java-priorityqueue/) Java `PriorityQueue`類是一種隊列數據結構實現,其中,根據對象的**優先級**處理對象。 它與遵循 [FIFO](https://en.wikipedia.org/wiki/FIFO_(computing_and_electronics)) (先進先出)算法的標準隊列不同。 在優先級隊列中,添加的對象根據其優先級。 默認情況下,優先級由對象的自然順序決定。 隊列構建時提供的[比較器](https://howtodoinjava.com/java/collections/java-comparator/)可以覆蓋默認優先級。 ![Priority Queue](https://img.kancloud.cn/b6/1d/b61d777d8956d72f3e241f534ced5754_608x261.png) 優先隊列 ## 1\. `PriorityQueue`特性 讓我們記下`PriorityQueue`的幾個要點。 * `PriorityQueue`是一個無界隊列,并且會動態增長。 默認初始容量為`'11'`,可以在適當的構造器中使用`initialCapacity`參數來覆蓋。 * 它不允許使用`NULL`對象。 * 添加到`PriorityQueue`的對象必須是可比較的。 * 默認情況下,優先級隊列的對象以自然順序排序。 * 比較器可用于隊列中對象的自定義排序。 * 優先級隊列的**頭**是基于自然排序或基于比較器排序的**最小**元素。 當我們輪詢隊列時,它從隊列中返回頭對象。 * 如果存在多個具有相同優先級的對象,則它可以隨機輪詢其中的任何一個。 * `PriorityQueue`是**不是線程安全的**。 在并發環境中使用`PriorityBlockingQueue`。 * 它為添加和輪詢方法提供了`O(log(n))`時間。 ## 2\. Java `PriorityQueue`示例 讓我們看看對象的排序如何影響`PriorityQueue`中的添加和刪除操作。 在給定的示例中,對象的類型為`Employee`。 `Employee`類實現 `Comparable`接口,默認情況下,該接口使對象可通過員工`'id'`字段進行比較。 ```java public class Employee implements Comparable<Employee> { private Long id; private String name; private LocalDate dob; public Employee(Long id, String name, LocalDate dob) { super(); this.id = id; this.name = name; this.dob = dob; } @Override public int compareTo(Employee emp) { return this.getId().compareTo(emp.getId()); } //Getters and setters @Override public String toString() { return "Employee [id=" + id + ", name=" + name + ", dob=" + dob + "]"; } } ``` #### 2.1 自然排序 Java `PriorityQueue`示例,用于添加和輪詢根據其自然順序進行比較的元素。 ```java PriorityQueue<Employee> priorityQueue = new PriorityQueue<>(); priorityQueue.add(new Employee(1l, "AAA", LocalDate.now())); priorityQueue.add(new Employee(4l, "CCC", LocalDate.now())); priorityQueue.add(new Employee(5l, "BBB", LocalDate.now())); priorityQueue.add(new Employee(2l, "FFF", LocalDate.now())); priorityQueue.add(new Employee(3l, "DDD", LocalDate.now())); priorityQueue.add(new Employee(6l, "EEE", LocalDate.now())); while(true) { Employee e = priorityQueue.poll(); System.out.println(e); if(e == null) break; } ``` 程序輸出。 ```java Employee [id=1, name=AAA, dob=2018-10-31] Employee [id=2, name=FFF, dob=2018-10-31] Employee [id=5, name=BBB, dob=2018-10-31] Employee [id=4, name=CCC, dob=2018-10-31] Employee [id=3, name=DDD, dob=2018-10-31] Employee [id=6, name=EEE, dob=2018-10-31] ``` #### 2.2 使用比較器自定義順序 讓我們使用[**基于 Java 8 lambda 的比較器**](https://howtodoinjava.com/java8/using-comparator-becomes-easier-with-lambda-expressions-java-8/)語法重新定義自定義順序,并驗證結果。 ```java //Comparator for name field Comparator<Employee> nameSorter = Comparator.comparing(Employee::getName); PriorityQueue<Employee> priorityQueue = new PriorityQueue<>( nameSorter ); priorityQueue.add(new Employee(1l, "AAA", LocalDate.now())); priorityQueue.add(new Employee(4l, "CCC", LocalDate.now())); priorityQueue.add(new Employee(5l, "BBB", LocalDate.now())); priorityQueue.add(new Employee(2l, "FFF", LocalDate.now())); priorityQueue.add(new Employee(3l, "DDD", LocalDate.now())); priorityQueue.add(new Employee(6l, "EEE", LocalDate.now())); while(true) { Employee e = priorityQueue.poll(); System.out.println(e); if(e == null) break; } ``` 程序輸出: ```java Employee [id=1, name=AAA, dob=2018-10-31] Employee [id=5, name=BBB, dob=2018-10-31] Employee [id=4, name=CCC, dob=2018-10-31] Employee [id=3, name=DDD, dob=2018-10-31] Employee [id=6, name=EEE, dob=2018-10-31] Employee [id=2, name=FFF, dob=2018-10-31] ``` ## 3\. Java `PriorityQueue`構造器 `PriorityQueue`類提供了 6 種不同的方法來用 Java 構造優先級隊列。 * **`PriorityQueue()`**:構造具有默認初始容量(11)的空隊列,該初始容量根據其元素的自然順序對其進行排序。 * **`PriorityQueue(Collection c)`**:構造一個空隊列,其中包含指定集合中的元素。 * **`PriorityQueue(int initialCapacity)`**:構造具有指定初始容量的空隊列,該隊列根據其自然順序對其元素進行排序。 * **`PriorityQueue(int initialCapacity, Comparator comparator)`**:構造具有指定初始容量的空隊列,該隊列根據指定的比較器對其元素進行排序。 * **`PriorityQueue(PriorityQueue c)`**:構造一個空隊列,其中包含指定優先級隊列中的元素。 * **`PriorityQueue(SortedSet c)`**:構造一個空隊列,其中包含指定排序集中的元素。 ## 4\. Java `PriorityQueue`方法 您應該知道`PriorityQueue`類下面給出了重要的方法。 * **`boolean add(object)`**:將指定的元素插入此優先級隊列。 * **`boolean offer(Object)`**:將指定的元素插入此優先級隊列。 * **`boolean remove(object)`**:從此隊列中移除指定元素的單個實例(如果存在)。 * **`Object poll()`**:檢索并刪除此隊列的頭部,如果此隊列為空,則返回`null`。 * **`Object element()`**:檢索但不刪除此隊列的頭,如果此隊列為空,則拋出`NoSuchElementException`。 * **`Object peek()`**:檢索但不刪除此隊列的頭,如果此隊列為空,則返回`null`。 * **`void clear()`**:從此優先級隊列中刪除所有元素。 * **`Comparator comparator()`**:返回用于對此隊列中的元素進行排序的比較器;如果此隊列是根據其元素的自然順序排序的,則返回`null`。 * **`boolean contains(Object o)`**:如果此隊列包含指定的元素,則返回`true`。 * **`Iterator iterator()`**:在此隊列中的元素上返回一個迭代器。 * **`int size()`**:返回此隊列中的元素數。 * **`Object[] toArray()`**:返回包含此隊列中所有元素的數組。 ## 5\. 總結 在此 **Java 隊列教程**中,我們學習了使用`PriorityQueue`類,該類能夠按默認的自然順序或指定比較器的自定義順序存儲元素。 我們還了解了`PriorityQueue`類的一些重要方法和[構造器](https://howtodoinjava.com/oops/java-constructors/)。 將我的問題放在評論部分。 學習愉快! 參考文獻: [`PriorityQueue`類 Java 文檔](https://docs.oracle.com/javase/8/docs/api/java/util/PriorityQueue.html)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看