📜  Java队列接口

📅  最后修改于: 2020-10-12 09:59:37             🧑  作者: Mango

Java队列接口

Java Queue接口以FIFO(先进先出)的方式对元素进行排序。在FIFO中,首先删除第一个元素,最后删除最后一个元素。

队列接口声明

public interface Queue extends Collection

Java队列接口方法

Method Description
boolean add(object) It is used to insert the specified element into this queue and return true upon success.
boolean offer(object) It is used to insert the specified element into this queue.
Object remove() It is used to retrieves and removes the head of this queue.
Object poll() It is used to retrieves and removes the head of this queue, or returns null if this queue is empty.
Object element() It is used to retrieves, but does not remove, the head of this queue.
Object peek() It is used to retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.

PriorityQueue类

PriorityQueue类提供使用队列的便利。但是它不以FIFO方式对元素进行排序。它继承了AbstractQueue类。

PriorityQueue类声明

我们来看一下java.util.PriorityQueue类的声明。

public class PriorityQueue extends AbstractQueue implements Serializable

Java PriorityQueue示例

import java.util.*;
class TestCollection12{
public static void main(String args[]){
PriorityQueue queue=new PriorityQueue();
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 itr2=queue.iterator();
while(itr2.hasNext()){
System.out.println(itr2.next());
}
}
}
Output:head:Amit
       head:Amit
       iterating the queue elements:
       Amit
       Jai
       Karan
       Vijay
       Rahul
       after removing two elements:
       Karan
       Rahul
       Vijay

Java PriorityQueue示例:书籍

让我们看一个PriorityQueue示例,其中我们将书添加到队列中并打印所有书。 PriorityQueue中的元素必须为Comparable类型。默认情况下,String和Wrapper类是Comparable的。要在PriorityQueue中添加用户定义的对象,您需要实现Comparable接口。

import java.util.*;
class Book implements Comparable{
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 queue=new PriorityQueue();
//Creating Books
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);
//Adding Books to the queue
queue.add(b1);
queue.add(b2);
queue.add(b3);
System.out.println("Traversing the queue elements:");
//Traversing 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