数据结构——java Queue类

定义    


队列是一种特殊的线性表,它只允许在表的前端进行删除操作,而在表的后端进行插入操作。

LinkedList类实现了Queue接口,因此我们可以把LinkedList当成Queue来用

 

图例            


 Queue本身是一种先入先出的模型(FIFO),和我们日常生活中的排队模型很类似。根据不同的实现,他们主要有数组和链表两种实现形式。如下图:

 

 

 

 

与队列相关的类的关系图如下:

 

常用方法              


序号方法名描述
1boolean add(E e)将指定的元素插入到队列中。
2Object element()检索该队列的头。
 boolean offer(E e)将指定元素插入到该队列中(在该队列容量之内)。
3Object peek()检索该队列的头,如果该队列为空返回null。
4Object poll()检索并删除该队列的头,如果该队列为空返回null。
5Object remove()检索并删除该队列的头。

 

源码


 

 1 package java.util;
 2 
 3 public interface Queue<E> extends Collection<E> {
 4     /**
 5      * Inserts the specified element into this queue if it is possible to do so
 6      * immediately without violating capacity restrictions, returning
 7      * {@code true} upon success and throwing an {@code IllegalStateException}
 8      * if no space is currently available.
 9      *
10      * @param e the element to add
11      * @return {@code true} (as specified by {@link Collection#add})
12      * @throws IllegalStateException if the element cannot be added at this
13      *         time due to capacity restrictions
14      * @throws ClassCastException if the class of the specified element
15      *         prevents it from being added to this queue
16      * @throws NullPointerException if the specified element is null and
17      *         this queue does not permit null elements
18      * @throws IllegalArgumentException if some property of this element
19      *         prevents it from being added to this queue
20      */
21     boolean add(E e);
22 
23     /**
24      * Inserts the specified element into this queue if it is possible to do
25      * so immediately without violating capacity restrictions.
26      * When using a capacity-restricted queue, this method is generally
27      * preferable to {@link #add}, which can fail to insert an element only
28      * by throwing an exception.
29      *
30      * @param e the element to add
31      * @return {@code true} if the element was added to this queue, else
32      *         {@code false}
33      * @throws ClassCastException if the class of the specified element
34      *         prevents it from being added to this queue
35      * @throws NullPointerException if the specified element is null and
36      *         this queue does not permit null elements
37      * @throws IllegalArgumentException if some property of this element
38      *         prevents it from being added to this queue
39      */
40     boolean offer(E e);
41 
42     /**
43      * Retrieves and removes the head of this queue.  This method differs
44      * from {@link #poll poll} only in that it throws an exception if this
45      * queue is empty.
46      *
47      * @return the head of this queue
48      * @throws NoSuchElementException if this queue is empty
49      */
50     E remove();
51 
52     /**
53      * Retrieves and removes the head of this queue,
54      * or returns {@code null} if this queue is empty.
55      *
56      * @return the head of this queue, or {@code null} if this queue is empty
57      */
58     E poll();
59 
60     /**
61      * Retrieves, but does not remove, the head of this queue.  This method
62      * differs from {@link #peek peek} only in that it throws an exception
63      * if this queue is empty.
64      *
65      * @return the head of this queue
66      * @throws NoSuchElementException if this queue is empty
67      */
68     E element();
69 
70     /**
71      * Retrieves, but does not remove, the head of this queue,
72      * or returns {@code null} if this queue is empty.
73      *
74      * @return the head of this queue, or {@code null} if this queue is empty
75      */
76     E peek();
77 }

 

 

参考:http://blog.csdn.net/u010617952/article/details/51726789

转载于:https://www.cnblogs.com/Y-zhiwei/p/8303199.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值