📜  实现PriorityQueue API的Java程序

📅  最后修改于: 2022-05-13 01:54:56.743000             🧑  作者: Mango

实现PriorityQueue API的Java程序

PriorityQueue 是一种线性数据结构,其中元素根据其自然顺序或在构造时在队列中提供的某些自定义比较器进行排序。在PriorityQueue中,队列的前部指向最小元素,后部根据自然Ordering指向最大元素。对于按字母顺序排列的 PriorityQueue,将考虑其 ASCII 值进行排序。

PriorityQueue 的一些重要特性如下:

  • 它不允许插入空元素。
  • 它是一个无界队列,这意味着它的大小可以扩展。
  • 它继承了 Object、Abstract Collection、AbstractQueue 等类。
  • 它不是线程安全的。
  • 不能为不可比较的对象创建它。

PriorityQueue 类继承了 Queue 接口及其所有方法。 PriorityQueue API 实现了 serializable、Iterable、Collection 和 Queue,可以从下图所示的架构中看出。

Serializable, Iterable, Collection, Queue

句法:

public class PriorityQueue extends AbstractQueue implements Serializable

参数: E — 此队列中保存的元素类型。

方法:

Method  Type      Description    
add(E e)           boolean Inserts an element e to the PriorityQueue
clear()     void    Removes all the elements from the PriorityQueue  
contains(Object O)    booleanReturn true if it contains the specified element
iterator()      IteratorReturns an iterator over all the elements
remove​(Object o) booleanRemoves the specified element from the Queue
comparator()       Comparator   Returns the custom comparator used to order th elements
toArray()  Object[]    Returns an array that contains all the elements in the PriorityQueue.
peek()   Return the head of the PriorityQueue without deleting the element from the Queue
poll()           Removes and returns the head of the queue. Returns null if the queue is empty.
spliterator()   Spliterator Creates a late-binding and fail-fast Spliterator over the elements in the PriorityQueue.    

执行:

例子

Java
// Java Program to implement Priority Queue API
 
// Importing all classes from java.util package
import java.util.*;
 
// Class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating(Declaring) an object of PriorityQueue of
        // Integer type i.e Integer elements will be
        // inserted in above object
        PriorityQueue pq = new PriorityQueue<>();
 
        // Adding elements to the object created above
        // Custom inputs
        pq.add(89);
        pq.add(67);
        pq.add(78);
        pq.add(12);
        pq.add(19);
 
        // Printing the head of the PriorityQueue
        // using peek() method of Queues
        System.out.println("PriorityQueue Head:"
                           + pq.peek());
 
        // Display message
        System.out.println("\nPriorityQueue contents:");
 
        // Defining the iterator to traverse over elements of
        // object
        Iterator i = pq.iterator();
 
        // Condition check using hasNext() method which hold
        // true till single element is remaining in List
        while (i.hasNext()) {
 
            // Printing the elements of object
            System.out.print(i.next() + " ");
        }
 
        // Removing random element from above elements added
        // from the PriorityQueue
        // Custom removal be element equals 12
        pq.remove(12);
 
        // Display message
        System.out.print("\nPriorityQueue contents:");
 
        // Declaring iterator to traverse over object
        // elements
        Iterator it = pq.iterator();
 
        // Condition check using hasNext() method which hold
        // true till single element is remaining in List
        while (it.hasNext()) {
 
            // Printing the elements
            System.out.print(it.next() + " ");
        }
 
        // Removing all the elements from the PriorityQueue
        // using clear() method
        pq.clear();
 
        // Adding another different set of elements
        // to the Queue object
        // Custom different inputs
        pq.add(5);
        pq.add(7);
        pq.add(2);
        pq.add(9);
 
        // Checking a random element just inserted
        // using contains() which returns boolean value
        System.out.print("The queue has 7 = "
                         + pq.contains(7));
 
        // Display message for content in Priority queue
        System.out.print("\nPriorityQueue contents:");
 
        // Converting PriorityQueue to array
        // using toArray() method
        Object[] arr = pq.toArray();
 
        // Iterating over the array elements
        for (int j = 0; j < arr.length; j++) {
 
            // Printing all the elements in the array
            System.out.print(arr[j] + " ");
        }
    }
}


输出
PriorityQueue Head:12

PriorityQueue contents:
12 19 78 89 67 
PriorityQueue contents:19 67 78 89 The queue has 7 = true
PriorityQueue contents:2 7 5 9