• 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 = 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
89let queue = new Queue();
90let result = queue.add("a");
91let result1 = queue.add(1);
92let b = [1, 2, 3];
93let result2 = queue.add(b);
94let c = {name : "Dylon", age : "13"};
95let result3 = queue.add(c);
96```
97
98### pop
99
100pop(): T
101
102Removes the first element from this container.
103
104**System capability**: SystemCapability.Utils.Lang
105
106**Return value**
107
108| Type| Description|
109| -------- | -------- |
110| T | Element removed.|
111
112**Error codes**
113
114For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
115
116| ID| Error Message|
117| -------- | -------- |
118| 10200011 | The pop method cannot be bound. |
119
120**Example**
121
122```ts
123let queue = new Queue();
124queue.add(2);
125queue.add(4);
126queue.add(5);
127queue.add(2);
128queue.add(4);
129let result = queue.pop();
130```
131
132### getFirst
133
134getFirst(): T
135
136Obtains the first element of this container.
137
138**System capability**: SystemCapability.Utils.Lang
139
140**Return value**
141
142| Type| Description|
143| -------- | -------- |
144| T | The first element obtained.|
145
146**Error codes**
147
148For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
149
150| ID| Error Message|
151| -------- | -------- |
152| 10200011 | The getFirst method cannot be bound. |
153
154**Example**
155
156```ts
157let queue = new Queue();
158queue.add(2);
159queue.add(4);
160queue.add(5);
161queue.add(2);
162let result = queue.getFirst();
163```
164
165### forEach
166
167forEach(callbackFn: (value: T, index?: number, Queue?: Queue<T>) => void,
168thisArg?: Object): void
169
170Uses a callback to traverse the elements in this container and obtain their position indexes.
171
172**System capability**: SystemCapability.Utils.Lang
173
174**Parameters**
175
176| Name| Type| Mandatory| Description|
177| -------- | -------- | -------- | -------- |
178| callbackFn | function | Yes| Callback invoked to traverse the elements in the container.|
179| thisArg | Object | No| Value to use when the callback is invoked.|
180
181callbackfn
182
183| Name| Type| Mandatory| Description|
184| -------- | -------- | -------- | -------- |
185| value | T | Yes| Value of the element that is currently traversed.|
186| index | number | No| Position index of the element that is currently traversed.|
187| Queue | Queue<T> | No| Instance that invokes the **forEach** method.|
188
189**Error codes**
190
191For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
192
193| ID| Error Message|
194| -------- | -------- |
195| 10200011 | The forEach method cannot be bound. |
196
197**Example**
198
199```ts
200let queue = new Queue();
201queue.add(2);
202queue.add(4);
203queue.add(5);
204queue.add(4);
205queue.forEach((value, index) => {
206    console.log("value:" + value, "index:" + index);
207});
208```
209
210### [Symbol.iterator]
211
212[Symbol.iterator]\(): IterableIterator<T>
213
214Obtains an iterator, each item of which is a JavaScript object.
215
216**System capability**: SystemCapability.Utils.Lang
217
218**Return value**
219
220| Type| Description|
221| -------- | -------- |
222| IterableIterator<T> | Iterator obtained.|
223
224**Error codes**
225
226For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
227
228| ID| Error Message|
229| -------- | -------- |
230| 10200011 | The Symbol.iterator method cannot be bound. |
231
232**Example**
233```ts
234let queue = new Queue();
235queue.add(2);
236queue.add(4);
237queue.add(5);
238queue.add(4);
239
240// Method 1:
241for (let item of queue) {
242  console.log("value:" + item);
243}
244
245// Method 2:
246let iter = queue[Symbol.iterator]();
247let temp = iter.next().value;
248while(temp != undefined) {
249  console.log("value:" + temp);
250  temp = iter.next().value;
251}
252```
253