Java教程

Java队列接口

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

队列接口声明

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。

PriorityQueue类

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

PriorityQueue类声明

让我们看看java.util.PriorityQueue类的声明。
public class PriorityQueue<E> extends AbstractQueue<E> implements Serializable

Java PriorityQueue示例

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());
        }
    }
}
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<
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

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4