• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Linear Container Queue
2
3> **NOTE**
4>
5> 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.
6
7**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.
8
9Unlike **[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.
10
11**Recommended use case**: Use **Queue** in FIFO scenarios.
12
13## Modules to Import
14
15```ts
16import Queue from '@ohos.util.Queue';
17```
18
19
20## Queue
21
22### Attributes
23
24**System capability**: SystemCapability.Utils.Lang
25
26| Name| Type| Readable| Writable| Description|
27| -------- | -------- | -------- | -------- | -------- |
28| length | number | Yes| No| Number of elements in a queue (called container later).|
29
30
31### constructor
32
33constructor()
34
35A constructor used to create a **Queue** instance.
36
37**System capability**: SystemCapability.Utils.Lang
38
39**Example**
40
41```ts
42let queue = new Queue();
43```
44
45
46### add
47
48add(element: T): boolean
49
50Adds an element at the end of this container.
51
52**System capability**: SystemCapability.Utils.Lang
53
54**Parameters**
55
56| Name| Type| Mandatory| Description|
57| -------- | -------- | -------- | -------- |
58| element | T | Yes| Target element.|
59
60**Return value**
61
62| Type| Description|
63| -------- | -------- |
64| boolean | Returns **true** if the element is added successfully; returns **false** otherwise.|
65
66**Example**
67
68```ts
69let queue = new Queue();
70let result = queue.add("a");
71let result1 = queue.add(1);
72queue.add(1);
73let b = [1, 2, 3];
74queue.add(b);
75let c = {name : "lala", age : "13"};
76let result3 = queue.add(c);
77```
78
79### pop
80
81pop(): T
82
83Removes the first element from this container.
84
85**System capability**: SystemCapability.Utils.Lang
86
87**Return value**
88
89| Type| Description|
90| -------- | -------- |
91| T | Element removed.|
92
93**Example**
94
95```ts
96let queue = new Queue();
97queue.add(2);
98queue.add(4);
99queue.add(5);
100queue.add(2);
101queue.add(4);
102let result = queue.pop();
103```
104
105### getFirst
106
107getFirst(): T
108
109Obtains the first element of this container.
110
111**System capability**: SystemCapability.Utils.Lang
112
113**Parameters**
114
115| Type| Description|
116| -------- | -------- |
117| T | The first element obtained.|
118
119**Example**
120
121```ts
122let queue = new Queue();
123queue.add(2);
124queue.add(4);
125queue.add(5);
126queue.add(2);
127let result = queue.getFirst();
128```
129
130### forEach
131
132forEach(callbackfn: (value: T, index?: number, Queue?: Queue<T>) => void,
133thisArg?: Object): void
134
135Uses a callback to traverse the elements in this container and obtain their position indexes.
136
137**System capability**: SystemCapability.Utils.Lang
138
139**Parameters**
140
141| Name| Type| Mandatory| Description|
142| -------- | -------- | -------- | -------- |
143| callbackfn | function | Yes| Callback invoked to traverse the elements in the container.|
144| thisArg | Object | No| Value to use when the callback is invoked.|
145
146callbackfn
147
148| Name| Type| Mandatory| Description|
149| -------- | -------- | -------- | -------- |
150| value | T | Yes| Value of the element that is currently traversed.|
151| index | number | No| Position index of the element that is currently traversed.|
152| Queue | Queue<T> | No| Instance that invokes the **forEach** method.|
153
154**Example**
155
156```ts
157let queue = new Queue();
158queue.add(2);
159queue.add(4);
160queue.add(5);
161queue.add(4);
162queue.forEach((value, index) => {
163    console.log("value:" + value, "index:" + index);
164});
165
166```
167
168### [Symbol.iterator]
169
170[Symbol.iterator]\(): IterableIterator<T>
171
172Obtains an iterator, each item of which is a JavaScript object.
173
174**System capability**: SystemCapability.Utils.Lang
175
176**Return value**
177
178| Type| Description|
179| -------- | -------- |
180| IterableIterator<T> | Iterator obtained.|
181
182**Example**
183```ts
184let queue = new Queue();
185queue.add(2);
186queue.add(4);
187queue.add(5);
188queue.add(4);
189
190// Method 1:
191for (let item of queue) {
192  console.log("value:" + item);
193}
194
195// Method 2:
196let iter = queue[Symbol.iterator]();
197let temp = iter.next().value;
198while(temp != undefined) {
199  console.log("value:" + temp);
200  temp = iter.next().value;
201}
202```
203