Java? 教程(Queue接口)

Queue接口

Queue是在處理之前保存元素的集合,除了基本的Collection操作外,隊列還提供額外的插入、刪除和檢查操作,Queue接口如下。

public interface Queue<E> extends Collection<E> {E element();boolean offer(E e);E peek();E poll();E remove();
}

每個Queue方法都有兩種形式:(1)如果操作失敗則拋出異常,(2)如果操作失敗,則返回特殊值(nullfalse,具體取決于操作),接口的常規結構如下表所示。

操作類型拋出異常返回特殊值
插入add(e)offer(e)
移除remove()poll()
檢查element()peek()

隊列通常(但不一定)以FIFO(先進先出)方式對元素進行排序,優先級隊列除外,它們根據元素的值對元素進行排序 — 有關詳細信息,請參閱“對象排序”部分。無論使用什么排序,隊列的頭部都是通過調用removepoll移除的元素。在FIFO隊列中,所有新元素都插入隊列的尾部,其他類型的隊列可能使用不同的放置規則,每個Queue實現都必須指定其排序屬性。

Queue實現可以限制它所擁有的元素數量,這樣的隊列被稱為有界,java.util.concurrent中的某些Queue實現是有界的,但java.util中的實現不是。

QueueCollection繼承的add方法插入一個元素,除非它違反了隊列的容量限制,在這種情況下它會拋出IllegalStateExceptionoffer方法,僅用于有界隊列,與add不同之處僅在于它通過返回false來表示插入元素失敗。

removepoll方法都移除并返回隊列的頭部,確切地移除哪個元素是隊列的排序策略的函數,僅當隊列為空時,removepoll方法的行為才有所不同,在這些情況下,remove拋出NoSuchElementException,而poll返回null

elementpeek方法返回但不移除隊列的頭部,它們之間的差異與removepoll的方式完全相同:如果隊列為空,則element拋出NoSuchElementException,而peek返回null

隊列實現通常不允許插入null元素,為實現Queue而進行了改進的LinkedList實現是一個例外,由于歷史原因,它允許null元素,但是你應該避免利用它,因為nullpollpeek方法用作特殊的返回值。

隊列實現通常不定義equalshashCode方法的基于元素的版本,而是從Object繼承基于標識的版本。

Queue接口不定義阻塞隊列方法,這在并發編程中很常見,這些等待元素出現或空間可用的方法在java.util.concurrent.BlockingQueue接口中定義,該接口擴展了Queue

在以下示例程序中,隊列用于實現倒數計時器,隊列預先加載了從命令行上指定的數字到0的所有整數值,按降序排列,然后,從隊列中刪除值并以一秒的間隔打印。該程序是人為的,因為在不使用隊列的情況下執行相同的操作會更自然,但它說明了在后續處理之前使用隊列來存儲元素。

import java.util.*;public class Countdown {public static void main(String[] args) throws InterruptedException {int time = Integer.parseInt(args[0]);Queue<Integer> queue = new LinkedList<Integer>();for (int i = time; i >= 0; i--)queue.add(i);while (!queue.isEmpty()) {System.out.println(queue.remove());Thread.sleep(1000);}}
}

在以下示例中,優先級隊列用于對元素集合進行排序,同樣,這個程序是人為的,因為沒有理由使用它來支持集合中提供的排序方法,但它說明了優先級隊列的行為。

static <E> List<E> heapSort(Collection<E> c) {Queue<E> queue = new PriorityQueue<E>(c);List<E> result = new ArrayList<E>();while (!queue.isEmpty())result.add(queue.remove());return result;
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/388208.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/388208.shtml
英文地址,請注明出處:http://en.pswp.cn/news/388208.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

字符串操作截取后面的字符串_對字符串的5個必知的熊貓操作

字符串操作截取后面的字符串We have to represent every bit of data in numerical values to be processed and analyzed by machine learning and deep learning models. However, strings do not usually come in a nice and clean format and require preprocessing to con…

最新 Unity3D鼠標滑輪控制物體放大縮小 [

var s 1.0;function Update () {var cube GameObject.Find("Cube");if(Input.GetAxis("Mouse ScrollWheel")){s Input.GetAxis("Mouse ScrollWheel");cube.transform.localScaleVector3(1*s,1*s,1*s);}}

sublime-text3 安裝 emmet 插件

下載sublime&#xff0c;http://www.sublimetext.com/ 安裝package control &#xff1a;https://packagecontrol.io/ins... 這個地址需要翻墻&#xff0c;訪問不了的可以看下圖 import urllib.request,os,hashlib; h 6f4c264a24d933ce70df5dedcf1dcaee ebe013ee18cced0ef93d…

數據科學家訪談錄 百度網盤_您應該在數據科學訪談中向THEM提問。

數據科學家訪談錄 百度網盤A quick search on Medium with the keywords “Data Science Interview” resulted in hundreds of Medium articles to help guide the reader from what concepts are covered to even specific company interviews ranging from Tesla, Walmart, …

unity3d]鼠標點擊地面人物自動走動(也包含按鍵wasdspace控制)

目錄(?)[-] 一效果圖二大概步驟 創建一個plane設置層為Terrain因為后面要判斷是否點擊的是這個層準備好人物模型并且將三個腳本拖放到人物上并且將動畫文件也拖放好記得看前面提醒哦 ThirdPersonCamera相當于smoothflowThirdPersonController修改版mouseMoveContr鼠標點擊人物…

uva 524(Prime Ring Problem UVA - 524 )

dfs練習題,我素數打表的時候ji了&#xff0c;一直沒發現實際上是ji*i&#xff0c;以后可記住了。還有最后一行不能有空格。。。昏迷了半天 我的代碼(紫書上的算法) #include <bits/stdc.h> using namespace std; int bk[110]; int num[110]; int vis[110]; int n; void d…

Web 開發基礎

一、 Web 開發簡介 最早的軟件都是運行在大型機上的&#xff0c;軟件使用者登陸到大型機上去運行軟件。后來隨著 PC 機的興起&#xff0c;軟件開始主要運行在桌面上&#xff0c;而數據庫這樣的軟件運行在服務器端&#xff0c;這種 Client/Server 模式簡稱 CS 架構。隨著互聯網的…

power bi函數_在Power BI中的行上使用聚合函數

power bi函數Aggregate functions are one of the main building blocks in Power BI. Being used explicitly in measures, or implicitly defined by Power BI, there is no single Power BI report which doesn’t use some sort of aggregate functions.聚合功能是Power BI…

關于如何在Python中使用靜態、類或抽象方法的權威指南

Python中方法的工作方式 方法是存儲在類屬性中的函數&#xff0c;你可以用下面這種方式聲明和訪問一個函數 >>> class Pizza(object):... def __init__(self, size):... self.size size... def get_size(self):... return self.size...>&…

廣義估計方程估計方法_廣義估計方程簡介

廣義估計方程估計方法A key assumption underpinning generalized linear models (which linear regression is a type of) is the independence of observations. In longitudinal data this will simply not hold. Observations within an individual (between time points) …

css二

結構性偽類:nth-child(index)系列1.:first-child2.:last-child3.nth-last-child(index)4.only-child :nth-of-type(index)系列1.first-of-type2.last-of-type3.nth-last-type(index)4.only-of-type :not偽類處理導航欄最后一個豎劃線a:not(:last-of-type) :empty偽類 選中所有內…

Unity3d鼠標點擊屏幕來控制人物的走動

今天呢&#xff0c;我們來一起實現一個在RPG中游戲中十分常見的功能&#xff0c;通過鼠標點擊屏幕來控制人物的走動。首先來說一下原理&#xff0c;當我們點擊屏幕時&#xff0c;我們按照一定的方法&#xff0c;將屏幕上的二維坐標轉化為三維坐標&#xff0c;然后我們從攝像機位…

Java中的ReentrantLock和synchronized兩種鎖定機制的對比

2019獨角獸企業重金招聘Python工程師標準>>> 多線程和并發性并不是什么新內容&#xff0c;但是 Java 語言設計中的創新之一就是&#xff0c;它是第一個直接把跨平臺線程模型和正規的內存模型集成到語言中的主流語言。核心類庫包含一個 Thread 類&#xff0c;可以用它…

10.15 lzxkj

幾天前寫的&#xff0c;忘了放了&#xff0c;在此填坑 10月16的題我出的不寫題解了 lzxkj 題目背景 眾所不周知的是&#xff0c; 酒店之王 xkj 一個經常迷失自我的人 有一天&#xff0c; 當起床鈴再一次打響的時候&#xff0c; TA 用 O(1)的時間在 TA 那早就已經生銹的大腦中自…

大數定理 中心極限定理_中心極限定理:直觀的遍歷

大數定理 中心極限定理One of the most beautiful concepts in statistics and probability is Central Limit Theorem,people often face difficulties in getting a clear understanding of this and the related concepts, I myself struggled understanding this during my…

萬惡之源 - Python數據類型二

列表 列表的介紹 列表是python的基礎數據類型之一 ,其他編程語言也有類似的數據類型. 比如JS中的數 組, java中的數組等等. 它是以[ ]括起來, 每個元素用 , 隔開而且可以存放各種數據類型: lst [1,a,True,[2,3,4]]列表相比于字符串,不僅可以存放不同的數據類型.而且可…

230. Kth Smallest Element in a BST

Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.Note: You may assume k is always valid, 1 ≤ k ≤ BSTs total elements.Example 1: Input: root [3,1,4,null,2], k 13/ \1 4\2 Output: 1 Example 2: Input: root …

探索性數據分析(EDA)-不要問如何,不要問什么

數據科學 &#xff0c; 機器學習 (Data Science, Machine Learning) This is part 1 in a series of articles guiding the reader through an entire data science project.這是一系列文章的第1部分 &#xff0c;指導讀者完成整個數據科學項目。 I am a new writer on Medium…

unity3d 攝像機跟隨鼠標和鍵盤的控制

鼠標控制&#xff1a; using UnityEngine; using System.Collections; public class shubiao : MonoBehaviour { //public Transform firepos; public int Ball30; public int CurBall1; public Rigidbody projectile; public Vector3 point; public float time100f; public…

《必然》九、享受重混盛宴,是每個人的機會

今天說的是《必然》的第七個關鍵詞&#xff0c;過濾Filtering。1我們需要過濾如今有一個問題&#xff0c;彌漫在我們的生活當中&#xff0c;困擾著所有人。那就是“今天我要吃什么呢&#xff1f;”同樣的&#xff0c;書店里這么多的書&#xff0c;我要看哪一本呢&#xff1f;網…