Linear Container Queue
NOTE
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.
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.
Unlike Deque, which supports insertion and removal at both the ends, Queue supports insertion at one end and removal at the other end.
Recommended use case: Use Queue in FIFO scenarios.
Modules to Import
import Queue from '@ohos.util.Queue';
Queue
Attributes
System capability: SystemCapability.Utils.Lang
Name | Type | Readable | Writable | Description |
---|---|---|---|---|
length | number | Yes | No | Number of elements in a queue (called container later). |
constructor
constructor()
A constructor used to create a Queue instance.
System capability: SystemCapability.Utils.Lang
Example
let queue = new Queue();
add
add(element: T): boolean
Adds an element at the end of this container.
System capability: SystemCapability.Utils.Lang
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
element | T | Yes | Target element. |
Return value
Type | Description |
---|---|
boolean | Returns true if the element is added successfully; returns false otherwise. |
Example
let queue = new Queue();
let result = queue.add("a");
let result1 = queue.add(1);
queue.add(1);
let b = [1, 2, 3];
queue.add(b);
let c = {name : "lala", age : "13"};
let result3 = queue.add(c);
pop
pop(): T
Removes the first element from this container.
System capability: SystemCapability.Utils.Lang
Return value
Type | Description |
---|---|
T | Element removed. |
Example
let queue = new Queue();
queue.add(2);
queue.add(4);
queue.add(5);
queue.add(2);
queue.add(4);
let result = queue.pop();
getFirst
getFirst(): T
Obtains the first element of this container.
System capability: SystemCapability.Utils.Lang
Parameters
Type | Description |
---|---|
T | The first element obtained. |
Example
let queue = new Queue();
queue.add(2);
queue.add(4);
queue.add(5);
queue.add(2);
let result = queue.getFirst();
forEach
forEach(callbackfn: (value: T, index?: number, Queue?: Queue<T>) => void, thisArg?: Object): void
Uses a callback to traverse the elements in this container and obtain their position indexes.
System capability: SystemCapability.Utils.Lang
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
callbackfn | function | Yes | Callback invoked to traverse the elements in the container. |
thisArg | Object | No | Value to use when the callback is invoked. |
callbackfn
Name | Type | Mandatory | Description |
---|---|---|---|
value | T | Yes | Value of the element that is currently traversed. |
index | number | No | Position index of the element that is currently traversed. |
Queue | Queue<T> | No | Instance that invokes the forEach method. |
Example
let queue = new Queue();
queue.add(2);
queue.add(4);
queue.add(5);
queue.add(4);
queue.forEach((value, index) => {
console.log("value:" + value, index);
});
[Symbol.iterator]
[Symbol.iterator](): IterableIterator<T>
Obtains an iterator, each item of which is a JavaScript object.
System capability: SystemCapability.Utils.Lang
Return value
Type | Description |
---|---|
IterableIterator<T> | Iterator obtained. |
Example
let queue = new Queue();
queue.add(2);
queue.add(4);
queue.add(5);
queue.add(4);
// Method 1:
for (let item of queue) {
console.log("value:" + item);
}
// Method 2:
let iter = queue[Symbol.iterator]();
let temp = iter.next().value;
while(temp != undefined) {
console.log("value:" + temp);
temp = iter.next().value;
}