首页  >  文章  >  Java  >  Java中的优先级队列

Java中的优先级队列

PHPz
PHPz原创
2024-08-30 16:04:30593浏览

一般情况下,Queue中的对象是按照先进先出的顺序放置的。即先进先出。在某些情况下,对象必须根据其优先级进行处理,此时,Java PriorityQueue 就发挥了作用。除此之外,PriorityQueue 还具有某些功能:

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

  • 这是一个无界队列。
  • 默认情况下,元素的排序是自然顺序。
  • 不允许使用空值。
  • 不可比较的对象不能用于创建 PriorityQueue。

Java PriorityQueue 声明

Java PriorityQueue 可以使用以下语法声明。

public class PriorityQueue<E> extends AbstractQueue<E> implements Serializable

Java PriorityQueue 的构造函数

以下是Java PriorityQueue中常用的构造函数:

1。 PriorityQueue(): 创建的 PriorityQueue 容量为 11,默认初始容量为 11。此外,元素是根据自然顺序排序的。

2。 PriorityQueue(集合 扩展 E> c): PriorityQueue 将是使用上述集合中的元素创建。

3。 PriorityQueue(int ic): 将使用提到的初始容量 ic 创建一个 PriorityQueue。此外,元素是根据自然顺序排序的。

4。 PriorityQueue(int ic, Comparator super E> comparator): 将使用提到的初始容量 ic 创建一个 PriorityQueue。此外,元素是根据提到的比较器排序的。

5。 PriorityQueue(PriorityQueue extends E> c): PriorityQueue 将是使用上述 PriorityQueue 中的元素创建。

6。 PriorityQueue(SortedSet 扩展 E> c): 将使用上述排序集中的元素创建 PriorityQueue。

方法

现在,让我们看看Java PriorityQueue中一些常用的方法:

1。 add(E e): 调用此方法时,元素 e 将被添加到 PriorityQueue 中。

2。 size(): 将返回集合中元素的数量。

3。 clear(): PriorityQueue 中的所有元素都将被删除。

4。 comparator(): 将返回用于对队列进行排序的比较器。如果使用自然排序,将返回 null。

5。 contains(Objecto): 如果队列包含提到的元素 o,则返回 true。

6。迭代器():将返回对队列中的元素使用的迭代器。

7。 Offer(Ee): 提到的元素 e 将被插入到队列中。

8。 peek(): PriorityQueue 的头部将被检索,而不是删除。如果不存在任何元素,将返回 null。

9。 poll(): PriorityQueue 的头部将被检索并删除。如果不存在任何元素,将返回 null。

10。删除(对象o):提到的元素的单个实例将从队列中删除。

11。 toArray(): 将返回队列中所有元素的数组。

12。 toArray(T[] a): 将返回队列中所有元素的数组,其中运行时间将与上述数组的运行时间相同。

Java PriorityQueue 示例

下面给出的是 Java PriorityQueue 的示例:

示例#1

创建 PriorityQueue 的示例程序。

代码:

import java.util.Iterator;
import java.util.PriorityQueue;
class Main{
public static void main(String args[]){
//create a PriorityQueue
PriorityQueue<String> q=new PriorityQueue<String>();
//add elements to the queue
q.add("Anna");
q.add("Annamu");
q.add("Adam");
q.add("Iza");
q.add("Thukidi");
System.out.println("Head of the queue:"+q.element());
System.out.println("Head of the queue :"+q.peek());
//Retrieve elements in queue using iterator
System.out.println("Queue elements are :");
Iterator it=q.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
//remove the element from queue
q.remove();
//remove the head of the queue
q.poll();
System.out.println("\n Queue after the removal of 2 elements :");
//Retrieve elements in queue using iterator
Iterator<String> it2=q.iterator();
while(it2.hasNext())
{
System.out.println(it2.next());
}
}
}

输出:

Java中的优先级队列

示例程序的工作:

  • 创建一个 PriorityQueue q。
  • 将元素添加到队列。
  • 打印队列的头部。
  • 使用迭代器打印队列中的所有元素。
  • 删除两个元素。
  • 移除两个元素后打印Queue中的所有元素。

示例#2

使用比较器创建 PriorityQueue 的示例程序。

Code:

import java.util.Comparator;
import java.util.Iterator;
import java.util.PriorityQueue;
class PriorityQueueExample{
public static void main(String[] args) {
//Create a custom comparator. In this, length of 2 strings are getting compared
Comparator<String> cmp = new Comparator<String>()
{
@Override
public int compare(String str1, String str2) {
return str1.length() - str2.length();
}
};
// PriorityQueue creation with Comparator
PriorityQueue<String> q = new PriorityQueue<>(cmp);
// Add elements to the Queue
q.add("Anna");
q.add("Annamu");
q.add("Adam");
q.add("Iza");
q.add("Thukidi");
q.add("Sam");
q.add("Elsa");
q.add("Kukku");
q.add("Mathu");
q.add("Radha");
// Remove elements from the Queue
while (!q.isEmpty()) {
System.ou<em>t</em>.println(q.remove());
}
}
}

Output:

Java中的优先级队列

Working of the sample program:

  • Create a custom comparator where the elements in the Queue are compared based on the length.
  • Create a PriorityQueue q.
  • Add elements to the Queue.
  • Remove the elements from the queue based on the length of the strings.
  • Print the elements in the queue in the order of removal.

Example #3

Sample program to implement a PriorityQueue by making use of different methods.

Code:

import java.util.*;
class Main{
public static void main(String args[]){
//create a PriorityQueue
PriorityQueue<String> q=new PriorityQueue<String>();
//add elements to the queue
q.add("Anna");
q.add("Annamu");
q.add("Adam");
q.add("Iza");
q.add("Thukidi");
System.out.println("Head of the queue:"+q.element());
System.out.println("Head of the queue :"+q.peek());
//Retrieve elements in queue using iterator
System.out.println("Queue elements are :");
Iterator it=q.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
//remove the element from queue
q.remove();
//remove the head of the queue
q.poll();
// Check whether the element Anna is present in queue using the method Contains()
boolean b = q.contains("Anna");
System.out.println("Is there any element Anna in the PriorityQueue ? " + b);
//Check whether the element Iza is present in queue using the method Contains()
boolean bl = q.contains("Iza");
System.out.println("Is there any element Anna in the PriorityQueue ? " + bl);
}
}

Output:

Java中的优先级队列

Working of the sample program:

  • Create a PriorityQueue q.
  • Add elements and Print the head of the queue.
  • Print all the elements in the Queue using iterator.
  • Remove two elements using the methods remove() and poll().
  • Check whether the two elements Anna and Iza are available in Queue using the method Contains().

以上是Java中的优先级队列的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn