首页 > 代码库 > 【DataStructure】Description and usage of queue
【DataStructure】Description and usage of queue
【Description】
A queue is a collection that implements the first-in-first-out protocal. This means that the only accessiable object in the collection in the first one that was inserted. The most common example of a queue is a waiting line.
【Interface】
In the java Collections Framework includes a queue interface, which is implemented by four classes: the linkedList class, the AbstractQueue class, the priorityQUeue class, and the ArrayDeque class. For simple FIFO queues, the arrayDeque class the best choice:
Queue<String> queue = new ArrayDeque<String>();
【Demo】
package com.albertshao.ds.queue; // Data Structures with Java, Second Edition // by John R. Hubbard // Copyright 2007 by McGraw-Hill import java.util.*; public class TestStringQueue { public static void main(String[] args) { Queue<String> queue = new ArrayDeque<String>(); queue.add("GB"); queue.add("DE"); queue.add("FR"); queue.add("ES"); System.out.println(queue); System.out.println("queue.element(): " + queue.element()); System.out.println("queue.remove(): " + queue.remove()); System.out.println(queue); System.out.println("queue.remove(): " + queue.remove()); System.out.println(queue); System.out.println("queue.add(\"IE\"): "); queue.add("IE"); System.out.println(queue); System.out.println("queue.remove(): " + queue.remove()); System.out.println(queue); } }
【Result】
[GB, DE, FR, ES] queue.element(): GB queue.remove(): GB [DE, FR, ES] queue.remove(): DE [FR, ES] queue.add("IE"): [FR, ES, IE] queue.remove(): FR [ES, IE]
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。