先上一张Java集合的框架图,便于参考
以下所有特性仅代表自JAVA 1.8
Queue
public interface Queueextends Collection { //在不违反容量限制的情况下,添加一个元素到队列中,如果队列容量不足则抛出异常 boolean add(E e); //在不违反容量限制的情况下,添加一个元素到队列中,如果队列容量不足,通常使用此方法,此时此方法优于add() boolean offer(E e); //取出并移除队列头部元素,如果队列为空则抛出异常 E remove(); //取出并移除队列头部元素,如果队列为空则返回NULL E poll(); //取出队列头部元素,但不会移除,如果队列为空则抛出异常 E element(); //取出队列头部元素,但不会移除, 如果队列为空则返回NULL E peek(); }
BlockingQueue
public interface BlockingQueue非阻塞队列 阻塞队列extends Queue { boolean add(E e); boolean offer(E e); //添加元素到队列中,如果队列容量不足,则等待 void put(E e) throws InterruptedException; //添加元素到队列中,如果队列容量不足,则等待特定的一段时间 boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException; //取出并移除队列的头部元素,如果队列为空则等待,直到队列有新的元素加入 E take() throws InterruptedException; //取出并移除队列的头部元素,如果队列为空则等待特定的一段时间 E poll(long timeout, TimeUnit unit) throws InterruptedException; //返回队列的剩余空间 int remainingCapacity(); boolean remove(Object o); //返回当前队列是否包含给定的对象 public boolean contains(Object o); //将队列中的元素移除,并添加到给定的集合中 int drainTo(Collection super E> c); //移除给定数量的元素,并且将元素添加到给定的集合中 int drainTo(Collection super E> c, int maxElements); }
ArrayBlockingQueue
linkedBlockingQueue
SynchronizeQueue



