openharmony 鸿蒙 js-apis-queue

2025-06-12 浏览 (1)

@ohos.util.Queue (Linear Container Queue)

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.

This topic uses the following to identify the use of generics:

  • T: Type

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.

Modules to Import

import { Queue } from '@kit.ArkTS';

Queue

Attributes

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

NameTypeReadableWritableDescription
lengthnumberYesNoNumber of elements in a queue (called container later).

constructor

constructor()

A constructor used to create a Queue instance.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Error codes

For details about the error codes, see Utils Error Codes.

IDError Message
10200012The Queue's constructor cannot be directly invoked.

Example

let queue : Queue<number|string|Object> = new Queue();

add

add(element: T): boolean

Adds an element at the end of this container.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
elementTYesTarget element.

Return value

TypeDescription
booleanReturns true if the element is added successfully; returns false otherwise.

Error codes

For details about the error codes, see Utils Error Codes.

IDError Message
10200011The add method cannot be bound.

Example

class C1 {
  name: string = ""
  age: string = ""
}
let queue : Queue<number|string|C1|number[]> = new Queue();
let result = queue.add("a");
let result1 = queue.add(1);
let b = [1, 2, 3];
let result2 = queue.add(b);
let c : C1 = {name : "Dylan", age : "13"};
let result3 = queue.add(c);

pop

pop(): T

Removes the first element from this container.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Return value

TypeDescription
TElement removed.

Error codes

For details about the error codes, see Utils Error Codes.

IDError Message
10200011The pop method cannot be bound.

Example

let queue : Queue<number> = 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.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Return value

TypeDescription
TThe first element obtained.

Error codes

For details about the error codes, see Utils Error Codes.

IDError Message
10200011The getFirst method cannot be bound.

Example

let queue : Queue<number> = 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.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
callbackFnfunctionYesCallback invoked to traverse the elements in the container.
thisArgObjectNoValue of this to use when callbackFn is invoked. The default value is this instance.

callbackFn

NameTypeMandatoryDescription
valueTYesValue of the element that is currently traversed.
indexnumberNoPosition index of the element that is currently traversed. The default value is 0.
QueueQueue<T>NoInstance that calls the forEach API. The default value is this instance.

Error codes

For details about the error codes, see Universal Error Codes and Utils Error Codes.

IDError Message
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.
10200011The forEach method cannot be bound.

Example

let queue : Queue<number> = new Queue();
queue.add(2);
queue.add(4);
queue.add(5);
queue.add(4);
queue.forEach((value : number, index ?: number) : void => {
  console.log("value:" + value, "index:" + index);
});

[Symbol.iterator]

[Symbol.iterator](): IterableIterator<T>

Obtains an iterator, each item of which is a JavaScript object.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Return value

TypeDescription
IterableIterator<T>Iterator obtained.

Error codes

For details about the error codes, see Utils Error Codes.

IDError Message
10200011The Symbol.iterator method cannot be bound.

Example

let queue : Queue<number> = new Queue();
queue.add(2);
queue.add(4);
queue.add(5);
queue.add(4);

// Method 1:
while(queue.length) {
  let item = queue.pop()
  console.log("value:" + item);
}

// Method 2:
let iter = queue[Symbol.iterator]();
let temp: IteratorResult<number> = iter.next().value;
while(temp != undefined) {
  console.log("value:" + temp);
  temp = iter.next().value;
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙ArkTS

harmony 鸿蒙Compilation Toolchain Error Codes

harmony 鸿蒙TypeScript Compiler Error Codes

harmony 鸿蒙Utils Error Codes

harmony 鸿蒙js-apis-arkts-collections

harmony 鸿蒙@arkts.math.Decimal (High-Precision Math Library Decimal)

harmony 鸿蒙@arkts.lang (ArkTS Base Capability)

harmony 鸿蒙@arkts.utils (ArkTS Utils)

harmony 鸿蒙@ohos.util.ArrayList (Linear Container ArrayList)

harmony 鸿蒙@ohos.buffer (Buffer)

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/xxd7Ls