Java教程-Java队列接口

Java队列接口
队列接口(Queue)位于java.util包中,扩展了Collection接口。它用于按照先进先出(FIFO)的方式保存被处理的元素。它是一个有序对象列表,其中插入元素发生在列表的末尾,删除元素发生在列表的开头。
作为一个接口,队列在声明时需要具体的类,最常用的类是Java中的LinkedList和PriorityQueue。这些类的实现不是线程安全的。如果需要线程安全的实现,可以使用PriorityBlockingQueue。
队列接口声明
public interface Queue<E> extends Collection<E>
Java队列接口的方法
方法 | 描述 |
---|---|
boolean add(object) | 将指定元素插入此队列中,并在成功时返回true。 |
boolean offer(object) | 将指定元素插入此队列中。 |
Object remove() | 检索并删除此队列的头部。 |
Object poll() | 检索并删除此队列的头部,如果此队列为空,则返回null。 |
Object element() | 检索但不删除此队列的头部。 |
Object peek() | 检索但不删除此队列的头部,如果此队列为空,则返回null。 |
队列的特点
以下是队列的一些重要特点:
- 如前所述,队列使用FIFO的概念来插入和删除元素。
- Java队列支持Collection接口的所有方法,包括删除、插入等。
- PriorityQueue、ArrayBlockingQueue和LinkedList是最常用的实现方式。
- 如果在BlockingQueue上进行任何空操作,将引发NullPointerException。
- 出现在util包中的队列称为无界队列。
- 出现在util.concurrent包中的队列称为有界队列。
- 除了双端队列之外,所有队列都支持在队列的头部和尾部进行删除和插入。事实上,双端队列支持在两端插入和删除元素。
PriorityQueue类
PriorityQueue也是集合框架中定义的一个类,它根据优先级给我们提供了一种处理对象的方式。如前所述,Java队列中的元素按照FIFO模式进行插入和删除。然而,有时需要根据优先级来处理队列的元素,这就是PriorityQueue派上用场的地方。
PriorityQueue类声明
让我们来看一下java.util.PriorityQueue类的声明。
public class PriorityQueue<E> extends AbstractQueue<E> implements Serializable
Java PriorityQueue示例
文件名:TestCollection12.java
import java.util.*;
class TestCollection12 {
public static void main(String args[]) {
PriorityQueue<String> queue = new PriorityQueue<String>();
queue.add("Amit");
queue.add("Vijay");
queue.add("Karan");
queue.add("Jai");
queue.add("Rahul");
System.out.println("head: " + queue.element());
System.out.println("head: " + queue.peek());
System.out.println("Iterating the queue elements:");
Iterator itr = queue.iterator();
while (itr.hasNext()) {
System.out.println(itr.next());
}
queue.remove();
queue.poll();
System.out.println("After removing two elements:");
Iterator<String> itr2 = queue.iterator();
while (itr2.hasNext()) {
System.out.println(itr2.next());
}
}
}
输出:
head: Amit
head: Amit
Iterating the queue elements:
Amit
Jai
Karan
Vijay
Rahul
After removing two elements:
Karan
Rahul
Vijay
Java PriorityQueue示例:Book
让我们看一个PriorityQueue的示例,其中我们将书籍添加到队列并打印所有书籍。PriorityQueue中的元素必须是Comparable类型。字符串和包装类默认是可比较的。要在PriorityQueue中添加自定义对象,需要实现Comparable接口。
文件名:LinkedListExample.java
import java.util.*;
class Book implements Comparable<Book>{
int id;
String name, author, publisher;
int quantity;
public Book(int id, String name, String author, String publisher, int quantity) {
this.id = id;
this.name = name;
this.author = author;
this.publisher = publisher;
this.quantity = quantity;
}
public int compareTo(Book b) {
if (id > b.id) {
return 1;
} else if (id < b.id) {
return -1;
} else {
return 0;
}
}
}
public class LinkedListExample {
public static void main(String[] args) {
Queue<Book> queue = new PriorityQueue<Book>();
// 创建书籍
Book b1 = new Book(121, "Let us C", "Yashwant Kanetkar", "BPB", 8);
Book b2 = new Book(233, "Operating System", "Galvin", "Wiley", 6);
Book b3 = new Book(101, "Data Communications & Networking", "Forouzan", "Mc Graw Hill", 4);
// 将书籍添加到队列
queue.add(b1);
queue.add(b2);
queue.add(b3);
System.out.println("Traversing the queue elements:");
// 遍历队列元素
for (Book b : queue) {
System.out.println(b.id + " " + b.name + " " + b.author + " " + b.publisher + " " + b.quantity);
}
queue.remove();
System.out.println("After removing one book record:");
for (Book b : queue) {
System.out.println(b.id + " " + b.name + " " + b.author + " " + b.publisher + " " + b.quantity);
}
}
}
输出:
Traversing the queue elements:
101 Data Communications & Networking Forouzan Mc Graw Hill 4
233 Operating System Galvin Wiley 6
121 Let us C Yashwant Kanetkar BPB 8
After removing one book record:
121 Let us C Yashwant Kanetkar BPB 8
233 Operating System Galvin Wiley 6