队列通常(但并非一定)以 FIFO(先进先出)的方式排序各个元素。不过优先级队列和 LIFO 队列(或堆栈)例外,前者根据提供的比较器或元素的自然顺序对元素进行排序,后者按 LIFO(后进先出)的方式对元素进行排序。无论使用哪种排序方式,队列的头都是调用remove()或poll()所移除的元素。在 FIFO 队列中,所有的新元素都插入队列的末尾。其他种类的队列可能使用不同的元素放置规则。每个Queue实现必须指定其顺序属性。
offer 添加一个元素并返回true 如果队列已满,则返回false
poll 移除并返问队列头部的元素如果队列为空,则返回null
peek 返回队列头部的元素 如果队列为空,则返回null
put 添加一个元素 如果队列满,则阻塞
take移除并返回队列头部的元素 如果队列为空,则阻塞
element 返回队列头部的元素 如果队列为空,则抛出一个NoSuchElementException异常
add增加一个元索 如果队列已满,则抛出一个IIIegaISlabEepeplian异常
remove 移除并返回队列头部的元素如果队列为空,则抛出一个
NoSuchElementException异常
注意:poll和peek方法出错进返回null。因此,向队列中插入null值是不合法的。
还有带超时的offer和poll方法重载,例如,下面的调用:
boolean success = q.offer(x,100,TimeUnit.MILLISECONDS)
尝试在100毫秒内向队列尾部插入一个元素。如果成功,立即返回true;否则,当到达超时进,返回false。同样地,调用:
Object head = q.poll(100, TimeUnit.MILLISECONDS)
如果在100毫秒内成功地移除了队列头元素,则立即返回头元素;否则在到达超时时,返回null。
阻塞操作有put和take。put方法在队列满时阻塞,take方法在队列空时阻塞。
Queue接口与List、Set同一级别,都是继承了Collection接口。LinkedList实现了Queue接 口。Queue接口窄化了对LinkedList的方法的访问权限(即在方法中的参数类型如果是Queue时,就完全只能访问Queue接口所定义的方法 了,而不能直接访问 LinkedList的非Queue的方法),以使得只有恰当的方法才可以使用。BlockingQueue 继承了Queue接口。
java使用数据结构来实现FIFO先进先出的队列,实例如下:
/** To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package linkedlisttest
import java.util.ArrayList
import java.util.Deque
import java.util.LinkedList
import java.util.List
/**
*
* @author Vicky.H
* @email [email protected]
*/
public class FIFOTest {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
FIFO<A> fifo = new FIFOImpl<A>(5)
for (int i = 0 i < 20 i++) {
A a = new A("A:" + i)
A head = fifo.addLastSafe(a)
System.out.println(i + "\thead:" + head + "\tsize:" + fifo.size())
}
System.out.println("---------------")
System.out.println("弹出数据")
List<A> polls = fifo.setMaxSize(3)
for (A a : polls) {
System.out.println("\thead:" + a)
}
System.out.println("剩余数据")
for (A a : fifo) {
System.out.println("\thead:" + a)
}
System.out.println(fifo.size())
}
}
interface FIFO<T> extends List<T>, Deque<T>, Cloneable, java.io.Serializable {
/**
* 向最后添加一个新的,如果长度超过允许的最大值,则弹出一个 *
*/
T addLastSafe(T addLast)
/**
* 弹出head,如果Size = 0返回null。而不同于pop抛出异常
* @return
*/
T pollSafe()
/**
* 获得最大保存
*
* @return
*/
int getMaxSize()
/**
* 设置最大存储范围
*
* @return 返回的是,因为改变了队列大小,导致弹出的head
*/
List<T> setMaxSize(int maxSize)
}
class FIFOImpl<T> extends LinkedList<T> implements FIFO<T> {
private int maxSize = Integer.MAX_VALUE
private final Object synObj = new Object()
public FIFOImpl() {
super()
}
public FIFOImpl(int maxSize) {
super()
this.maxSize = maxSize
}
@Override
public T addLastSafe(T addLast) {
synchronized (synObj) {
T head = null
while (size() >= maxSize) {
head = poll()
}
addLast(addLast)
return head
}
}
@Override
public T pollSafe() {
synchronized (synObj) {
return poll()
}
}
@Override
public List<T> setMaxSize(int maxSize) {
List<T> list = null
if (maxSize < this.maxSize) {
list = new ArrayList<T>()
synchronized (synObj) {
while (size() > maxSize) {
list.add(poll())
}
}
}
this.maxSize = maxSize
return list
}
@Override
public int getMaxSize() {
return this.maxSize
}
}
class A {
private String name
public A() {
}
public A(String name) {
this.name = name
}
public String getName() {
return name
}
public void setName(String name) {
this.name = name
}
@Override
public String toString() {
return "A{" + "name=" + name + '}'
}
}
代码如下,可以直接运行。public static void main(String[] args) {
final int M = 6// number of girls,可改动
final int N = 7// number of boys,可改动
int x = 3// some boy,可改动
int y = 5// some girl,可改动
String result = ""// 记录结果,即第二个问题
// 初始化,假设队列存放男女生编号,从1开始
Queue<Integer>boys = new LinkedList<Integer>()
for (int i = 1i <= Ni++) {
boys.add(i)
}
Queue<Integer>girls = new LinkedList<Integer>()
for (int i = 1i <= Mi++) {
girls.add(i)
}
// 跳舞开始
int min = boys.size() >girls.size() ? girls.size() : boys.size()
int k = 1// songs
int count = 2// 求出两个值,可改动
while (k <1000) {//为了不死循环,这里假设最多有999支舞蹈
System.out.println("***This is the " + k + "st dance:")
for (int i = 0i <mini++) {
// 跳舞,第一个问题:输出每曲配对情况
System.out.println("Boy " + boys.peek() + " <=>Girl "
+ girls.peek())
// 跳过的排到对尾
int boy = boys.remove()
boys.add(boy)
int girl = girls.remove()
girls.add(girl)
// 判断 x和y跳舞了没有
if (boy == x &&girl == y) {
result += k + ","
count--
}
}
if (count == 0)
break
// next dance
k++
}
// 结果
if (count == 0)
System.out.println("\n***Boy " + x + " and Girl " + y
+ " dance together in : " + result)//第二个问题的解答,跳了哪几支舞
else
System.out.println("\n***Boy " + x + " and Girl " + y
+ " have no chance to dance!")//第二个问题的解答,两人没机会跳舞
}