Java Queue is an interface available in java.util package and extends java.util.Collection interface. Just like Java List, Java Queue is a collection of ordered elements (Or objects) but it performs insert and remove operations differently. We can use Queue to store elements before processing those elements.
《Java Queue》
在本节中,我们将讨论有关Java Queue的一些重要问题:
{\fn黑体\fs22\bord1\shad0\3aHBE\4aH00\fscx67\fscy66\2cHFFFFFF\3cH808080}你觉得呢? 队列接口是java.util的一个子类型. 收藏界面. () ( )* 就像一个真实世界的队列(例如在银行或自动取款机)一样,队列会在队列末尾插入元素并从队列开头删除. () ( )* 爪哇语 队列代表命令的元素列表. (_) ( )* 爪哇语 队列遵循FIFO的顺序来插入并去除它的元素. FIFO 代表 First In First Out.
- 爪哇语 队列支持收藏界面的所有方法. () ( )* 最常被使用的队列执行是LinkedList,ArrayBlockingQue和PresidentQue. ( ( )* 屏蔽队列不接受空元素 。 如果我们执行任何无效的操作,它会扔出 NullPointer Exception. () ( )* 屏蔽队列用于执行基于生产者/消费者的应用程序。 () ( )* 屏蔽队列是线性安全( ) ( )* 以java.util套件提供的所有列车均以java.util提供。 并发软件包为 Bbounded quees. () ( )* 所有Deques都不安全。 (_) ( )* 同步链接 队列是无限制的线程安全 基于链接节点的队列
- 所有队列都支持在队列尾部插入和在队列头部去除,但德克除外. (_ ( )* 德克是队列,但支持元素在两端插入和去除. (_) (英语)
Java Queue 类图表
Java Queue interface extends Collection interface. Collection interface extends Iterable interface. Some of the frequently used Queue implementation classes are LinkedList, PriorityQueue, ArrayBlockingQueue, DelayQueue, LinkedBlockingQueue, PriorityBlockingQueue etc.. AbstractQueue provides a skeletal implementation of the Queue interface to reduce the effort in implementing Queue.
Java Queue 方法
在本节中,我们将讨论一些有用的和常用的Java Queue方法:
- int 大小 (): 获取 Set.
- boolean isEmpty (): 检查Set是否为空 () )3. 布林包含(对象o):如果本集包含指定的元素则返回为真. () (英语). 4. 迭接器 : 返回此集元素的浏览器 。 元素不按特定顺序返回。 (
- 布尔去除全部( c) : 从此集中删除指定收藏中包含的所有元素( 可选操作) 。 () 6 布尔保留 All( Collection c) : 只保留此集中包含在指定收藏中的元素( 可选操作) 。 ()
- 空空清 (): 从集中删除全部元素.
- E 删除( E) : 获取并删除此队列的首级
- Epoll (): 获取并删除此队列的头部, 或如果此队列为空则返回为无效 。 (_)
- E取景 (: 检索,但不删除此队列的首部, 如果队列为空则返回为无效 。 () ) 11. 布尔出价(英: 如果可以在不违反容量限制的情况下立即将指定的元素插入到此队列中 。 () )12 (英语). E 元素( ): 检索,但不删除此队列的首部 。 13。 boolean add( E) : 如果可以在不违反容量限制的情况下立即将指定元素插入到此队列中, 在成功时返回真, 如果当前没有空格, 则丢弃非法状态例外 。 ( _) )14. 对象[]到箭头( ): 返回包含本集所有元素的数组 。 如果此套集对其执行者返回其元素的顺序提供了任何保证,则该方法必须以相同的顺序返回元素. ( (英语)
Java Queue 基本内容
由于Java Queue扩展了Java Collection,它还支持所有集合界面操作,让我们在下面的例子中探索一些简单的操作:
1package com.journaldev.queue;
2import java.util.*;
3
4public class QueueExample {
5 public static void main(String[] args) {
6
7 Queue<String> queue = new LinkedList<>();
8 queue.add("one");
9 queue.add("two");
10 queue.add("three");
11 queue.add("four");
12 System.out.println(queue);
13
14 queue.remove("three");
15 System.out.println(queue);
16 System.out.println("Queue Size: " + queue.size());
17 System.out.println("Queue Contains element 'two' or not? : " + queue.contains("two"));
18
19 // To empty the queue
20 queue.clear();
21 }
22}
Output:-
1[one, two, three, four]
2[one, two, four]
3Queue Size: 3
4Queue Contains element 'two' or not? : true
Java Array 到 Queue
在这里,我们可以用一个简单的例子探索如何使用Collections.addAll()
方法将Java数组转换为队列。
1import java.util.*;
2
3public class ArrayToQueue {
4 public static void main(String[] args) {
5
6 String nums[] = {"one","two","three","four","five"};
7 Queue<String> queue = new LinkedList<>();
8 Collections.addAll(queue, nums);
9 System.out.println(queue);
10 }
11}
** 输出:-** 当我们在程序上运行时,我们会得到以下输出:
1[one, two, three, four, five]
Java 接线到 Array
在这里,我们将探索如何用一个简单的例子将Java Queue转换为Java Array。
1import java.util.*;
2
3public class QueueToArray {
4 public static void main(String[] args) {
5
6 Queue<String> queue = new LinkedList<>();
7 queue.add("one");
8 queue.add("two");
9 queue.add("three");
10 queue.add("four");
11 queue.add("five");
12
13 String strArray[] = queue.toArray(new String[queue.size()]);
14 System.out.println(Arrays.toString(strArray));
15
16 }
17}
** 输出:-** 当我们在程序上运行时,我们会得到以下输出:
1[one, two, three, four, five]
Java Queue 常见操作
Java Queue 支持由 Collection 接口支持的所有操作,以及其他一些操作,它支持两种形式的几乎所有操作。
- 一组操作在操作失败时投放例外 *另一组操作在操作失败时返回特殊值
下表简要解释了所有 Queue 共同操作。
Operation | Throws exception | Special value |
---|---|---|
Insert | add(e) | offer(e) |
Remove | remove() | poll() |
Examine | element() | peek() |
我们将收集每个操作,并详细讨论它们,并在接下来的部分中提供一些有用的例子。
Java Queue 插入操作
在本节中,我们将详细讨论Java Queue Insert操作,并提供一些有用的例子。如果此操作成功执行,则返回真实
值。
- 附加(e):
如果操作失败,它会抛出一个例外.- Queue.offer(e): 如果操作失败,它会返回一个特殊值。
**注:此处的特殊值可能为false
或null
Queue add() 操作
add() 操作被用来将新元素插入队列中。如果它成功地执行插入操作,则返回true
值。否则它会丢弃 java.lang.IllegalStateException。
1import java.util.concurrent.*;
2
3public class QueueAddOperation {
4 public static void main(String[] args) {
5
6 BlockingQueue<String> queue = new ArrayBlockingQueue<>(2);
7
8 System.out.println(queue.add("one"));
9 System.out.println(queue.add("two"));
10 System.out.println(queue);
11 System.out.println(queue.add("three"));
12 System.out.println(queue);
13 }
14}
** 输出:-** 当我们在程序上运行时,我们会得到以下输出:
1true
2true
3[one, two]
4Exception in thread "main" java.lang.IllegalStateException: Queue full
由于我们的队列仅限于两个元素,当我们尝试使用 BlockingQueue.add()添加第三个元素时,它会像上面的例外。
排队报价(操作)
offer() 操作被用来将新元素插入队列中,如果它成功地执行插入操作,则返回真
值,否则返回假
值。
1import java.util.concurrent.*;
2
3public class QueueOfferOperation {
4 public static void main(String[] args) {
5
6 BlockingQueue<String> queue = new ArrayBlockingQueue<>(2);
7
8 System.out.println(queue.offer("one"));
9 System.out.println(queue.offer("two"));
10 System.out.println(queue);
11 System.out.println(queue.offer("three"));
12 System.out.println(queue);
13 }
14}
** 输出:-** 当我们在程序上运行时,我们会得到以下输出:
1true
2true
3[one, two]
4false
5[one, two]
由于我们的队列仅限于两个元素,当我们尝试使用 BlockingQueue.offer() 操作添加第三个元素时,它会返回如上所示的假
值。
Java Queue 删除操作
在本节中,我们将详细讨论Java Queue Delete操作,并提供一些有用的例子。 删除操作返回队列的头部元素,如果执行成功。
- 删除():
如果操作失败,它会投放一个例外.- Queue.poll(): 如果操作失败,它会返回一个特殊值。
**注:此处的特殊值可能为false
或null
Queue remove() 操作
remove() 操作是用来从队列的头部删除一个元素,如果它成功地执行删除操作,它会返回队列的头部元素,否则它会丢弃 java.util.NoSuchElementException。
1import java.util.*;
2
3public class QueueRemoveOperation
4{
5 public static void main(String[] args)
6 {
7 Queue<String> queue = new LinkedList<>();
8 queue.offer("one");
9 queue.offer("two");
10 System.out.println(queue);
11 System.out.println(queue.remove());
12 System.out.println(queue.remove());
13 System.out.println(queue.remove());
14 }
15}
** 输出:-** 当我们在程序上运行时,我们会得到以下输出:
1[one, two]
2one
3two
4Exception in thread "main" java.util.NoSuchElementException
由于我们的队列只有两个元素,当我们尝试第三次调用 remove() 方法时,它会像上面的例外。 NOTE:- Queue.remove(element)用于从队列中删除一个指定的元素。
Queue poll() 操作
使用 poll() 操作来删除一个元素从队列的头部。如果它成功地执行删除操作,则返回队列的头部元素。否则它返回null
值。
1import java.util.*;
2
3public class QueuePollOperation
4{
5 public static void main(String[] args)
6 {
7 Queue<String> queue = new LinkedList<>();
8 queue.offer("one");
9 queue.offer("two");
10 System.out.println(queue);
11 System.out.println(queue.poll());
12 System.out.println(queue.poll());
13 System.out.println(queue.poll());
14 }
15}
** 输出:-** 当我们在程序上运行时,我们会得到以下输出:
1[one, two]
2one
3two
4null
由于我们的队列只有两个元素,当我们尝试第三次调用 poll() 方法时,它会如上所示返回 null 值。
Java Queue 检查操作
在本节中,我们将详细讨论Java Queue Examine操作,并提供一些有用的例子。如果此操作顺利执行,它会返回队列的头部元素,而不会删除它。
- 元素():
如果操作失败,它会抛出一个例外.- Queue.peek(): 如果操作失败,它会返回一个特殊值。
**注:此处的特殊值可能为false
或null
Queue element() 操作
element() 操作是用来从队列的头部获取一个元素,而不删除它。如果它成功地执行检查操作,它会返回队列的头部元素。
1import java.util.*;
2
3public class QueueElementOperation {
4 public static void main(String[] args) {
5
6 Queue<String> queue = new LinkedList<>();
7 queue.add("one");
8
9 System.out.println(queue.element());
10 System.out.println(queue);
11 queue.clear();
12 System.out.println(queue.element());
13 }
14}
** 输出:-** 当我们在程序上运行时,我们会得到以下输出:
1one
2[one]
3Exception in thread "main" java.util.NoSuchElementException
如果我们试图在空排列中调用 element() 方法,它会像上面的例外。
Queue peek() 操作
peek() 操作是用来从队列的头部获取一个元素,而不删除它. 如果它成功地执行检查操作,它会返回队列的头部元素.否则它会返回 null 值。
1import java.util.*;
2
3public class QueuePeekOperation {
4 public static void main(String[] args) {
5
6 Queue<String> queue = new LinkedList<>();
7 queue.add("one");
8
9 System.out.println(queue.peek());
10 System.out.println(queue);
11 queue.clear();
12 System.out.println(queue.peek());
13 }
14}
** 输出:-** 当我们在程序上运行时,我们会得到以下输出:
1one
2[one]
3null
如果我们尝试在空尾声中调用 peek() 方法,它会返回 null 值,但不会像上面的例外。
Java Queue 类别
在Java中,我们可以找到许多队列实现,W可以广泛地将它们分为以下两种类型。
- 无限队列
- 无限队列
边缘队列是由容量限制的队列,这意味着我们需要在创建时提供最大队列大小。 例如ArrayBlockingQueue(见上面的例子)。 边缘队列是没有边缘队列的队列,这意味着我们不应该提供队列的大小。 例如LinkedList(见上面的例子)。 所有在java.util包中可用的队列都是边缘队列,而在java.util.concurrent包中可用的队列是边缘队列。
- 封锁队列
- 非封锁队列
所有执行BlockingQueue接口的队列都是BlockingQueues和休息的队列是Non-BlockingQueues。
区块链操作
除了Queue 的两种操作形式外,BlockingQueue 还支持如下所示的两种形式。
Operation | Throws exception | Special value | Blocks | Times out |
---|---|---|---|---|
Insert | add(e) | offer(e) | put(e) | offer(e, time, unit) |
Remove | remove() | poll() | take() | poll(time, unit) |
Examine | element() | peek() | N/A | N/A |
有些操作被阻止,直到它完成它的工作,而其他被阻止,直到时间结束. 这都是Java中的队列上的快速圆形。我希望这些Java队列示例将帮助你开始与队列集编程。