1# @ohos.util.Deque (Linear Container Deque) 2 3Double-ended queue (deque) is a sequence container implemented based on the queue data structure that follows the principles of First In First Out (FIFO) and Last In First Out (LIFO). It allows insertion and removal of elements at both the ends. **Deque** can dynamically adjust the capacity based on project requirements. It doubles the capacity each time. **Deque** differs from **[Queue](js-apis-queue.md)** and **[Vector](js-apis-vector.md)** mainly in the following aspects: 4 5**Queue** follows the principle of FIFO only and allows element removal at the front and insertion at the rear. 6 7**Vector** supports insertion and deletion of elements in between, as well as at both the ends. When compared with **Vector**, **Deque** is more efficient in inserting and removing header elements, but less efficient in accessing elements. 8 9**Recommended use case**: Use **Deque** when you need to frequently insert or remove elements at both the ends of a container. 10 11This topic uses the following to identify the use of generics: 12- T: Type 13 14> **NOTE** 15> 16> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version. 17 18 19## Modules to Import 20 21```ts 22import Deque from '@ohos.util.Deque'; 23``` 24 25## Deque 26 27### Attributes 28 29**System capability**: SystemCapability.Utils.Lang 30 31| Name| Type| Readable| Writable| Description| 32| -------- | -------- | -------- | -------- | -------- | 33| length | number | Yes| No| Number of elements in a deque (called container later).| 34 35### constructor 36 37constructor() 38 39A constructor used to create a **Deque** instance. 40 41**System capability**: SystemCapability.Utils.Lang 42 43**Error codes** 44 45For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 46 47| ID| Error Message| 48| -------- | -------- | 49| 10200012 | The Deque's constructor cannot be directly invoked. | 50 51**Example** 52 53```ts 54let deque = new Deque(); 55``` 56 57### insertFront 58 59insertFront(element: T): void 60 61Inserts an element at the front of this container. 62 63**System capability**: SystemCapability.Utils.Lang 64 65**Parameters** 66 67| Name| Type| Mandatory| Description| 68| -------- | -------- | -------- | -------- | 69| element | T | Yes| Target element.| 70 71**Error codes** 72 73For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 74 75| ID| Error Message| 76| -------- | -------- | 77| 10200011 | The insertFront method cannot be bound. | 78 79**Example** 80 81```ts 82let deque = new Deque(); 83deque.insertFront("a"); 84deque.insertFront(1); 85let b = [1, 2, 3]; 86deque.insertFront(b); 87let c = {name : "Dylon", age : "13"}; 88deque.insertFront(c); 89deque.insertFront(false); 90``` 91 92### insertEnd 93 94insertEnd(element: T): void 95 96Inserts an element at the end of this container. 97 98**System capability**: SystemCapability.Utils.Lang 99 100**Parameters** 101 102| Name| Type| Mandatory| Description| 103| -------- | -------- | -------- | -------- | 104| element | T | Yes| Target element.| 105 106**Error codes** 107 108For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 109 110| ID| Error Message| 111| -------- | -------- | 112| 10200011 | The insertEnd method cannot be bound. | 113 114**Example** 115 116```ts 117let deque = new Deque(); 118deque.insertEnd("a"); 119deque.insertEnd(1); 120let b = [1, 2, 3]; 121deque.insertEnd(b); 122let c = {name : "Dylon", age : "13"}; 123deque.insertEnd(c); 124deque.insertEnd(false); 125``` 126 127### has 128 129has(element: T): boolean 130 131Checks whether this container has the specified element. 132 133**System capability**: SystemCapability.Utils.Lang 134 135**Parameters** 136 137| Name| Type| Mandatory| Description| 138| -------- | -------- | -------- | -------- | 139| element | T | Yes| Target element.| 140 141**Return value** 142 143| Type| Description| 144| -------- | -------- | 145| boolean | Returns **true** if the specified element is contained; returns **false** otherwise.| 146 147**Error codes** 148 149For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 150 151| ID| Error Message| 152| -------- | -------- | 153| 10200011 | The has method cannot be bound. | 154 155**Example** 156 157```ts 158let deque = new Deque(); 159let result = deque.has("squirrel"); 160deque.insertFront("squirrel"); 161let result1 = deque.has("squirrel"); 162``` 163 164### popFirst 165 166popFirst(): T 167 168Removes the first element of this container. 169 170**System capability**: SystemCapability.Utils.Lang 171 172**Return value** 173 174| Type| Description| 175| -------- | -------- | 176| T | First element removed.| 177 178**Error codes** 179 180For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 181 182| ID| Error Message| 183| -------- | -------- | 184| 10200011 | The popFirst method cannot be bound. | 185 186**Example** 187 188```ts 189let deque = new Deque(); 190deque.insertFront(2); 191deque.insertFront(4); 192deque.insertEnd(5); 193deque.insertFront(2); 194deque.insertFront(4); 195let result = deque.popFirst(); 196``` 197 198### popLast 199 200popLast(): T 201 202Removes the last element of this container. 203 204**System capability**: SystemCapability.Utils.Lang 205 206**Return value** 207 208| Type| Description| 209| -------- | -------- | 210| T | Last element removed.| 211 212**Error codes** 213 214For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 215 216| ID| Error Message| 217| -------- | -------- | 218| 10200011 | The popLast method cannot be bound. | 219 220**Example** 221 222```ts 223let deque = new Deque(); 224deque.insertFront(2); 225deque.insertEnd(4); 226deque.insertFront(5); 227deque.insertFront(2); 228deque.insertFront(4); 229let result = deque.popLast(); 230``` 231 232### forEach 233 234forEach(callbackFn: (value: T, index?: number, deque?: Deque<T>) => void, 235thisArg?: Object): void 236 237Uses a callback to traverse the elements in this container and obtain their position indexes. 238 239**System capability**: SystemCapability.Utils.Lang 240 241**Parameters** 242 243| Name| Type| Mandatory| Description| 244| -------- | -------- | -------- | -------- | 245| callbackFn | function | Yes| Callback invoked to traverse the elements in the container.| 246| thisArg | Object | No| Value to use when the callback is invoked.| 247 248callbackfn 249 250| Name| Type| Mandatory| Description| 251| -------- | -------- | -------- | -------- | 252| value | T | Yes| Value of the element that is currently traversed.| 253| index | number | No| Position index of the element that is currently traversed.| 254| deque | Deque<T> | No| Instance that invokes the **forEach** method.| 255 256**Error codes** 257 258For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 259 260| ID| Error Message| 261| -------- | -------- | 262| 10200011 | The forEach method cannot be bound. | 263 264**Example** 265 266```ts 267let deque = new Deque(); 268deque.insertFront(2); 269deque.insertEnd(4); 270deque.insertFront(5); 271deque.insertEnd(4); 272deque.forEach((value, index) => { 273 console.log("value:" + value, "index:" + index); 274}); 275``` 276 277### getFirst 278 279getFirst(): T 280 281Obtains the first element of this container. 282 283**System capability**: SystemCapability.Utils.Lang 284 285**Return value** 286 287| Type| Description| 288| -------- | -------- | 289| T | Element obtained.| 290 291**Error codes** 292 293For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 294 295| ID| Error Message| 296| -------- | -------- | 297| 10200011 | The getFirst method cannot be bound. | 298 299**Example** 300 301```ts 302let deque = new Deque(); 303deque.insertEnd(2); 304deque.insertEnd(4); 305deque.insertFront(5); 306deque.insertFront(4); 307let result = deque.getFirst(); 308``` 309 310### getLast 311 312getLast(): T 313 314Obtains the last element of this container. 315 316**System capability**: SystemCapability.Utils.Lang 317 318**Return value** 319 320| Type| Description| 321| -------- | -------- | 322| T | Element obtained.| 323 324**Error codes** 325 326For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 327 328| ID| Error Message| 329| -------- | -------- | 330| 10200011 | The getLast method cannot be bound. | 331 332**Example** 333 334```ts 335let deque = new Deque(); 336deque.insertFront(2); 337deque.insertFront(4); 338deque.insertFront(5); 339deque.insertFront(4); 340let result = deque.getLast(); 341``` 342 343### [Symbol.iterator] 344 345[Symbol.iterator]\(): IterableIterator<T> 346 347Obtains an iterator, each item of which is a JavaScript object. 348 349**System capability**: SystemCapability.Utils.Lang 350 351**Return value** 352 353| Type| Description| 354| -------- | -------- | 355| IterableIterator<T> | Iterator obtained.| 356 357**Error codes** 358 359For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 360 361| ID| Error Message| 362| -------- | -------- | 363| 10200011 | The Symbol.iterator method cannot be bound. | 364 365**Example** 366```ts 367let deque = new Deque(); 368deque.insertFront(2); 369deque.insertFront(4); 370deque.insertFront(5); 371deque.insertFront(4); 372 373// Method 1: 374for (let item of deque) { 375 console.log("value:" + item); 376} 377 378// Method 2: 379let iter = deque[Symbol.iterator](); 380let temp = iter.next().value; 381while(temp != undefined) { 382 console.log("value:" + temp); 383 temp = iter.next().value; 384} 385``` 386