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** 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 '@kit.ArkTS';
23```
24
25## Deque
26
27### Attributes
28
29**Atomic service API**: This API can be used in atomic services since API version 12.
30
31**System capability**: SystemCapability.Utils.Lang
32
33| Name| Type| Readable| Writable| Description|
34| -------- | -------- | -------- | -------- | -------- |
35| length | number | Yes| No| Number of elements in a deque (called container later).|
36
37### constructor
38
39constructor()
40
41A constructor used to create a **Deque** instance.
42
43**Atomic service API**: This API can be used in atomic services since API version 12.
44
45**System capability**: SystemCapability.Utils.Lang
46
47**Error codes**
48
49For details about the error codes, see [Utils Error Codes](errorcode-utils.md).
50
51| ID| Error Message|
52| -------- | -------- |
53| 10200012 | The Deque's constructor cannot be directly invoked. |
54
55**Example**
56
57```ts
58let deque: Deque<string | number | boolean | Object> = new Deque();
59```
60
61### insertFront
62
63insertFront(element: T): void
64
65Inserts an element at the front of this container.
66
67**Atomic service API**: This API can be used in atomic services since API version 12.
68
69**System capability**: SystemCapability.Utils.Lang
70
71**Parameters**
72
73| Name| Type| Mandatory| Description|
74| -------- | -------- | -------- | -------- |
75| element | T | Yes| Target element.|
76
77**Error codes**
78
79For details about the error codes, see [Utils Error Codes](errorcode-utils.md).
80
81| ID| Error Message|
82| -------- | -------- |
83| 10200011 | The insertFront method cannot be bound. |
84
85**Example**
86
87```ts
88class C1 {
89  name: string = ""
90  age: string = ""
91}
92let deque: Deque<string | number | boolean | Array<number> | C1> = new Deque();
93deque.insertFront("a");
94deque.insertFront(1);
95let b = [1, 2, 3];
96deque.insertFront(b);
97let c: C1 = {name : "Dylan", age : "13"};
98deque.insertFront(c);
99deque.insertFront(false);
100```
101
102### insertEnd
103
104insertEnd(element: T): void
105
106Inserts an element at the end of this container.
107
108**Atomic service API**: This API can be used in atomic services since API version 12.
109
110**System capability**: SystemCapability.Utils.Lang
111
112**Parameters**
113
114| Name| Type| Mandatory| Description|
115| -------- | -------- | -------- | -------- |
116| element | T | Yes| Target element.|
117
118**Error codes**
119
120For details about the error codes, see [Utils Error Codes](errorcode-utils.md).
121
122| ID| Error Message|
123| -------- | -------- |
124| 10200011 | The insertEnd method cannot be bound. |
125
126**Example**
127
128```ts
129class C1 {
130  name: string = ""
131  age: string = ""
132}
133
134let deque: Deque<string | number | boolean | Array<number> | C1> = new Deque();
135deque.insertEnd("a");
136deque.insertEnd(1);
137let b = [1, 2, 3];
138deque.insertEnd(b);
139let c: C1 = {name : "Dylan", age : "13"};
140deque.insertEnd(c);
141deque.insertEnd(false);
142```
143
144### has
145
146has(element: T): boolean
147
148Checks whether this container has the specified element.
149
150**Atomic service API**: This API can be used in atomic services since API version 12.
151
152**System capability**: SystemCapability.Utils.Lang
153
154**Parameters**
155
156| Name| Type| Mandatory| Description|
157| -------- | -------- | -------- | -------- |
158| element | T | Yes| Target element.|
159
160**Return value**
161
162| Type| Description|
163| -------- | -------- |
164| boolean | Returns **true** if the specified element is contained; returns **false** otherwise.|
165
166**Error codes**
167
168For details about the error codes, see [Utils Error Codes](errorcode-utils.md).
169
170| ID| Error Message|
171| -------- | -------- |
172| 10200011 | The has method cannot be bound. |
173
174**Example**
175
176```ts
177let deque: Deque<string> = new Deque();
178deque.insertFront("squirrel");
179let result = deque.has("squirrel");
180```
181
182### popFirst
183
184popFirst(): T
185
186Removes the first element of this container.
187
188**Atomic service API**: This API can be used in atomic services since API version 12.
189
190**System capability**: SystemCapability.Utils.Lang
191
192**Return value**
193
194| Type| Description|
195| -------- | -------- |
196| T | First element removed.|
197
198**Error codes**
199
200For details about the error codes, see [Utils Error Codes](errorcode-utils.md).
201
202| ID| Error Message|
203| -------- | -------- |
204| 10200011 | The popFirst method cannot be bound. |
205
206**Example**
207
208```ts
209let deque: Deque<number> = new Deque();
210deque.insertFront(2);
211deque.insertFront(4);
212deque.insertEnd(5);
213deque.insertFront(2);
214deque.insertFront(4);
215let result = deque.popFirst();
216```
217
218### popLast
219
220popLast(): T
221
222Removes the last element of this container.
223
224**Atomic service API**: This API can be used in atomic services since API version 12.
225
226**System capability**: SystemCapability.Utils.Lang
227
228**Return value**
229
230| Type| Description|
231| -------- | -------- |
232| T | Last element removed.|
233
234**Error codes**
235
236For details about the error codes, see [Utils Error Codes](errorcode-utils.md).
237
238| ID| Error Message|
239| -------- | -------- |
240| 10200011 | The popLast method cannot be bound. |
241
242**Example**
243
244```ts
245let deque: Deque<number> = new Deque();
246deque.insertFront(2);
247deque.insertEnd(4);
248deque.insertFront(5);
249deque.insertFront(2);
250deque.insertFront(4);
251let result = deque.popLast();
252```
253
254### forEach
255
256forEach(callbackFn: (value: T, index?: number, deque?: Deque&lt;T&gt;) => void,
257thisArg?: Object): void
258
259Uses a callback to traverse the elements in this container and obtain their position indexes.
260
261**Atomic service API**: This API can be used in atomic services since API version 12.
262
263**System capability**: SystemCapability.Utils.Lang
264
265**Parameters**
266
267| Name| Type| Mandatory| Description|
268| -------- | -------- | -------- | -------- |
269| callbackFn | function | Yes| Callback invoked to traverse the elements in the container.|
270| thisArg | Object | No| Value of **this** to use when **callbackFn** is invoked. The default value is this instance.|
271
272callbackFn
273
274| Name| Type| Mandatory| Description|
275| -------- | -------- | -------- | -------- |
276| value | T | Yes| Value of the element that is currently traversed.|
277| index | number | No| Position index of the element that is currently traversed. The default value is **0**.|
278| deque | Deque&lt;T&gt; | No| Instance that calls the **forEach** API. The default value is this instance.|
279
280**Error codes**
281
282For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
283
284| ID| Error Message|
285| -------- | -------- |
286| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
287| 10200011 | The forEach method cannot be bound. |
288
289**Example**
290
291```ts
292let deque: Deque<number> = new Deque();
293deque.insertFront(2);
294deque.insertEnd(4);
295deque.insertFront(5);
296deque.insertEnd(4);
297deque.forEach((value: number, index?: number | undefined, deque?: Deque<number> | undefined):void => {
298  console.log("value:" + value, "index:" + index);
299});
300```
301
302### getFirst
303
304getFirst(): T
305
306Obtains the first element of this container.
307
308**Atomic service API**: This API can be used in atomic services since API version 12.
309
310**System capability**: SystemCapability.Utils.Lang
311
312**Return value**
313
314| Type| Description|
315| -------- | -------- |
316| T | First element of the T type obtained.|
317
318**Error codes**
319
320For details about the error codes, see [Utils Error Codes](errorcode-utils.md).
321
322| ID| Error Message|
323| -------- | -------- |
324| 10200011 | The getFirst method cannot be bound. |
325
326**Example**
327
328```ts
329let deque: Deque<number> = new Deque();
330deque.insertEnd(2);
331deque.insertEnd(4);
332deque.insertFront(5);
333deque.insertFront(4);
334let result = deque.getFirst();
335```
336
337### getLast
338
339getLast(): T
340
341Obtains the last element of this container.
342
343**Atomic service API**: This API can be used in atomic services since API version 12.
344
345**System capability**: SystemCapability.Utils.Lang
346
347**Return value**
348
349| Type| Description|
350| -------- | -------- |
351| T | Last element of the T type obtained.|
352
353**Error codes**
354
355For details about the error codes, see [Utils Error Codes](errorcode-utils.md).
356
357| ID| Error Message|
358| -------- | -------- |
359| 10200011 | The getLast method cannot be bound. |
360
361**Example**
362
363```ts
364let deque: Deque<number> = new Deque();
365deque.insertFront(2);
366deque.insertFront(4);
367deque.insertFront(5);
368deque.insertFront(4);
369let result = deque.getLast();
370```
371
372### [Symbol.iterator]
373
374[Symbol.iterator]\(): IterableIterator&lt;T&gt;
375
376Obtains an iterator, each item of which is a JavaScript object.
377
378**Atomic service API**: This API can be used in atomic services since API version 12.
379
380**System capability**: SystemCapability.Utils.Lang
381
382**Return value**
383
384| Type| Description|
385| -------- | -------- |
386| IterableIterator&lt;T&gt; | Iterator obtained.|
387
388**Error codes**
389
390For details about the error codes, see [Utils Error Codes](errorcode-utils.md).
391
392| ID| Error Message|
393| -------- | -------- |
394| 10200011 | The Symbol.iterator method cannot be bound. |
395
396**Example**
397```ts
398let deque: Deque<number> = new Deque();
399deque.insertFront(2);
400deque.insertFront(4);
401deque.insertFront(5);
402deque.insertFront(4);
403
404// Method 1:
405let nums: Array<number> = Array.from(deque)
406for (let item of nums) {
407  console.log("value:" + item);
408}
409
410// Method 2:
411let iter = deque[Symbol.iterator]();
412let temp:IteratorResult<number> = iter.next();
413while(!temp.done) {
414  console.log("value:" + temp.value);
415  temp = iter.next();
416}
417```
418