[Java数据结构]Queue

Queue扩展了Collection,它添加了支持根据先进先出FIFO原则对元素排序的方法。

当对Queue调用add和offer方法时,元素始终添加在Queue的末尾;要检索一个元素,就要使用一个元素,就要使用remove或者poll方法,它们始终删除并返回处于Queue最前面的元素。

例程:

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

public class QueueTest {
    public static void main(String[] args) {
        Queue<String> queue=new LinkedList<String>();
        
        queue.add("first");
        queue.add("second");
        queue.add("third");
        queue.add("fourth");
        
        System.out.println(queue.remove());
        System.out.println(queue.remove());
        System.out.println(queue.remove());
        System.out.println(queue.remove());
    }
}

输出:

first
second
third
fourth

--END--2019-12-25 09:47

原文地址:https://www.cnblogs.com/heyang78/p/12094748.html