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