欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

QueueDemo

程序员文章站 2022-03-14 11:21:37
...
package com.zhoubo.concurrent.collection;

import java.util.LinkedList;
import java.util.Queue;


public class QueueDemo {
	Queue<String> queue = new LinkedList<String>();
	public QueueDemo() {
		queue.offer("1");//将制定的元素插入此队列,插入失败则返回false。而不像collection.add()抛出异常。
		queue.offer("2");
		queue.offer("3");
	}
	public static void main(String args[]){
		QueueDemo demo = new QueueDemo();
		System.out.println(demo.queue.peek());//检索但是不移除此队列的头,如果队列为空,则返回null。
		System.out.println(demo.queue.element());//检索但是不移除此队列的头,如果队列为空,则抛出异常java.util.NoSuchElementException。
		System.out.println(demo.queue.poll());//检索并移除此队列的头,如果队列为空,则返回null
		System.out.println(demo.queue.poll());
		System.out.println(demo.queue.poll());
		System.out.println(demo.queue.remove());////检索并移除此队列的头,如果队列为空,则抛出异常java.util.NoSuchElementException
	}

}

 

相关标签: queue

推荐阅读