queue队列的使用


//queue 的基本用法
import java.util.LinkedList;
import java.util.Queue;

public class Jesse{
public static void main(String[] args) {
Queue<Integer> que=new LinkedList<>();
que.offer(1);
que.offer(2);
que.offer(3);
System.out.println("the size of queue: "+que.size());
System.out.println(que.poll());
System.out.println(que.poll());
System.out.println(que.poll());
System.out.println("the size of queue: "+que.size());
que.offer(3);

System.out.println(que.contains(3));
que.clear();
System.out.println(que.peek());
}
}
 

 

offer,add 区别:

一些队列有大小限制,因此如果想在一个满的队列中加入一个新项,多出的项就会被拒绝。

这时新的 offer 方法就可以起作用了。它不是对调用 add() 方法抛出一个 unchecked 异常,而只是得到由 offer() 返回的 false。

poll,remove 区别:

remove() 和 poll() 方法都是从队列中删除第一个元素。remove() 的行为与 Collection 接口的版本相似, 但是新的 poll() 方法在用空集合调用时不是抛出异常,只是返回 null。因此新的方法更适合容易出现异常条件的情况。

peek,element区别:

element() 和 peek() 用于在队列的头部查询元素。与 remove() 方法类似,在队列为空时, element() 抛出一个异常,而 peek() 返回 null。

posted @ 2020-04-14 10:39  JesseKwok  阅读(350)  评论(0)    收藏  举报