队列的使用在日常开发中,特别常见,但是对于队列接口中的一些方法可能使用起来有些疑惑,本文简单记录一下关于Queue接口中几种类似方法的区别。
- add() 和 offer()
- add() : 添加元素,如果添加成功则返回true,如果队列是满的,则抛出异常
- offer() : 添加元素,如果添加成功则返回true,如果队列是满的,则返回false
- 区别:对于一些有容量限制的队列,当队列满的时候,用add()方法添加元素,则会抛出异常,用offer()添加元素,则返回false
- remove() 和 poll()
- remove() : 移除队列头的元素并且返回,如果队列为空则抛出异常
- poll() : 移除队列头的元素并且返回,如果队列为空则返回null
- 区别:在移除队列头元素时,当队列为空的时候,用remove()方法会抛出异常,用poll()方法则会返回null
- element() 和 peek()
- element() :返回队列头元素但不移除,如果队列为空,则抛出异常
- peek() :返回队列头元素但不移除,如果队列为空,则返回null
- 区别 :在取出队列头元素时,如果队列为空,用element()方法则会抛出异常,用peek()方法则会返回null
附上源码以及中文注释:
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.
* 插入一个具体的元素到队列中如果没有超过容量限制并且返回true。
* 如果没有队列已满则抛出IllegalStateException
*
* @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
* 如果插入的对象不对,则会抛出类型转换ClassCastException
* @throws NullPointerException if the specified element is null and
* this queue does not permit null elements
* 如果插入null,则会抛出空指针异常
* @throws IllegalArgumentException if some property of this element
* prevents it from being added to this queue
* 添加元素,如果添加成功则返回true,如果队列是满的,则抛出异常
*/
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.
* 插入一个具体的元素到队列中如果没有超过容量限制并且返回true。
* 如果没有队列已满则返回false。
* 当使用一个有容量限制的队列时,建议使用该方法offer(),因为使用add()方法当队列满时会
* 直接抛出异常,则offer()方法则返回false
*
* @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
* 添加元素,如果添加成功则返回true,如果队列是满的,则返回false
*/
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
* 移除队列头的元素并且返回,如果队列为空则返回null
*/
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
* 返回队列头元素但不移除,如果队列为空,则返回null
*/
E peek();
}
网友评论