Java 队列接口
Java.util 包具有接口队列,该接口扩展了“集合”接口。它用于保留根据FIFO原则处理的组件。它是一个有序的项目列表,其中在末尾添加新元素,从开头删除旧元素。
作为一个接口,队列需要一个具体的类来声明,Java中最受欢迎的类是链接列表和优先级队列。这些类的实现不是线程安全的。如果需要线程安全实现,则优先级阻止队列是一个可行的解决方案。
声明
public interface Queue<E> extends Collection<E>
Java 队列接口的方法
方法 | 说明 |
---|---|
boolean add(object) | 将指定的元素插入到队列中,如果成功,则返回 true |
boolean offer(object) | 将指定的元素插入到队列中 |
Object remove() | 用于检索和删除队列的头。 |
Object poll() | 如果队列为空,则返回空值;否则,它获取并删除队列的头部 |
Object element() | 它只检索,但不会删除队列的头部 |
Object peek() | 如果队列为空,则返回 null,否则它将获取队列的标头而不将其删除 |
功能
- 队列的项目使用 FIFO 范例进行添加和删除。
- Java 队列支持集合接口的所有方法,例如删除、插入等。
- “链接列表”、“数组阻止队列”和“优先级队列”是队列的最流行的实现。
- 对阻塞队列的任何空操作都会导致引发空点异常。
- 无界队列是包含在 util 包中的那些队列。
- 有界队列是包含在 util.concurrent 程序包中的那些队列。
- 除了 Deques 之外,所有队列都可以轻松进出线路的前面和后面。Deques实际上允许在两端移除和插入元素。
实现
import java.util.LinkedList;
import java.util.Queue;
public class QueueDemo {
public static void main(String[] args)
{
Queue<Integer> q
= new LinkedList<>();
// Elements {10, 20, 30, 40, 50} are added to the queue
for (int i = 10; i <= 50; i += 10)
q.add(i);
// Printing the contents of queue.
System.out.println("The Elements of the queue are : " + q);
// Removing queue's head.
int x = q.remove();
System.out.println("Removed element - " + x);
System.out.println(q);
// Viewing queue's head
int head = q.peek();
System.out.println("Head of the queue - " + head);
int size = q.size();
System.out.println("Size of the queue - " + size);
}
}
输出
The Elements of the queue are : [10, 20, 30, 40, 50]
Removed element - 10
[20, 30, 40, 50]
Head of the queue - 20
Size of the queue - 4
优先级队列类
在集合框架中定义的另一个类 PriorityQueue 提供了一种在处理对象时确定对象的优先级的方法。在 Java 队列中,对象插入和删除被描述为遵循 FIFO 模式。但是,当需要根据队列元素的优先级处理队列元素时,可以使用优先级队列
声明
public class PriorityQueue<E> extends AbstractQueue<E> implements Serializable
Java 优先级队列类的方法
方法 | 说明 |
---|---|
boolean add(E e) | 将元素 e 添加到优先级队列 |
void clear() | 通过删除所有元素清除优先级队列 |
Comparatorcomparator() | 返回用于对队列中元素进行排序的自定义比较器 |
boolean contains(Object o) | 检查给定元素 o 是否存在于优先级队列中。如果是,则返回 true |
Iterator< E >iterator() | 获取给定优先级队列的迭代器 |
boolean offer(E e) | 将给定元素 e 插入到优先级队列中 |
E peek() | 用于返回队列的头而不删除元素 |
E poll() | 如果队列为空,则返回 null,否则它将删除并返回队列的头 |
int size() | 返回优先级队列中的元素数 |
返回优先级队列中的元素数 | 用于返回优先级队列的数组表示形式 |
T[] toArray(T[] a) | 用于返回优先级队列的数组表示形式,其运行时类型与指定的数组 a 相同 |
优先级队列的特征
- 优先级队列是一个未绑定的队列。
- 优先级队列不允许空值。不能为不可比较的对象创建优先级队列。
- 它继承自集合、抽象集合、抽象队列和对象等类。
- 队列的头部/前面根据自然顺序包含最少的元素。
- 优先级队列的实现不是线程安全的。因此,如果我们想要同步访问,我们应该使用优先级阻止队列
实现
import java.util.*;
class PriorityQueueTest {
// Main Method
publicstaticvoidmain(String args[])
{
// Empty priority queue is created
PriorityQueue<String> pq = newPriorityQueue<String>();
// Using add() to add items to pq
pq.add("Ram");
pq.add("Mohan");
pq.add("Sohan");
// displaying top element of PriorityQueue
System.out.println(pq.peek());
// displaying the top element and removing it from the PriorityQueue container
System.out.println(pq.poll());
// Top element of pq is printed again
System.out.println(pq.peek());
}
}
输出
Mohan
Mohan
Ram
队列和优先级队列实现之间的区别
队列 | 优先级队列 |
---|---|
队列是一种线性数据结构 | 优先级队列是嵌入了优先级因子的队列的扩展 |
遵循先进先出 (FIFO) 算法为元素提供服务 | 首先为优先级较高的元素提供服务 |
在 O(1) 中完成排队和取消排队 | 使用二进制堆在 O(long) 中完成排队和取消排队 |
用于广度优先搜索等算法 | 用于算法,如迪克斯特拉算法,普里姆算法,CPU调度 |
© 版权声明
非商业转载或引用请标注本文链接,商业转载或引用请联系站长
部分文章内容可能来自互联网,如有侵权,请通过邮件联系
部分文章内容可能来自互联网,如有侵权,请通过邮件联系
THE END
暂无评论内容