您的位置:首页 > 产品设计 > UI/UE

Java集合源码学习(13)_Queue接口以及基础实现AbstractQueue

2014-07-21 20:26 756 查看

1:Queue接口

继承接口Collection;

通常而言,顺序是FIFO,例外是优先级队列(顺序由指定的Comparator来决定)和栈(LIFO)

增加了下面几个方法:

Throws exceptionReturns special value
Insert
add(e)
offer(e)
Remove
remove()
poll()
Examine
element()
peek()

2:AbstractQueue

add()、remove()、element()是基于offer()、poll()、peek()来实现的;
代码如下:
public abstract class AbstractQueue<E> extends AbstractCollection<E> implements Queue<E> {
protected AbstractQueue() {
}
public boolean add(E e) {
if (offer(e))
return true;
else
throw new IllegalStateException("Queue full");
}
public E remove() {
E x = poll();
if (x != null)
return x;
else
throw new NoSuchElementException();
}
public E element() {
E x = peek();
if (x != null)
return x;
else
throw new NoSuchElementException();
}
public void clear() {
while (poll() != null)
;
}
public boolean addAll(Collection<? extends E> c) {
if (c == null)
throw new NullPointerException();
if (c == this)
throw new IllegalArgumentException();
boolean modified = false;
Iterator<? extends E> e = c.iterator();
while (e.hasNext()) {
if (add(e.next()))
modified = true;
}
return modified;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: