• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.util.Queue (Linear Container Queue)
2
3**Queue** follows the principle of First In First Out (FIFO). It supports insertion of elements at the end and removal from the front of the queue. **Queue** is implemented based on the queue data structure.
4
5Unlike **[Deque](js-apis-deque.md)**, which supports insertion and removal at both the ends, **Queue** supports insertion at one end and removal at the other end.
6
7**Recommended use case**: Use **Queue** in FIFO scenarios.
8
9This topic uses the following to identify the use of generics:
10- T: Type
11
12> **NOTE**
13>
14> 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.
15
16
17## Modules to Import
18
19```ts
20import Queue from '@ohos.util.Queue';
21```
22
23
24## Queue
25
26### Attributes
27
28**System capability**: SystemCapability.Utils.Lang
29
30| Name| Type| Readable| Writable| Description|
31| -------- | -------- | -------- | -------- | -------- |
32| length | number | Yes| No| Number of elements in a queue (called container later).|
33
34
35### constructor
36
37constructor()
38
39A constructor used to create a **Queue** 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 Queue's constructor cannot be directly invoked. |
50
51**Example**
52
53```ts
54let queue : Queue<number | string | Object> = new Queue();
55```
56
57
58### add
59
60add(element: T): boolean
61
62Adds an element at the end of this container.
63
64**System capability**: SystemCapability.Utils.Lang
65
66**Parameters**
67
68| Name| Type| Mandatory| Description|
69| -------- | -------- | -------- | -------- |
70| element | T | Yes| Target element.|
71
72**Return value**
73
74| Type| Description|
75| -------- | -------- |
76| boolean | Returns **true** if the element is added successfully; returns **false** otherwise.|
77
78**Error codes**
79
80For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
81
82| ID| Error Message|
83| -------- | -------- |
84| 10200011 | The add method cannot be bound. |
85
86**Example**
87
88```ts
89class C1 {
90  name: string = ""
91  age: string = ""
92}
93let queue : Queue<number | string | C1 | number[]> = new Queue();
94let result = queue.add("a");
95let result1 = queue.add(1);
96let b = [1, 2, 3];
97let result2 = queue.add(b);
98let c : C1 = {name : "Dylon", age : "13"};
99let result3 = queue.add(c);
100```
101
102### pop
103
104pop(): T
105
106Removes the first element from this container.
107
108**System capability**: SystemCapability.Utils.Lang
109
110**Return value**
111
112| Type| Description|
113| -------- | -------- |
114| T | Element removed.|
115
116**Error codes**
117
118For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
119
120| ID| Error Message|
121| -------- | -------- |
122| 10200011 | The pop method cannot be bound. |
123
124**Example**
125
126```ts
127let queue : Queue<number> = new Queue();
128queue.add(2);
129queue.add(4);
130queue.add(5);
131queue.add(2);
132queue.add(4);
133let result = queue.pop();
134```
135
136### getFirst
137
138getFirst(): T
139
140Obtains the first element of this container.
141
142**System capability**: SystemCapability.Utils.Lang
143
144**Return value**
145
146| Type| Description|
147| -------- | -------- |
148| T | The first element obtained.|
149
150**Error codes**
151
152For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
153
154| ID| Error Message|
155| -------- | -------- |
156| 10200011 | The getFirst method cannot be bound. |
157
158**Example**
159
160```ts
161let queue : Queue<number> = new Queue();
162queue.add(2);
163queue.add(4);
164queue.add(5);
165queue.add(2);
166let result = queue.getFirst();
167```
168
169### forEach
170
171forEach(callbackFn: (value: T, index?: number, Queue?: Queue&lt;T&gt;) => void,
172thisArg?: Object): void
173
174Uses a callback to traverse the elements in this container and obtain their position indexes.
175
176**System capability**: SystemCapability.Utils.Lang
177
178**Parameters**
179
180| Name| Type| Mandatory| Description|
181| -------- | -------- | -------- | -------- |
182| callbackFn | function | Yes| Callback invoked to traverse the elements in the container.|
183| thisArg | Object | No| Value of **this** to use when **callbackFn** is invoked. The default value is this instance.|
184
185callbackFn
186
187| Name| Type| Mandatory| Description|
188| -------- | -------- | -------- | -------- |
189| value | T | Yes| Value of the element that is currently traversed.|
190| index | number | No| Position index of the element that is currently traversed. The default value is **0**.|
191| Queue | Queue&lt;T&gt; | No| Instance that calls the **forEach** API. The default value is this instance.|
192
193**Error codes**
194
195For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
196
197| ID| Error Message|
198| -------- | -------- |
199| 10200011 | The forEach method cannot be bound. |
200
201**Example**
202
203```ts
204let queue : Queue<number> = new Queue();
205queue.add(2);
206queue.add(4);
207queue.add(5);
208queue.add(4);
209queue.forEach((value : number, index ?: number) : void => {
210  console.log("value:" + value, "index:" + index);
211});
212```
213
214### [Symbol.iterator]
215
216[Symbol.iterator]\(): IterableIterator&lt;T&gt;
217
218Obtains an iterator, each item of which is a JavaScript object.
219
220> **NOTE**
221>
222> This API cannot be used in .ets files.
223
224**System capability**: SystemCapability.Utils.Lang
225
226**Return value**
227
228| Type| Description|
229| -------- | -------- |
230| IterableIterator&lt;T&gt; | Iterator obtained.|
231
232**Error codes**
233
234For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
235
236| ID| Error Message|
237| -------- | -------- |
238| 10200011 | The Symbol.iterator method cannot be bound. |
239
240**Example**
241```ts
242let queue : Queue<number> = new Queue();
243queue.add(2);
244queue.add(4);
245queue.add(5);
246queue.add(4);
247
248// Method 1:
249while(queue.length) {
250  let item = queue.pop()
251  console.log("value:" + item);
252}
253
254// Method 2:
255let iter = queue[Symbol.iterator]();
256let temp: IteratorResult<number> = iter.next().value;
257while(temp != undefined) {
258  console.log("value:" + temp);
259  temp = iter.next().value;
260}
261```
262