# @ohos.util.Deque (Linear Container Deque) Double-Ended Queue (Deque) is a data structure implemented based on a circular queue. It supports insertion and deletion of elements at both ends. It follows the principles of First In First Out (FIFO) and Last In First Out (LIFO). Deque can dynamically adjust the capacity based on project requirements. It doubles the capacity each time. Queue allows element removal at the front and insertion at the rear. Compared with [Queue](js-apis-queue.md), which only allows element deletion at the front and insertion at the back, Deque permits insertion and deletion at both ends. Both [Vector](js-apis-vector.md) and Deque support insertion and deletion at the ends, but Deque does not support insertion in the middle. Deque is more efficient than a Vector for inserting and deleting elements at the front, whereas a Vector excels in element access efficiency. **Recommended use case**: Use **Deque** when you need to frequently insert or remove elements at both the ends of a container. 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 ```ts import { Deque } from '@kit.ArkTS'; ``` ## Deque ### Properties **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.Utils.Lang | Name| Type| Read-Only| Optional| Description| | -------- | -------- | -------- | -------- | -------- | | length | number | Yes| No| Number of elements in a Deque.| ### constructor constructor() A constructor used to create a **Deque** 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](errorcode-utils.md). | ID| Error Message| | -------- | -------- | | 10200012 | The Deque's constructor cannot be directly invoked. | **Example** ```ts let deque: Deque = new Deque(); ``` ### insertFront insertFront(element: T): void Inserts an element at the front of this Deque. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | element | T | Yes| Target element.| **Error codes** For details about the error codes, see [Utils Error Codes](errorcode-utils.md). | ID| Error Message| | -------- | -------- | | 10200011 | The insertFront method cannot be bound. | **Example** ```ts class C1 { name: string = "" age: string = "" } let deque: Deque | C1> = new Deque(); deque.insertFront("a"); deque.insertFront(1); let b = [1, 2, 3]; deque.insertFront(b); let c: C1 = {name : "Dylan", age : "13"}; deque.insertFront(c); deque.insertFront(false); ``` ### insertEnd insertEnd(element: T): void Inserts an element at the end of this Deque. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | element | T | Yes| Target element.| **Error codes** For details about the error codes, see [Utils Error Codes](errorcode-utils.md). | ID| Error Message| | -------- | -------- | | 10200011 | The insertEnd method cannot be bound. | **Example** ```ts class C1 { name: string = "" age: string = "" } let deque: Deque | C1> = new Deque(); deque.insertEnd("a"); deque.insertEnd(1); let b = [1, 2, 3]; deque.insertEnd(b); let c: C1 = {name : "Dylan", age : "13"}; deque.insertEnd(c); deque.insertEnd(false); ``` ### has has(element: T): boolean Checks whether this Deque has the specified element. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | element | T | Yes| Target element.| **Return value** | Type| Description| | -------- | -------- | | boolean | Operation result. The value **true** is returned if the specified element is contained; otherwise, **false** is returned.| **Error codes** For details about the error codes, see [Utils Error Codes](errorcode-utils.md). | ID| Error Message| | -------- | -------- | | 10200011 | The has method cannot be bound. | **Example** ```ts let deque: Deque = new Deque(); deque.insertFront("squirrel"); let result = deque.has("squirrel"); ``` ### popFirst popFirst(): T Removes the first element of this Deque. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.Utils.Lang **Return value** | Type| Description| | -------- | -------- | | T | First element removed.| **Error codes** For details about the error codes, see [Utils Error Codes](errorcode-utils.md). | ID| Error Message| | -------- | -------- | | 10200011 | The popFirst method cannot be bound. | **Example** ```ts let deque: Deque = new Deque(); deque.insertFront(2); deque.insertFront(4); deque.insertEnd(5); deque.insertFront(2); deque.insertFront(4); let result = deque.popFirst(); ``` ### popLast popLast(): T Removes the last element of this Deque. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.Utils.Lang **Return value** | Type| Description| | -------- | -------- | | T | Last element removed.| **Error codes** For details about the error codes, see [Utils Error Codes](errorcode-utils.md). | ID| Error Message| | -------- | -------- | | 10200011 | The popLast method cannot be bound. | **Example** ```ts let deque: Deque = new Deque(); deque.insertFront(2); deque.insertEnd(4); deque.insertFront(5); deque.insertFront(2); deque.insertFront(4); let result = deque.popLast(); ``` ### forEach forEach(callbackFn: (value: T, index?: number, deque?: Deque<T>) => void, thisArg?: Object): void Uses a callback to traverse the elements in this Deque and obtain their indexes. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | callbackFn | function | Yes| Callback invoked to traverse the elements in the Deque.| | thisArg | Object | No| Value of **this** to use when **callbackFn** is invoked. The default value is this instance.| callbackFn parameters | 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. The default value is **0**.| | deque | Deque<T> | No| Instance that calls the **forEach** API. The default value is this instance.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). | ID| Error Message| | -------- | -------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | | 10200011 | The forEach method cannot be bound. | **Example** ```ts let deque: Deque = new Deque(); deque.insertFront(2); deque.insertEnd(4); deque.insertFront(5); deque.insertEnd(4); deque.forEach((value: number, index?: number | undefined, deque?: Deque | undefined):void => { console.log("value:" + value, "index:" + index); }); ``` ### getFirst getFirst(): T Obtains the first element of this Deque. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.Utils.Lang **Return value** | Type| Description| | -------- | -------- | | T | First element of the T type obtained.| **Error codes** For details about the error codes, see [Utils Error Codes](errorcode-utils.md). | ID| Error Message| | -------- | -------- | | 10200011 | The getFirst method cannot be bound. | **Example** ```ts let deque: Deque = new Deque(); deque.insertEnd(2); deque.insertEnd(4); deque.insertFront(5); deque.insertFront(4); let result = deque.getFirst(); ``` ### getLast getLast(): T Obtains the last element of this Deque. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.Utils.Lang **Return value** | Type| Description| | -------- | -------- | | T | Last element of the T type obtained.| **Error codes** For details about the error codes, see [Utils Error Codes](errorcode-utils.md). | ID| Error Message| | -------- | -------- | | 10200011 | The getLast method cannot be bound. | **Example** ```ts let deque: Deque = new Deque(); deque.insertFront(2); deque.insertFront(4); deque.insertFront(5); deque.insertFront(4); let result = deque.getLast(); ``` ### [Symbol.iterator] [Symbol.iterator]\(): IterableIterator<T> Returns 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** | Type| Description| | -------- | -------- | | IterableIterator<T> | Iterator obtained.| **Error codes** For details about the error codes, see [Utils Error Codes](errorcode-utils.md). | ID| Error Message| | -------- | -------- | | 10200011 | The Symbol.iterator method cannot be bound. | **Example** ```ts let deque: Deque = new Deque(); deque.insertFront(2); deque.insertFront(4); deque.insertFront(5); deque.insertFront(4); // Method 1: let nums: Array = Array.from(deque); for (let item of nums) { console.log("value:" + item); } // Method 2: let iter = deque[Symbol.iterator](); let temp:IteratorResult = iter.next(); while(!temp.done) { console.log("value:" + temp.value); temp = iter.next(); } ```