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

本文深入探讨了Java集合框架中的Queue接口,它作为Collection的子接口,以及AbstractQueue作为其基础实现的原理和用法。文章详细介绍了Queue接口的主要方法及其在Java编程中的应用。
摘要由CSDN通过智能技术生成

1:Queue接口

继承接口Collection;

通常而言,顺序是FIFO,例外是优先级队列(顺序由指定的Comparator来决定)和栈(LIFO)
增加了下面几个方法:

 Throws exceptionReturns special value
Insertadd(e)offer(e)
Removeremove()poll()
Examineelement()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;
	}

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值