首页  >  文章  >  Java  >  Java中的队列

Java中的队列

WBOY
WBOY原创
2024-08-30 16:03:40984浏览

队列数据结构采用先进先出(FIFO)原则。它用于按照到达顺序保存要处理的对象;这与排队的人非常相似。由于Java以Collection接口的形式提供了对数据结构的大量支持,因此队列是Collection接口中可用的接口。它扩展了 Collection 接口。它在 Java.util 包中提供,并支持 Collection 接口中可用的所有操作,以及一些额外的提取、插入和检查操作。

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

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

语法:

Interface Queue<E>

队列是一个接口,而不是一个类,因此不能直接实例化。该声明表明队列接受类似于集合的通用值,并且我们可以将任何对象传递给它。 Java有多种Queue接口的实现,我们在使用Queue时可以使用它们。它们是 LinkedList 和 PriorityQueue。

队列可以声明如下:

Queue< Object > q = new LinkedList<>();
Queue< Object > q = new PriorityQueue<>();

Java 中队列是如何工作的?

  • 如前所述,Queue 接口扩展了集合接口;因此,它支持其中可用的所有基本操作方法。
  • LinkedList 和 PriorityQueue 等实现实现了 Queue 接口中声明的队列特定方法。
  • LinkedList 按照 LinkedList 的标准保存元素,即按照插入顺序。 PriorityQueue 维护插入元素的自然顺序。
  • 请注意,这两个实现都不是线程安全的,为此,Java 提供了另一个名为 PriorityBlockingQueue 的实现,它是线程安全的。

Java 中的队列成员类型

  • 由于队列是一个接口,因此它只包含抽象方法,没有数据成员。
  • 队列仅提供一种定义在子类中实现的操作的方法。

Java 中的队列函数

  • 由于队列支持 FIFO 结构,因此它允许从一端插入元素并从另一端(前端)删除元素。
  • 这是队列支持的两个基本操作。
  • 队列中所有可用的方法可以分为两类;第一种方法在操作失败时抛出异常,例如找不到元素,而在第二种方法中,在操作失败时返回任何特定值(例如 null 或 false),而不是异常.
  • 队列概念中的头始终代表队列中的第一个元素;删除时,该头元素将首先被删除。

以下是队列中可用的所有方法:

  Returns special value Throws exception
Insert offer(e) add(e)
Remove poll() remove()
Examine peek() element()
  返回特殊值 抛出异常 插入 报价(e) 添加(e) 删除 民意调查() 删除() 检查 窥视() 元素() 表>

So as explained, two types of methods throw an exception and return a special value. There are three types of operation in this kind of operation: insertion, the second is removal, and the third is retrieval or examination. In the case of the remove operation, an object will be removed from the queue. Still, in the case of examination, the object will be returned without actually removing from the queue.

Examples of Queue in Java

Given below are the different examples of Queue in Java:

Example #1 – Add operation with LinkedList implementation

Code:

import java.util.LinkedList;
import java.util.Queue;
public class QueueOperations {
public static void main(String[] args) {
Queue<Integer> q = new LinkedList<Integer> ();
q.add(5);
q.add(2);
q.add(1);
q.add(4);
q.add(3);
System.out.println(q);
}
}

Output:

Java中的队列

Note here that the order of insertion is the same with output from left to write.

Example #2 – Let’s remove the added elements one by one

Code:

import java.util.LinkedList;
import java.util.Queue;
public class QueueOperations {
public static void main(String[] args) {
Queue<Integer> q = new LinkedList<Integer> ();
q.add(5);
q.add(2);
q.add(1);
q.add(4);
q.add(3);
System.out.println(q);
while (!q.isEmpty()) {
System.out.print(q.remove() + " ");
}
System.out.println("");
System.out.println(q);
}
}

Output:

Java中的队列

Here, we have used the function isEmpty() to check when the queue becomes empty after removing elements. The removal order is the same as per the insertion. After removing all the elements, we printed the queue and obtained an empty bracket at the end.

Example #3 – Insertion and Removal Operation on PriorityQueue

Code:

import java.util.PriorityQueue;
import java.util.Queue;
public class QueueOperations {
public static void main(String[] args) {
Queue<Integer> q = new PriorityQueue<Integer> ();
q.add(5);
q.add(2);
q.add(1);
q.add(4);
q.add(3);
System.out.println(q);
while (!q.isEmpty()) {
System.out.print(q.remove() + " ");
}
System.out.println("");
System.out.println(q);
}
}

Output:

Java中的队列

Here, we have used PriorityQueue, which will hold and return the elements depending upon the elements’ natural ordering or upon the comparator, if any passed. Note the insertion order and removal orders are not the same. The removal is based totally on the value of elements.

Example #4 – Examine operation on LinkedList

Code:

import java.util.LinkedList;
import java.util.Queue;
public class QueueOperations {
public static void main(String[] args) {
Queue<Integer> q = new LinkedList<Integer> ();
q.add(5);
q.add(2);
q.add(1);
q.add(4);
q.add(3);
System.out.println(q);
System.out.println( q.peek() );
System.out.println(q);
}
}

Output:

Java中的队列

Note here that we have used the peek() function, which will return the head of the queue without actually removing it. We printed the queue after performing the peek operation, and you can observe that the head element, which is 5, remains unchanged in the queue.

Example #5 – Examine operation on PriorityQueue

Code:

import java.util.PriorityQueue;
import java.util.Queue;
public class QueueOperations {
public static void main(String[] args) {
Queue<Integer> q = new PriorityQueue<Integer> ();
q.add(5);
q.add(2);
q.add(1);
q.add(4);
q.add(3);
System.out.println(q);
System.out.println( q.peek() );
System.out.println(q);
}
}

Output:

Java中的队列

This is similar to the previous example’s LinkedList operation, but note the head element is 1 because it’s a PriorityQueue.

Conclusion

Java utilizes the Queue interface as a means to maintain elements in insertion order. It supports operations like insertion, retrieval, and removal. There are alternative methods available for all the methods. We have seen examples of the most commonly used methods in queue operation.

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

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