JDK源码阅读(2):Queue

Queue

version:1.8

不同于Stack类提供的栈的具体实现,Queue为JDK提供的队列先进先出)的接口

/**
 * @see java.util.Collection
 * @see LinkedList
 * @see PriorityQueue
 * @see java.util.concurrent.LinkedBlockingQueue
 * @see java.util.concurrent.BlockingQueue
 * @see java.util.concurrent.ArrayBlockingQueue
 * @see java.util.concurrent.LinkedBlockingQueue
 * @see java.util.concurrent.PriorityBlockingQueue
 * **/
public interface Queue<E> extends Collection<E> 

其定义了六个方法,分为两类,每类三个(进,出,查看队首元素),两类方法的区别主要在于对队列为空(添加失败) 时的处理方法。

第一类:队列为空时返回null

添加失败(非添加元素原因)返回false
队列为空返回null

/**
  * @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);

//@return the head of this queue, or {@code null} if this queue is empty
E poll();

//@return the head of this queue, or {@code null} if this queue is empty
E peek();

第二类:队列为空抛出异常

若由于容量限制添加失败抛出异常IllegalStateException
队列为空抛出异常NoSuchElementException

/**
  * @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);

//@return the head of this queue
//@throws NoSuchElementException if this queue is empty
E remove();

//@return the head of this queue
//@throws NoSuchElementException if this queue is empty
E element();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值