- Queue实现一个先进先出的队列
- add/off将元素添加到队尾
- remove/poll从队首获取元素并删除
- element/peek从队首获取元素但是不删除
- 避免将null元素添加到队列
同一个方式不同实现其实是最后报错方式不一样,比如remove/poll,remove空队列会报异常,poll会返回null
import java.util.LinkedList;
import java.util.Queue;
public class Main {
public static void main(String[] args) {
Queue<Person> queue=new LinkedList<>();
queue.offer(new Person("xiaohong",10));
queue.offer(new Person("xiaozhang",20));
queue.offer(new Person("zhangsan",30));
System.out.println(queue.remove());
System.out.println(queue.remove());
System.out.println(queue.poll());
if(!queue.isEmpty()){
System.out.println(queue.remove());
}
}
}