• 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 = 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();
159deque.insertFront("squirrel");
160let result = deque.has("squirrel");
161```
162
163### popFirst
164
165popFirst(): T
166
167Removes the first element of this container.
168
169**System capability**: SystemCapability.Utils.Lang
170
171**Return value**
172
173| Type| Description|
174| -------- | -------- |
175| T | First element removed.|
176
177**Error codes**
178
179For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
180
181| ID| Error Message|
182| -------- | -------- |
183| 10200011 | The popFirst method cannot be bound. |
184
185**Example**
186
187```ts
188let deque = new Deque();
189deque.insertFront(2);
190deque.insertFront(4);
191deque.insertEnd(5);
192deque.insertFront(2);
193deque.insertFront(4);
194let result = deque.popFirst();
195```
196
197### popLast
198
199popLast(): T
200
201Removes the last element of this container.
202
203**System capability**: SystemCapability.Utils.Lang
204
205**Return value**
206
207| Type| Description|
208| -------- | -------- |
209| T | Last element removed.|
210
211**Error codes**
212
213For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
214
215| ID| Error Message|
216| -------- | -------- |
217| 10200011 | The popLast method cannot be bound. |
218
219**Example**
220
221```ts
222let deque = new Deque();
223deque.insertFront(2);
224deque.insertEnd(4);
225deque.insertFront(5);
226deque.insertFront(2);
227deque.insertFront(4);
228let result = deque.popLast();
229```
230
231### forEach
232
233forEach(callbackFn: (value: T, index?: number, deque?: Deque<T>) => void,
234thisArg?: Object): void
235
236Uses a callback to traverse the elements in this container and obtain their position indexes.
237
238**System capability**: SystemCapability.Utils.Lang
239
240**Parameters**
241
242| Name| Type| Mandatory| Description|
243| -------- | -------- | -------- | -------- |
244| callbackFn | function | Yes| Callback invoked to traverse the elements in the container.|
245| thisArg | Object | No| Value to use when the callback is invoked.|
246
247callbackfn
248
249| Name| Type| Mandatory| Description|
250| -------- | -------- | -------- | -------- |
251| value | T | Yes| Value of the element that is currently traversed.|
252| index | number | No| Position index of the element that is currently traversed.|
253| deque | Deque<T> | No| Instance that invokes the **forEach** method.|
254
255**Error codes**
256
257For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
258
259| ID| Error Message|
260| -------- | -------- |
261| 10200011 | The forEach method cannot be bound. |
262
263**Example**
264
265```ts
266let deque = new Deque();
267deque.insertFront(2);
268deque.insertEnd(4);
269deque.insertFront(5);
270deque.insertEnd(4);
271deque.forEach((value, index) => {
272    console.log("value:" + value, "index:" + index);
273});
274```
275
276### getFirst
277
278getFirst(): T
279
280Obtains the first element of this container.
281
282**System capability**: SystemCapability.Utils.Lang
283
284**Return value**
285
286| Type| Description|
287| -------- | -------- |
288| T | Element obtained.|
289
290**Error codes**
291
292For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
293
294| ID| Error Message|
295| -------- | -------- |
296| 10200011 | The getFirst method cannot be bound. |
297
298**Example**
299
300```ts
301let deque = new Deque();
302deque.insertEnd(2);
303deque.insertEnd(4);
304deque.insertFront(5);
305deque.insertFront(4);
306let result = deque.getFirst();
307```
308
309### getLast
310
311getLast(): T
312
313Obtains the last element of this container.
314
315**System capability**: SystemCapability.Utils.Lang
316
317**Return value**
318
319| Type| Description|
320| -------- | -------- |
321| T | Element obtained.|
322
323**Error codes**
324
325For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
326
327| ID| Error Message|
328| -------- | -------- |
329| 10200011 | The getLast method cannot be bound. |
330
331**Example**
332
333```ts
334let deque = new Deque();
335deque.insertFront(2);
336deque.insertFront(4);
337deque.insertFront(5);
338deque.insertFront(4);
339let result = deque.getLast();
340```
341
342### [Symbol.iterator]
343
344[Symbol.iterator]\(): IterableIterator<T>
345
346Obtains an iterator, each item of which is a JavaScript object.
347
348**System capability**: SystemCapability.Utils.Lang
349
350**Return value**
351
352| Type| Description|
353| -------- | -------- |
354| IterableIterator<T> | Iterator obtained.|
355
356**Error codes**
357
358For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
359
360| ID| Error Message|
361| -------- | -------- |
362| 10200011 | The Symbol.iterator method cannot be bound. |
363
364**Example**
365```ts
366let deque = new Deque();
367deque.insertFront(2);
368deque.insertFront(4);
369deque.insertFront(5);
370deque.insertFront(4);
371
372// Method 1:
373for (let item of deque) {
374  console.log("value:" + item);
375}
376
377// Method 2:
378let iter = deque[Symbol.iterator]();
379let temp = iter.next().value;
380while(temp != undefined) {
381  console.log("value:" + temp);
382  temp = iter.next().value;
383}
384```
385