• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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: Deque<string | number | boolean | Object> = 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
82class C1 {
83  name: string = ""
84  age: string = ""
85}
86let deque: Deque<string | number | boolean | Array<number> | C1> = new Deque();
87deque.insertFront("a");
88deque.insertFront(1);
89let b = [1, 2, 3];
90deque.insertFront(b);
91let c: C1 = {name : "Dylon", age : "13"};
92deque.insertFront(c);
93deque.insertFront(false);
94```
95
96### insertEnd
97
98insertEnd(element: T): void
99
100Inserts an element at the end of this container.
101
102**System capability**: SystemCapability.Utils.Lang
103
104**Parameters**
105
106| Name| Type| Mandatory| Description|
107| -------- | -------- | -------- | -------- |
108| element | T | Yes| Target element.|
109
110**Error codes**
111
112For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
113
114| ID| Error Message|
115| -------- | -------- |
116| 10200011 | The insertEnd method cannot be bound. |
117
118**Example**
119
120```ts
121class C1 {
122  name: string = ""
123  age: string = ""
124}
125
126let deque: Deque<string | number | boolean | Array<number> | C1> = new Deque();
127deque.insertEnd("a");
128deque.insertEnd(1);
129let b = [1, 2, 3];
130deque.insertEnd(b);
131let c: C1 = {name : "Dylon", age : "13"};
132deque.insertEnd(c);
133deque.insertEnd(false);
134```
135
136### has
137
138has(element: T): boolean
139
140Checks whether this container has the specified element.
141
142**System capability**: SystemCapability.Utils.Lang
143
144**Parameters**
145
146| Name| Type| Mandatory| Description|
147| -------- | -------- | -------- | -------- |
148| element | T | Yes| Target element.|
149
150**Return value**
151
152| Type| Description|
153| -------- | -------- |
154| boolean | Returns **true** if the specified element is contained; returns **false** otherwise.|
155
156**Error codes**
157
158For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
159
160| ID| Error Message|
161| -------- | -------- |
162| 10200011 | The has method cannot be bound. |
163
164**Example**
165
166```ts
167let deque: Deque<string> = new Deque();
168deque.insertFront("squirrel");
169let result = deque.has("squirrel");
170```
171
172### popFirst
173
174popFirst(): T
175
176Removes the first element of this container.
177
178**System capability**: SystemCapability.Utils.Lang
179
180**Return value**
181
182| Type| Description|
183| -------- | -------- |
184| T | First element removed.|
185
186**Error codes**
187
188For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
189
190| ID| Error Message|
191| -------- | -------- |
192| 10200011 | The popFirst method cannot be bound. |
193
194**Example**
195
196```ts
197let deque: Deque<number> = new Deque();
198deque.insertFront(2);
199deque.insertFront(4);
200deque.insertEnd(5);
201deque.insertFront(2);
202deque.insertFront(4);
203let result = deque.popFirst();
204```
205
206### popLast
207
208popLast(): T
209
210Removes the last element of this container.
211
212**System capability**: SystemCapability.Utils.Lang
213
214**Return value**
215
216| Type| Description|
217| -------- | -------- |
218| T | Last element removed.|
219
220**Error codes**
221
222For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
223
224| ID| Error Message|
225| -------- | -------- |
226| 10200011 | The popLast method cannot be bound. |
227
228**Example**
229
230```ts
231let deque: Deque<number> = new Deque();
232deque.insertFront(2);
233deque.insertEnd(4);
234deque.insertFront(5);
235deque.insertFront(2);
236deque.insertFront(4);
237let result = deque.popLast();
238```
239
240### forEach
241
242forEach(callbackFn: (value: T, index?: number, deque?: Deque&lt;T&gt;) => void,
243thisArg?: Object): void
244
245Uses a callback to traverse the elements in this container and obtain their position indexes.
246
247**System capability**: SystemCapability.Utils.Lang
248
249**Parameters**
250
251| Name| Type| Mandatory| Description|
252| -------- | -------- | -------- | -------- |
253| callbackFn | function | Yes| Callback invoked to traverse the elements in the container.|
254| thisArg | Object | No| Value of **this** to use when **callbackFn** is invoked. The default value is this instance.|
255
256callbackFn
257
258| Name| Type| Mandatory| Description|
259| -------- | -------- | -------- | -------- |
260| value | T | Yes| Value of the element that is currently traversed.|
261| index | number | No| Position index of the element that is currently traversed. The default value is **0**.|
262| deque | Deque&lt;T&gt; | No| Instance that calls the **forEach** API. The default value is this instance.|
263
264**Error codes**
265
266For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
267
268| ID| Error Message|
269| -------- | -------- |
270| 10200011 | The forEach method cannot be bound. |
271
272**Example**
273
274```ts
275let deque: Deque<number> = new Deque();
276deque.insertFront(2);
277deque.insertEnd(4);
278deque.insertFront(5);
279deque.insertEnd(4);
280deque.forEach((value: number, index?: number | undefined, deque?: Deque<number> | undefined):void => {
281  console.log("value:" + value, "index:" + index);
282});
283```
284
285### getFirst
286
287getFirst(): T
288
289Obtains the first element of this container.
290
291**System capability**: SystemCapability.Utils.Lang
292
293**Return value**
294
295| Type| Description|
296| -------- | -------- |
297| T | First element of the T type obtained.|
298
299**Error codes**
300
301For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
302
303| ID| Error Message|
304| -------- | -------- |
305| 10200011 | The getFirst method cannot be bound. |
306
307**Example**
308
309```ts
310let deque: Deque<number> = new Deque();
311deque.insertEnd(2);
312deque.insertEnd(4);
313deque.insertFront(5);
314deque.insertFront(4);
315let result = deque.getFirst();
316```
317
318### getLast
319
320getLast(): T
321
322Obtains the last element of this container.
323
324**System capability**: SystemCapability.Utils.Lang
325
326**Return value**
327
328| Type| Description|
329| -------- | -------- |
330| T | Last element of the T type obtained.|
331
332**Error codes**
333
334For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
335
336| ID| Error Message|
337| -------- | -------- |
338| 10200011 | The getLast method cannot be bound. |
339
340**Example**
341
342```ts
343let deque: Deque<number> = new Deque();
344deque.insertFront(2);
345deque.insertFront(4);
346deque.insertFront(5);
347deque.insertFront(4);
348let result = deque.getLast();
349```
350
351### [Symbol.iterator]
352
353[Symbol.iterator]\(): IterableIterator&lt;T&gt;
354
355Obtains an iterator, each item of which is a JavaScript object.
356
357> **NOTE**
358>
359> This API cannot be used in .ets files.
360
361**System capability**: SystemCapability.Utils.Lang
362
363**Return value**
364
365| Type| Description|
366| -------- | -------- |
367| IterableIterator&lt;T&gt; | Iterator obtained.|
368
369**Error codes**
370
371For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
372
373| ID| Error Message|
374| -------- | -------- |
375| 10200011 | The Symbol.iterator method cannot be bound. |
376
377**Example**
378```ts
379let deque: Deque<number> = new Deque();
380deque.insertFront(2);
381deque.insertFront(4);
382deque.insertFront(5);
383deque.insertFront(4);
384
385// Method 1:
386let nums: Array<number> = Array.from(deque)
387for (let item of nums) {
388  console.log("value:" + item);
389}
390
391// Method 2:
392let iter = deque[Symbol.iterator]();
393let temp:IteratorResult<number> = iter.next();
394while(!temp.done) {
395  console.log("value:" + temp.value);
396  temp = iter.next();
397}
398```
399