数据结构——java Queue类
2024-09-01 20:09:50
定义
队列是一种特殊的线性表,它只允许在表的前端进行删除操作,而在表的后端进行插入操作。
LinkedList类实现了Queue接口,因此我们可以把LinkedList当成Queue来用
图例
Queue本身是一种先入先出的模型(FIFO),和我们日常生活中的排队模型很类似。根据不同的实现,他们主要有数组和链表两种实现形式。如下图:
与队列相关的类的关系图如下:
常用方法
序号 | 方法名 | 描述 |
1 | boolean add(E e) | 将指定的元素插入到队列中。 |
2 | Object element() | 检索该队列的头。 |
boolean offer(E e) | 将指定元素插入到该队列中(在该队列容量之内)。 | |
3 | Object peek() | 检索该队列的头,如果该队列为空返回null。 |
4 | Object poll() | 检索并删除该队列的头,如果该队列为空返回null。 |
5 | Object remove() | 检索并删除该队列的头。 |
源码
package java.util; public interface Queue<E> extends Collection<E> {
/**
* Inserts the specified element into this queue if it is possible to do so
* immediately without violating capacity restrictions, returning
* {@code true} upon success and throwing an {@code IllegalStateException}
* if no space is currently available.
*
* @param e the element to add
* @return {@code true} (as specified by {@link Collection#add})
* @throws IllegalStateException if the element cannot be added at this
* time due to capacity restrictions
* @throws ClassCastException if the class of the specified element
* prevents it from being added to this queue
* @throws NullPointerException if the specified element is null and
* this queue does not permit null elements
* @throws IllegalArgumentException if some property of this element
* prevents it from being added to this queue
*/
boolean add(E e); /**
* Inserts the specified element into this queue if it is possible to do
* so immediately without violating capacity restrictions.
* When using a capacity-restricted queue, this method is generally
* preferable to {@link #add}, which can fail to insert an element only
* by throwing an exception.
*
* @param e the element to add
* @return {@code true} if the element was added to this queue, else
* {@code false}
* @throws ClassCastException if the class of the specified element
* prevents it from being added to this queue
* @throws NullPointerException if the specified element is null and
* this queue does not permit null elements
* @throws IllegalArgumentException if some property of this element
* prevents it from being added to this queue
*/
boolean offer(E e); /**
* Retrieves and removes the head of this queue. This method differs
* from {@link #poll poll} only in that it throws an exception if this
* queue is empty.
*
* @return the head of this queue
* @throws NoSuchElementException if this queue is empty
*/
E remove(); /**
* Retrieves and removes the head of this queue,
* or returns {@code null} if this queue is empty.
*
* @return the head of this queue, or {@code null} if this queue is empty
*/
E poll(); /**
* Retrieves, but does not remove, the head of this queue. This method
* differs from {@link #peek peek} only in that it throws an exception
* if this queue is empty.
*
* @return the head of this queue
* @throws NoSuchElementException if this queue is empty
*/
E element(); /**
* Retrieves, but does not remove, the head of this queue,
* or returns {@code null} if this queue is empty.
*
* @return the head of this queue, or {@code null} if this queue is empty
*/
E peek();
}
参考:http://blog.csdn.net/u010617952/article/details/51726789
最新文章
- linux 下查看cpu位数 内核等参数命令(转)
- 自己封装一个Log模块
- Kylin的cube模型
- VS.net 2013中使用Git建立源代码管理 版本管理
- IP地址查询API的C#实现
- Timing advance of GSM(时间提前量)
- ASP.NET MVC 学习6、学习使用Code First Migrations功能,把Model的更新同步到DB中
- Get与POST的理解
- Git配合Tag的代码回滚
- javascript Dom 编程
- pyqt5 动画学习(三) 指定控件的移动轨迹
- webpack4.0各个击破(5)—— Module篇
- Linux+Shell常用命令总结
- Linux系统安装与初用
- 洛谷P4546 [THUWC2017]在美妙的数学王国中畅游 [LCT,泰勒展开]
- db2pd工具
- [LeetCode&;Python] Problem 401. Binary Watch
- linux问题集
- 【转】Java类成员变量默认初始化规则
- windows 中 到底是用的哪个java.exe??? 删除了PATH变量的Java设置还是可以运行java.exe windows/system32