未验证 提交 98fc7a57 编写于 作者: O openharmony_ci 提交者: Gitee

!5789 翻译完成:4270 Add the description of the characteristics of the container class

Merge pull request !5789 from wusongqing/TR4270
# Linear Container ArrayList # Linear Container ArrayList
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br/> > **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. > 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.
**ArrayList** is a linear data structure that is implemented based on arrays. **ArrayList** can dynamically adjust the capacity based on project requirements. It increases the capacity by 50% each time.
Similar to **ArrayList**, **[Vector](js-apis-vector.md)** is also implemented based on arrays and can dynamically adjust the capacity. It increases the capability by 100% each time.
When compared with **[LinkedList](js-apis-linkedlist.md)**, **ArrayList** is more efficient in random access but less efficient in the addition or removal operation, because its addition or removal operation affects the position of other elements in the container.
**Recommended use case**: Use **ArrayList** when elements in a container need to be frequently read.
## Modules to Import ## Modules to Import
```ts ```ts
import ArrayList from '@ohos.util.ArrayList' import ArrayList from '@ohos.util.ArrayList';
``` ```
## System Capabilities
SystemCapability.Utils.Lang
## ArrayList ## ArrayList
### Attributes ### Attributes
**System capability**: SystemCapability.Utils.Lang
| Name| Type| Readable| Writable| Description| | Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- | -------- |
| length | number | Yes| No| Number of entries in an array list (called container later).| | length | number | Yes| No| Number of elements in an array list (called container later).|
### constructor ### constructor
...@@ -28,6 +35,8 @@ constructor() ...@@ -28,6 +35,8 @@ constructor()
A constructor used to create an **ArrayList** instance. A constructor used to create an **ArrayList** instance.
**System capability**: SystemCapability.Utils.Lang
**Example** **Example**
```ts ```ts
...@@ -39,19 +48,21 @@ let arrayList = new ArrayList(); ...@@ -39,19 +48,21 @@ let arrayList = new ArrayList();
add(element: T): boolean add(element: T): boolean
Adds an entry at the end of this container. Adds an element at the end of this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to add.| | element | T | Yes| Target element.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| boolean | Returns **true** if the entry is added successfully; returns **false** otherwise.| | boolean | Returns **true** if the element is added successfully; returns **false** otherwise.|
**Example** **Example**
...@@ -69,14 +80,16 @@ Adds an entry at the end of this container. ...@@ -69,14 +80,16 @@ Adds an entry at the end of this container.
insert(element: T, index: number): void insert(element: T, index: number): void
Inserts an entry at the specified position in this container. Inserts an element at the specified position in this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to insert.| | element | T | Yes| Target element.|
| index | number | Yes| Index of the position where the entry is to be inserted.| | index | number | Yes| Index of the position where the element is to be inserted.|
**Example** **Example**
...@@ -91,19 +104,21 @@ arrayList.insert(true, 2); ...@@ -91,19 +104,21 @@ arrayList.insert(true, 2);
has(element: T): boolean has(element: T): boolean
Checks whether this container has the specified entry. Checks whether this container has the specified element.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to check.| | element | T | Yes| Target element.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| boolean | Returns **true** if the specified entry is contained; returns **false** otherwise.| | boolean | Returns **true** if the specified element is contained; returns **false** otherwise.|
**Example** **Example**
...@@ -118,19 +133,21 @@ let result1 = arrayList.has("Ahfbrgrbgnutfodgorrogorgrogofdfdf"); ...@@ -118,19 +133,21 @@ let result1 = arrayList.has("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
getIndexOf(element: T): number getIndexOf(element: T): number
Obtains the index of the first occurrence of the specified entry in this container. Obtains the index of the first occurrence of the specified element in this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to query.| | element | T | Yes| Target element.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| number | Returns the position index if obtained; returns **-1** if the specified entry is not found.| | number | Returns the position index if obtained; returns **-1** if the specified element is not found.|
**Example** **Example**
...@@ -150,19 +167,21 @@ let result = arrayList.getIndexOf(2); ...@@ -150,19 +167,21 @@ let result = arrayList.getIndexOf(2);
getLastIndexOf(element: T): number getLastIndexOf(element: T): number
Obtains the index of the last occurrence of the specified entry in this container. Obtains the index of the last occurrence of the specified element in this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to query.| | element | T | Yes| Target element.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| number | Returns the position index if obtained; returns **-1** if the specified entry is not found.| | number | Returns the position index if obtained; returns **-1** if the specified element is not found.|
**Example** **Example**
...@@ -182,19 +201,21 @@ let result = arrayList.getLastIndexOf(2); ...@@ -182,19 +201,21 @@ let result = arrayList.getLastIndexOf(2);
removeByIndex(index: number): T removeByIndex(index: number): T
Removes an entry with the specified position from this container. Removes an element with the specified position from this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| index | number | Yes| Position index of the entry to remove.| | index | number | Yes| Position index of the target element.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| T | Entry removed.| | T | Element removed.|
**Example** **Example**
...@@ -212,19 +233,21 @@ let result = arrayList.removeByIndex(2); ...@@ -212,19 +233,21 @@ let result = arrayList.removeByIndex(2);
remove(element: T): boolean remove(element: T): boolean
Removes the first occurrence of the specified entry from this container. Removes the first occurrence of the specified element from this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to remove.| | element | T | Yes| Target element.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| boolean | Returns **true** if the entry is removed successfully; returns **false** otherwise.| | boolean | Returns **true** if the element is removed successfully; returns **false** otherwise.|
**Example** **Example**
...@@ -241,7 +264,9 @@ let result = arrayList.remove(2); ...@@ -241,7 +264,9 @@ let result = arrayList.remove(2);
removeByRange(fromIndex: number, toIndex: number): void removeByRange(fromIndex: number, toIndex: number): void
Removes from this container all of the entries within a range, including the entry at the start position but not that at the end position. Removes from this container all of the elements within a range, including the element at the start position but not that at the end position.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
...@@ -268,7 +293,9 @@ arrayList.removeByRange(2, 6); ...@@ -268,7 +293,9 @@ arrayList.removeByRange(2, 6);
replaceAllElements(callbackfn: (value: T, index?: number, arrlist?: ArrayList&lt;T&gt;) => T, replaceAllElements(callbackfn: (value: T, index?: number, arrlist?: ArrayList&lt;T&gt;) => T,
thisArg?: Object): void thisArg?: Object): void
Replaces all entries in this container with new entries, and returns the new ones. Replaces all elements in this container with new elements, and returns the new ones.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
...@@ -281,8 +308,8 @@ callbackfn ...@@ -281,8 +308,8 @@ callbackfn
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| value | T | Yes| Value of the entry that is currently traversed.| | value | T | Yes| Value of the element that is currently traversed.|
| index | number | No| Position index of the entry that is currently traversed.| | index | number | No| Position index of the element that is currently traversed.|
| arrlist | ArrayList&lt;T&gt; | No| Instance that invokes the **replaceAllElements** method.| | arrlist | ArrayList&lt;T&gt; | No| Instance that invokes the **replaceAllElements** method.|
**Example** **Example**
...@@ -306,21 +333,23 @@ arrayList.replaceAllElements((value, index) => { ...@@ -306,21 +333,23 @@ arrayList.replaceAllElements((value, index) => {
forEach(callbackfn: (value: T, index?: number, arrlist?: ArrayList&lt;T&gt;) => void, forEach(callbackfn: (value: T, index?: number, arrlist?: ArrayList&lt;T&gt;) => void,
thisArg?: Object): void thisArg?: Object): void
Uses a callback to traverse the entries in this container and obtain their position indexes. Uses a callback to traverse the elements in this container and obtain their position indexes.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| callbackfn | function | Yes| Callback invoked to traverse the entries in the container.| | callbackfn | function | Yes| Callback invoked to traverse the elements in the container.|
| thisArg | Object | No| Value to use when the callback is invoked.| | thisArg | Object | No| Value to use when the callback is invoked.|
callbackfn callbackfn
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| value | T | Yes| Value of the entry that is currently traversed.| | value | T | Yes| Value of the element that is currently traversed.|
| index | number | No| Position index of the entry that is currently traversed.| | index | number | No| Position index of the element that is currently traversed.|
| arrlist | ArrayList&lt;T&gt; | No| Instance that invokes the **forEach** method.| | arrlist | ArrayList&lt;T&gt; | No| Instance that invokes the **forEach** method.|
**Example** **Example**
...@@ -332,7 +361,7 @@ arrayList.add(4); ...@@ -332,7 +361,7 @@ arrayList.add(4);
arrayList.add(5); arrayList.add(5);
arrayList.add(4); arrayList.add(4);
arrayList.forEach((value, index) => { arrayList.forEach((value, index) => {
console.log(value, index); console.log("value:" + value, index);
}); });
``` ```
...@@ -340,7 +369,9 @@ arrayList.forEach((value, index) => { ...@@ -340,7 +369,9 @@ arrayList.forEach((value, index) => {
sort(comparator?: (firstValue: T, secondValue: T) => number): void sort(comparator?: (firstValue: T, secondValue: T) => number): void
Sorts entries in this container. Sorts elements in this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
...@@ -352,8 +383,8 @@ comparator ...@@ -352,8 +383,8 @@ comparator
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| firstValue | T | Yes| Previous entry.| | firstValue | T | Yes| Previous element.|
| secondValue | T | Yes| Next entry.| | secondValue | T | Yes| Next element.|
**Example** **Example**
...@@ -363,8 +394,8 @@ arrayList.add(2); ...@@ -363,8 +394,8 @@ arrayList.add(2);
arrayList.add(4); arrayList.add(4);
arrayList.add(5); arrayList.add(5);
arrayList.add(4); arrayList.add(4);
arrayList.sort(a, (b => a - b)); arrayList.sort((a, b) => a - b);
arrayList.sort(a, (b => b - a)); arrayList.sort((a, b) => b - a);
arrayList.sort(); arrayList.sort();
``` ```
...@@ -372,7 +403,9 @@ arrayList.sort(); ...@@ -372,7 +403,9 @@ arrayList.sort();
subArrayList(fromIndex: number, toIndex: number): ArrayList&lt;T&gt; subArrayList(fromIndex: number, toIndex: number): ArrayList&lt;T&gt;
Obtains entries within a range in this container, including the entry at the start position but not that at the end position, and returns these entries as a new **ArrayList** instance. Obtains elements within a range in this container, including the element at the start position but not that at the end position, and returns these elements as a new **ArrayList** instance.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
...@@ -406,6 +439,8 @@ clear(): void ...@@ -406,6 +439,8 @@ clear(): void
Clears this container and sets its length to **0**. Clears this container and sets its length to **0**.
**System capability**: SystemCapability.Utils.Lang
**Example** **Example**
```ts ```ts
...@@ -423,6 +458,8 @@ clone(): ArrayList&lt;T&gt; ...@@ -423,6 +458,8 @@ clone(): ArrayList&lt;T&gt;
Clones this container and returns a copy. The modification to the copy does not affect the original instance. Clones this container and returns a copy. The modification to the copy does not affect the original instance.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
...@@ -447,6 +484,8 @@ getCapacity(): number ...@@ -447,6 +484,8 @@ getCapacity(): number
Obtains the capacity of this container. Obtains the capacity of this container.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
| Type| Description| | Type| Description|
...@@ -470,6 +509,8 @@ convertToArray(): Array&lt;T&gt; ...@@ -470,6 +509,8 @@ convertToArray(): Array&lt;T&gt;
Converts this container into an array. Converts this container into an array.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
| Type| Description| | Type| Description|
...@@ -491,7 +532,9 @@ let result = arrayList.convertToArray(); ...@@ -491,7 +532,9 @@ let result = arrayList.convertToArray();
isEmpty(): boolean isEmpty(): boolean
Checks whether this container is empty (contains no entry). Checks whether this container is empty (contains no element).
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
...@@ -516,6 +559,8 @@ increaseCapacityTo(newCapacity: number): void ...@@ -516,6 +559,8 @@ increaseCapacityTo(newCapacity: number): void
Increases the capacity of this container. Increases the capacity of this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
...@@ -540,6 +585,8 @@ trimToCurrentLength(): void ...@@ -540,6 +585,8 @@ trimToCurrentLength(): void
Trims the capacity of this container to its current length. Trims the capacity of this container to its current length.
**System capability**: SystemCapability.Utils.Lang
**Example** **Example**
```ts ```ts
...@@ -557,6 +604,8 @@ arrayList.trimToCurrentLength(); ...@@ -557,6 +604,8 @@ arrayList.trimToCurrentLength();
Obtains an iterator, each item of which is a JavaScript object. Obtains an iterator, each item of which is a JavaScript object.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
| Type| Description| | Type| Description|
...@@ -574,14 +623,14 @@ arrayList.add(4); ...@@ -574,14 +623,14 @@ arrayList.add(4);
// Method 1: // Method 1:
for (let item of arrayList) { for (let item of arrayList) {
console.log(item); console.log("value:" + item);
} }
// Method 2: // Method 2:
let iter = arrayList[Symbol.iterator](); let iter = arrayList[Symbol.iterator]();
let temp = iter.next().value; let temp = iter.next().value;
while(temp != undefined) { while(temp != undefined) {
console.log(temp); console.log("value:" + temp);
temp = iter.next().value; temp = iter.next().value;
} }
``` ```
# Linear Container Deque # Linear Container Deque
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br/> > **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. > 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.
Double-ended queue (deque) is a sequence container implemented based on the queue data structure that follows the principles of First In First Out (FIFO) and Last In First Out (LIFO). It allows insertion and removal of elements at both the ends. **Deque** can dynamically adjust the capacity based on project requirements. It doubles the capacity each time. **Deque** differs from **[Queue](js-apis-queue.md)** and **[Vector](js-apis-vector.md)** mainly in the following aspects:
**Queue** follows the principle of FIFO only and allows element removal at the front and insertion at the rear.
**Vector** supports insertion and deletion of elements in between, as well asat both the ends. When compared with **Vector**, **Deque** is more efficient in inserting and removing header elements, but less efficient in accessing elements.
**Recommended use case**: Use **Deque** when you need to frequently insert or remove elements at both the ends of a container.
## Modules to Import ## Modules to Import
```ts ```ts
import Deque from '@ohos.util.Deque' import Deque from '@ohos.util.Deque';
``` ```
## System Capabilities
SystemCapability.Utils.Lang
## Deque ## Deque
### Attributes ### Attributes
**System capability**: SystemCapability.Utils.Lang
| Name| Type| Readable| Writable| Description| | Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- | -------- |
| length | number | Yes| No| Number of entries in a double-ended queue (deque, called container later).| | length | number | Yes| No| Number of elements in a deque (called container later).|
### constructor ### constructor
...@@ -28,6 +34,8 @@ constructor() ...@@ -28,6 +34,8 @@ constructor()
A constructor used to create a **Deque** instance. A constructor used to create a **Deque** instance.
**System capability**: SystemCapability.Utils.Lang
**Example** **Example**
```ts ```ts
...@@ -38,13 +46,15 @@ let deque = new Deque(); ...@@ -38,13 +46,15 @@ let deque = new Deque();
insertFront(element: T): void insertFront(element: T): void
Inserts an entry at the front of this container. Inserts an element at the front of this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to insert.| | element | T | Yes| Target element.|
**Example** **Example**
...@@ -62,13 +72,15 @@ deque.insertFront(false); ...@@ -62,13 +72,15 @@ deque.insertFront(false);
insertEnd(element: T): void insertEnd(element: T): void
Inserts an entry at the end of this container. Inserts an element at the end of this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to insert.| | element | T | Yes| Target element.|
**Example** **Example**
...@@ -86,19 +98,21 @@ deque.insertEnd(false); ...@@ -86,19 +98,21 @@ deque.insertEnd(false);
has(element: T): boolean has(element: T): boolean
Checks whether this container has the specified entry. Checks whether this container has the specified element.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to check.| | element | T | Yes| Target element.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| boolean | Returns **true** if the specified entry is contained; returns **false** otherwise.| | boolean | Returns **true** if the specified element is contained; returns **false** otherwise.|
**Example** **Example**
...@@ -113,13 +127,15 @@ let result1 = deque.has("Ahfbrgrbgnutfodgorrogorg"); ...@@ -113,13 +127,15 @@ let result1 = deque.has("Ahfbrgrbgnutfodgorrogorg");
popFirst(): T popFirst(): T
Removes the first entry of this container. Removes the first element of this container.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| T | Entry removed.| | T | Element removed.|
**Example** **Example**
...@@ -137,13 +153,15 @@ let result = deque.popFirst(); ...@@ -137,13 +153,15 @@ let result = deque.popFirst();
popLast(): T popLast(): T
Removes the last entry of this container. Removes the last element of this container.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| T | Entry removed.| | T | Element removed.|
**Example** **Example**
...@@ -162,13 +180,15 @@ let result = deque.popLast(); ...@@ -162,13 +180,15 @@ let result = deque.popLast();
forEach(callbackfn: (value: T, index?: number, deque?: Deque&lt;T&gt;) => void, forEach(callbackfn: (value: T, index?: number, deque?: Deque&lt;T&gt;) => void,
thisArg?: Object): void thisArg?: Object): void
Uses a callback to traverse the entries in this container and obtain their position indexes. Uses a callback to traverse the elements in this container and obtain their position indexes.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| callbackfn | function | Yes| Callback invoked to traverse the entries in the container.| | callbackfn | function | Yes| Callback invoked to traverse the elements in the container.|
| thisArg | Object | No| Value to use when the callback is invoked.| | thisArg | Object | No| Value to use when the callback is invoked.|
callbackfn callbackfn
...@@ -176,7 +196,7 @@ callbackfn ...@@ -176,7 +196,7 @@ callbackfn
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| value | T | Yes| Value of the element that is currently traversed.| | value | T | Yes| Value of the element that is currently traversed.|
| index | number | No| Position index of the entry that is currently traversed.| | index | number | No| Position index of the element that is currently traversed.|
| deque | Deque&lt;T&gt; | No| Instance that invokes the **forEach** method.| | deque | Deque&lt;T&gt; | No| Instance that invokes the **forEach** method.|
**Example** **Example**
...@@ -188,7 +208,7 @@ deque.insertEnd(4); ...@@ -188,7 +208,7 @@ deque.insertEnd(4);
deque.insertFront(5); deque.insertFront(5);
deque.insertEnd(4); deque.insertEnd(4);
deque.forEach((value, index) => { deque.forEach((value, index) => {
console.log(value, index); console.log("value:" + value, index);
}); });
``` ```
...@@ -196,13 +216,15 @@ deque.forEach((value, index) => { ...@@ -196,13 +216,15 @@ deque.forEach((value, index) => {
getFirst(): T getFirst(): T
Obtains the first entry of this container. Obtains the first element of this container.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| T | Entry obtained.| | T | Element obtained.|
**Example** **Example**
...@@ -219,13 +241,15 @@ let result = deque.getFirst(); ...@@ -219,13 +241,15 @@ let result = deque.getFirst();
getLast(): T getLast(): T
Obtains the last entry of this container. Obtains the last element of this container.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| T | Entry obtained.| | T | Element obtained.|
**Example** **Example**
...@@ -242,9 +266,10 @@ let result = deque.getLast(); ...@@ -242,9 +266,10 @@ let result = deque.getLast();
[Symbol.iterator]\(): IterableIterator&lt;T&gt; [Symbol.iterator]\(): IterableIterator&lt;T&gt;
Obtains an iterator, each item of which is a JavaScript object. Obtains an iterator, each item of which is a JavaScript object.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
| Type| Description| | Type| Description|
...@@ -261,14 +286,14 @@ deque.insertFront(4); ...@@ -261,14 +286,14 @@ deque.insertFront(4);
// Method 1: // Method 1:
for (let item of deque) { for (let item of deque) {
console.log(item); console.log("value:" + item);
} }
// Method 2: // Method 2:
let iter = deque[Symbol.iterator](); let iter = deque[Symbol.iterator]();
let temp = iter.next().value; let temp = iter.next().value;
while(temp != undefined) { while(temp != undefined) {
console.log(temp); console.log("value:" + temp);
temp = iter.next().value; temp = iter.next().value;
} }
``` ```
# Nonlinear Container HashMap # Nonlinear Container HashMap
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br/> > **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. > 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.
**HashMap** is a map implemented based on the array, linked list, and red-black tree. It provides efficient data query, insertion, and removal. The elements in a **HashMap** instance are mappings of key-value pairs. Each key must be unique and have only one value.
**HashMap** is faster in accessing data than **[TreeMap](js-apis-treemap.md)**, because the former accesses the keys based on the hash codes, whereas the latter stores and accesses the keys in sorted order.
**[HashSet](js-apis-hashset.md)** is implemented based on **HashMap**. The input parameter of **HashMap** consists of **key** and **value**. In **HashSet**, only the **value** object is processed.
**Recommended use case**: Use **HashMap** when you need to quickly access, remove, and insert key-value pairs.
## Modules to Import ## Modules to Import
```ts ```ts
import HashMap from '@ohos.util.HashMap' import HashMap from '@ohos.util.HashMap';
``` ```
## System Capabilities
SystemCapability.Utils.Lang
## HashMap ## HashMap
### Attributes ### Attributes
**System capability**: SystemCapability.Utils.Lang
| Name| Type| Readable| Writable| Description| | Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- | -------- |
| length | number | Yes| No| Number of entries in a hash map (called container later).| | length | number | Yes| No| Number of elements in a hash map (called container later).|
### constructor ### constructor
...@@ -30,6 +35,8 @@ constructor() ...@@ -30,6 +35,8 @@ constructor()
A constructor used to create a **HashMap** instance. A constructor used to create a **HashMap** instance.
**System capability**: SystemCapability.Utils.Lang
**Example** **Example**
```ts ```ts
...@@ -41,7 +48,9 @@ let hashMap = new HashMap(); ...@@ -41,7 +48,9 @@ let hashMap = new HashMap();
isEmpty(): boolean isEmpty(): boolean
Checks whether this container is empty (contains no entry). Checks whether this container is empty (contains no element).
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
...@@ -63,11 +72,13 @@ hasKey(key: K): boolean ...@@ -63,11 +72,13 @@ hasKey(key: K): boolean
Checks whether this container contains the specified key. Checks whether this container contains the specified key.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| key | K | Yes| Key to check.| | key | K | Yes| Target key.|
**Return value** **Return value**
...@@ -91,11 +102,13 @@ hasValue(value: V): boolean ...@@ -91,11 +102,13 @@ hasValue(value: V): boolean
Checks whether this container contains the specified value. Checks whether this container contains the specified value.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| value | V | Yes| Value to check.| | value | V | Yes| Target value.|
**Return value** **Return value**
...@@ -119,11 +132,13 @@ get(key: K): V ...@@ -119,11 +132,13 @@ get(key: K): V
Obtains the value of the specified key in this container. Obtains the value of the specified key in this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| key | K | Yes| Key to query.| | key | K | Yes| Target key.|
**Return value** **Return value**
...@@ -145,13 +160,15 @@ let result = hashMap.get("sdfs"); ...@@ -145,13 +160,15 @@ let result = hashMap.get("sdfs");
setAll(map: HashMap<K, V>): void setAll(map: HashMap<K, V>): void
Adds all entries in a **HashMap** instance to this container. Adds all elements in a **HashMap** instance to this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| map | HashMap<K, V> | Yes| **HashMap** instance whose entries are to be added to the current container.| | map | HashMap<K, V> | Yes| **HashMap** instance whose elements are to be added to the current container.|
**Example** **Example**
...@@ -168,20 +185,22 @@ hashMap.setAll(newHashMap); ...@@ -168,20 +185,22 @@ hashMap.setAll(newHashMap);
set(key: K, value: V): Object set(key: K, value: V): Object
Adds an entry to this container. Adds an element to this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| key | K | Yes| Key of the entry to add.| | key | K | Yes| Key of the target element.|
| value | V | Yes| Value of the entry to add.| | value | V | Yes| Value of the element.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| Object | Container that contains the new entry.| | Object | Container that contains the new element.|
**Example** **Example**
...@@ -195,19 +214,21 @@ let result = hashMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123); ...@@ -195,19 +214,21 @@ let result = hashMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
remove(key: K): V remove(key: K): V
Removes an entry with the specified key from this container. Removes an element with the specified key from this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| key | K | Yes| Key of the entry to remove.| | key | K | Yes| Key of the target element.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| V | Value of the entry removed.| | V | Value of the element.|
**Example** **Example**
...@@ -225,6 +246,8 @@ clear(): void ...@@ -225,6 +246,8 @@ clear(): void
Clears this container and sets its length to **0**. Clears this container and sets its length to **0**.
**System capability**: SystemCapability.Utils.Lang
**Example** **Example**
```ts ```ts
...@@ -239,7 +262,9 @@ hashMap.clear(); ...@@ -239,7 +262,9 @@ hashMap.clear();
keys(): IterableIterator&lt;K&gt; keys(): IterableIterator&lt;K&gt;
Obtains an iterator that contains all the entries in this container. Obtains an iterator that contains all the elements in this container.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
...@@ -256,7 +281,7 @@ hashMap.set("sdfs", 356); ...@@ -256,7 +281,7 @@ hashMap.set("sdfs", 356);
let iter = hashMap.keys(); let iter = hashMap.keys();
let temp = iter.next().value; let temp = iter.next().value;
while(temp != undefined) { while(temp != undefined) {
console.log(temp); console.log("value:" + temp);
temp = iter.next().value; temp = iter.next().value;
} }
``` ```
...@@ -268,6 +293,8 @@ values(): IterableIterator&lt;V&gt; ...@@ -268,6 +293,8 @@ values(): IterableIterator&lt;V&gt;
Obtains an iterator that contains all the values in this container. Obtains an iterator that contains all the values in this container.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
| Type| Description| | Type| Description|
...@@ -283,7 +310,7 @@ hashMap.set("sdfs", 356); ...@@ -283,7 +310,7 @@ hashMap.set("sdfs", 356);
let iter = hashMap.values(); let iter = hashMap.values();
let temp = iter.next().value; let temp = iter.next().value;
while(temp != undefined) { while(temp != undefined) {
console.log(temp); console.log("value:" + temp);
temp = iter.next().value; temp = iter.next().value;
} }
``` ```
...@@ -293,20 +320,22 @@ while(temp != undefined) { ...@@ -293,20 +320,22 @@ while(temp != undefined) {
replace(key: K, newValue: V): boolean replace(key: K, newValue: V): boolean
Replaces an entry in this container. Replaces an element in this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| key | K | Yes| Key of the entry to replace.| | key | K | Yes| Key of the target element.|
| newValue | V | Yes| New value of the entry.| | newValue | V | Yes| New value of the element.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| boolean | Returns **true** if the entry is replaced successfully; returns **false** otherwise.| | boolean | Returns **true** if the element is replaced successfully; returns **false** otherwise.|
**Example** **Example**
...@@ -321,20 +350,22 @@ let result = hashMap.replace("sdfs", 357); ...@@ -321,20 +350,22 @@ let result = hashMap.replace("sdfs", 357);
forEach(callbackfn: (value?: V, key?: K, map?: HashMap<K, V>) => void, thisArg?: Object): void forEach(callbackfn: (value?: V, key?: K, map?: HashMap<K, V>) => void, thisArg?: Object): void
Uses a callback to traverse the entries in this container and obtain their position indexes. Uses a callback to traverse the elements in this container and obtain their position indexes.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| callbackfn | function | Yes| Callback invoked to traverse the entries in the container.| | callbackfn | function | Yes| Callback invoked to traverse the elements in the container.|
| thisArg | Object | No| Value to use when the callback is invoked.| | thisArg | Object | No| Value to use when the callback is invoked.|
callbackfn callbackfn
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| value | V | No| Value of the entry that is currently traversed.| | value | V | No| Value of the element that is currently traversed.|
| key | K | No| Key of the entry that is currently traversed.| | key | K | No| Key of the element that is currently traversed.|
| map | HashMap<K, V> | No| Instance that invokes the **forEach** method.| | map | HashMap<K, V> | No| Instance that invokes the **forEach** method.|
**Example** **Example**
...@@ -344,7 +375,7 @@ let hashMap = new HashMap(); ...@@ -344,7 +375,7 @@ let hashMap = new HashMap();
hashMap.set("sdfs", 123); hashMap.set("sdfs", 123);
hashMap.set("dfsghsf", 357); hashMap.set("dfsghsf", 357);
hashMap.forEach((value, key) => { hashMap.forEach((value, key) => {
console.log(value, key); console.log("value:" + value, key);
}); });
``` ```
...@@ -353,7 +384,9 @@ hashMap.forEach((value, key) => { ...@@ -353,7 +384,9 @@ hashMap.forEach((value, key) => {
entries(): IterableIterator&lt;[K, V]&gt; entries(): IterableIterator&lt;[K, V]&gt;
Obtains an iterator that contains all the entries in this container. Obtains an iterator that contains all the elements in this container.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
...@@ -370,8 +403,8 @@ hashMap.set("sdfs", 356); ...@@ -370,8 +403,8 @@ hashMap.set("sdfs", 356);
let iter = hashMap.entries(); let iter = hashMap.entries();
let temp = iter.next().value; let temp = iter.next().value;
while(temp != undefined) { while(temp != undefined) {
console.log(temp[0]); console.log("key:" + temp[0]);
console.log(temp[1]); console.log("value:" + temp[1]);
temp = iter.next().value; temp = iter.next().value;
} }
``` ```
...@@ -383,6 +416,8 @@ while(temp != undefined) { ...@@ -383,6 +416,8 @@ while(temp != undefined) {
Obtains an iterator, each item of which is a JavaScript object. Obtains an iterator, each item of which is a JavaScript object.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
| Type| Description| | Type| Description|
...@@ -397,16 +432,16 @@ hashMap.set("sdfs", 356); ...@@ -397,16 +432,16 @@ hashMap.set("sdfs", 356);
// Method 1: // Method 1:
for (let item of hashMap) { for (let item of hashMap) {
console.log("key: " + item[0]); console.log("key:" + item[0]);
console.log("value: " + item[1]); console.log("value:" + item[1]);
} }
// Method 2: // Method 2:
let iter = hashMap[Symbol.iterator](); let iter = hashMap[Symbol.iterator]();
let temp = iter.next().value; let temp = iter.next().value;
while(temp != undefined) { while(temp != undefined) {
console.log(temp[0]); console.log("key:" + temp[0]);
console.log(temp[1]); console.log("value:" + temp[1]);
temp = iter.next().value; temp = iter.next().value;
} }
``` ```
# Nonlinear Container HashSet # Nonlinear Container HashSet
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br/> > **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. > 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.
**HashSet** is implemented based on [HashMap](js-apis-hashmap.md). In **HashSet**, only the **value** object is processed.
Unlike [TreeSet](js-apis-treeset.md), which stores and accesses data in sorted order, **HashSet** stores data in a random order. This means that **HashSet** may use a different order when storing and accessing elements. Both of them allows only unique elements. However, null values are allowed in **HashSet**, but not allowed in **TreeSet**.
**Recommended use case**: Use **HashSet** when you need a set that has only unique elements or need to deduplicate a set.
## Modules to Import ## Modules to Import
...@@ -10,18 +16,15 @@ ...@@ -10,18 +16,15 @@
import HashSet from '@ohos.util.HashSet'; import HashSet from '@ohos.util.HashSet';
``` ```
## System Capabilities
SystemCapability.Utils.Lang
## HashSet ## HashSet
### Attributes ### Attributes
**System capability**: SystemCapability.Utils.Lang
| Name| Type| Readable| Writable| Description| | Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- | -------- |
| length | number | Yes| No| Number of entries in a hash set (called container later).| | length | number | Yes| No| Number of elements in a hash set (called container later).|
### constructor ### constructor
...@@ -30,6 +33,8 @@ constructor() ...@@ -30,6 +33,8 @@ constructor()
A constructor used to create a **HashSet** instance. A constructor used to create a **HashSet** instance.
**System capability**: SystemCapability.Utils.Lang
**Example** **Example**
```ts ```ts
...@@ -41,7 +46,9 @@ let hashSet = new HashSet(); ...@@ -41,7 +46,9 @@ let hashSet = new HashSet();
isEmpty(): boolean isEmpty(): boolean
Checks whether this container is empty (contains no entry). Checks whether this container is empty (contains no element).
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
...@@ -61,19 +68,21 @@ let result = hashSet.isEmpty(); ...@@ -61,19 +68,21 @@ let result = hashSet.isEmpty();
has(value: T): boolean has(value: T): boolean
Checks whether this container contains the specified entry. Checks whether this container contains the specified element.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| value | T | Yes| Entry to check.| | value | T | Yes| Target element.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| boolean | Returns **true** if the specified entry is contained; returns **false** otherwise.| | boolean | Returns **true** if the specified element is contained; returns **false** otherwise.|
**Example** **Example**
...@@ -89,19 +98,21 @@ let result1 = hashSet.has("Ahfbrgrbgnutfodgorrogorgrogofdfdf"); ...@@ -89,19 +98,21 @@ let result1 = hashSet.has("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
add(value: T): boolean add(value: T): boolean
Adds an entry to this container. Adds an element to this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| value | T | Yes| Entry to add.| | value | T | Yes| Target element.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| boolean | Returns **true** if the entry is added successfully; returns **false** otherwise.| | boolean | Returns **true** if the element is added successfully; returns **false** otherwise.|
**Example** **Example**
...@@ -115,19 +126,21 @@ let result = hashSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf"); ...@@ -115,19 +126,21 @@ let result = hashSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
remove(value: T): boolean remove(value: T): boolean
Removes an entry from this container. Removes an element from this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| value | T | Yes| Entry to remove.| | value | T | Yes| Target element.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| boolean | Returns **true** if the entry is removed successfully; returns **false** otherwise.| | boolean | Returns **true** if the element is removed successfully; returns **false** otherwise.|
**Example** **Example**
...@@ -145,6 +158,8 @@ clear(): void ...@@ -145,6 +158,8 @@ clear(): void
Clears this container and sets its length to **0**. Clears this container and sets its length to **0**.
**System capability**: SystemCapability.Utils.Lang
**Example** **Example**
```ts ```ts
...@@ -161,6 +176,8 @@ values(): IterableIterator&lt;T&gt; ...@@ -161,6 +176,8 @@ values(): IterableIterator&lt;T&gt;
Obtains an iterator that contains all the values in this container. Obtains an iterator that contains all the values in this container.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
| Type| Description| | Type| Description|
...@@ -176,7 +193,7 @@ hashSet.add("sdfs"); ...@@ -176,7 +193,7 @@ hashSet.add("sdfs");
let iter = hashSet.values(); let iter = hashSet.values();
let temp = iter.next().value; let temp = iter.next().value;
while(temp != undefined) { while(temp != undefined) {
console.log(temp); console.log("value:" + temp);
temp = iter.next().value; temp = iter.next().value;
} }
``` ```
...@@ -186,20 +203,22 @@ while(temp != undefined) { ...@@ -186,20 +203,22 @@ while(temp != undefined) {
forEach(callbackfn: (value?: T, key?: T, set?: HashSet&lt;T&gt;) => void, thisArg?: Object): void forEach(callbackfn: (value?: T, key?: T, set?: HashSet&lt;T&gt;) => void, thisArg?: Object): void
Uses a callback to traverse the entries in this container and obtain their position indexes. Uses a callback to traverse the elements in this container and obtain their position indexes.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| callbackfn | function | Yes| Callback invoked to traverse the entries in the container.| | callbackfn | function | Yes| Callback invoked to traverse the elements in the container.|
| thisArg | Object | No| Value to use when the callback is invoked.| | thisArg | Object | No| Value to use when the callback is invoked.|
callbackfn callbackfn
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| value | T | No| Value of the entry that is currently traversed.| | value | T | No| Value of the element that is currently traversed.|
| key | T | No| Key of the entry that is currently traversed (same as **value**).| | key | T | No| Key of the element that is currently traversed (same as **value**).|
| set | HashSet&lt;T&gt; | No| Instance that invokes the **forEach** method.| | set | HashSet&lt;T&gt; | No| Instance that invokes the **forEach** method.|
**Example** **Example**
...@@ -209,7 +228,7 @@ let hashSet = new HashSet(); ...@@ -209,7 +228,7 @@ let hashSet = new HashSet();
hashSet.add("sdfs"); hashSet.add("sdfs");
hashSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf"); hashSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
hashSet.forEach((value, key) => { hashSet.forEach((value, key) => {
console.log(value, key); console.log("value:" + value, key);
}); });
``` ```
...@@ -217,7 +236,9 @@ hashSet.forEach((value, key) => { ...@@ -217,7 +236,9 @@ hashSet.forEach((value, key) => {
### entries ### entries
entries(): IterableIterator<[T, T]> entries(): IterableIterator<[T, T]>
Obtains an iterator that contains all the entries in this container. Obtains an iterator that contains all the elements in this container.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
...@@ -234,8 +255,8 @@ hashSet.add("sdfs"); ...@@ -234,8 +255,8 @@ hashSet.add("sdfs");
let iter = hashSet.entries(); let iter = hashSet.entries();
let temp = iter.next().value; let temp = iter.next().value;
while(temp != undefined) { while(temp != undefined) {
console.log(temp[0]); console.log("key:" + temp[0]);
console.log(temp[1]); console.log("value:" + temp[1]);
temp = iter.next().value; temp = iter.next().value;
} }
``` ```
...@@ -247,6 +268,8 @@ while(temp != undefined) { ...@@ -247,6 +268,8 @@ while(temp != undefined) {
Obtains an iterator, each item of which is a JavaScript object. Obtains an iterator, each item of which is a JavaScript object.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
| Type| Description| | Type| Description|
...@@ -269,7 +292,7 @@ for (let item of hashSet) { ...@@ -269,7 +292,7 @@ for (let item of hashSet) {
let iter = hashSet[Symbol.iterator](); let iter = hashSet[Symbol.iterator]();
let temp = iter.next().value; let temp = iter.next().value;
while(temp != undefined) { while(temp != undefined) {
console.log(temp); console.log("value: " + temp);
temp = iter.next().value; temp = iter.next().value;
} }
``` ```
# Nonlinear Container LightWeightMap # Nonlinear Container LightWeightMap
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br/> > **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. > 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.
**LightWeightMap** stores key-value (KV) pairs. Each key must be unique and have only one value.
**LightWeightMap** is based on generics and uses a lightweight structure. Keys in the map are searched using hash values, which are stored in an array.
Compared with **[HashMap](js-apis-hashmap.md)**, which can also store KV pairs, **LightWeightMap** occupies less memory.
**Recommended use case**: Use LightWeightMap when you need to store and access **KV pairs**.
## Modules to Import ## Modules to Import
```ts ```ts
import LightWeightMap from '@ohos.util.LightWeightMap' import LightWeightMap from '@ohos.util.LightWeightMap';
``` ```
## System Capabilities
SystemCapability.Utils.Lang
## LightWeightMap ## LightWeightMap
### Attributes ### Attributes
**System capability**: SystemCapability.Utils.Lang
| Name| Type| Readable| Writable| Description| | Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- | -------- |
| length | number | Yes| No| Number of entries in a lightweight map (called container later).| | length | number | Yes| No| Number of elements in a lightweight map (called container later).|
### constructor ### constructor
...@@ -30,6 +37,8 @@ constructor() ...@@ -30,6 +37,8 @@ constructor()
A constructor used to create a **LightWeightMap** instance. A constructor used to create a **LightWeightMap** instance.
**System capability**: SystemCapability.Utils.Lang
**Example** **Example**
```ts ```ts
...@@ -41,7 +50,9 @@ let lightWeightMap = new LightWeightMap(); ...@@ -41,7 +50,9 @@ let lightWeightMap = new LightWeightMap();
isEmpty(): boolean isEmpty(): boolean
Checks whether this container is empty (contains no entry). Checks whether this container is empty (contains no element).
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
...@@ -61,7 +72,9 @@ let result = lightWeightMap.isEmpty(); ...@@ -61,7 +72,9 @@ let result = lightWeightMap.isEmpty();
hasAll(map: LightWeightMap<K, V>): boolean hasAll(map: LightWeightMap<K, V>): boolean
Checks whether this container contains all entries of the specified **LightWeightMap** instance. Checks whether this container contains all elements of the specified **LightWeightMap** instance.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
...@@ -73,7 +86,7 @@ Checks whether this container contains all entries of the specified **LightWeigh ...@@ -73,7 +86,7 @@ Checks whether this container contains all entries of the specified **LightWeigh
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| boolean | Returns **true** if all the entries in the specified **LightWeightMap** instance are contained; returns **false** otherwise.| | boolean | Returns **true** if all the elements in the specified **LightWeightMap** instance are contained; returns **false** otherwise.|
**Example** **Example**
...@@ -93,11 +106,13 @@ hasKey(key: K): boolean; ...@@ -93,11 +106,13 @@ hasKey(key: K): boolean;
Checks whether this container contains the specified key. Checks whether this container contains the specified key.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| key | K | Yes| Key to check.| | key | K | Yes| Target key.|
**Return value** **Return value**
...@@ -122,11 +137,13 @@ hasValue(value: V): boolean ...@@ -122,11 +137,13 @@ hasValue(value: V): boolean
Checks whether this container contains the specified value. Checks whether this container contains the specified value.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| value | V | Yes| Value to check.| | value | V | Yes| Target value.|
**Return value** **Return value**
...@@ -150,11 +167,13 @@ increaseCapacityTo(minimumCapacity: number): void ...@@ -150,11 +167,13 @@ increaseCapacityTo(minimumCapacity: number): void
Increases the capacity of this container. Increases the capacity of this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| minimumCapacity | number | Yes| Minimum number of entries to accommodate in this container.| | minimumCapacity | number | Yes| Minimum number of elements to accommodate in this container.|
**Example** **Example**
...@@ -170,11 +189,13 @@ get(key: K): V ...@@ -170,11 +189,13 @@ get(key: K): V
Obtains the value of the specified key in this container. Obtains the value of the specified key in this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| key | K | Yes| Key to query.| | key | K | Yes| Target key.|
**Return value** **Return value**
...@@ -196,19 +217,21 @@ let result = lightWeightMap.get("sdfs"); ...@@ -196,19 +217,21 @@ let result = lightWeightMap.get("sdfs");
getIndexOfKey(key: K): number getIndexOfKey(key: K): number
Obtains the index of the first occurrence of an entry with the specified key in this container. Obtains the index of the first occurrence of an element with the specified key in this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| key | K | Yes| Key of the entry.| | key | K | Yes| Key of the element.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| number | Returns the position index if obtained; returns **-1** if the specified entry is not found.| | number | Returns the position index if obtained; returns **-1** otherwise.|
**Example** **Example**
...@@ -224,19 +247,21 @@ let result = lightWeightMap.getIndexOfKey("sdfs"); ...@@ -224,19 +247,21 @@ let result = lightWeightMap.getIndexOfKey("sdfs");
getIndexOfValue(value: V): number getIndexOfValue(value: V): number
Obtains the index of the first occurrence of an entry with the specified value in this container. Obtains the index of the first occurrence of an element with the specified value in this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| value | V | Yes| Value of the entry.| | value | V | Yes| Key of the element.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| number | Returns the position index if obtained; returns **-1** if the specified entry is not found.| | number | Returns the position index if obtained; returns **-1** otherwise.|
**Example** **Example**
...@@ -252,13 +277,15 @@ let result = lightWeightMap.getIndexOfValue(123); ...@@ -252,13 +277,15 @@ let result = lightWeightMap.getIndexOfValue(123);
getKeyAt(index: number): K getKeyAt(index: number): K
Obtains the key of an entry at the specified position in this container. Obtains the key of an element at the specified position in this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| index | number | Yes| Position index of the entry.| | index | number | Yes| Position index of the element.|
**Return value** **Return value**
...@@ -280,13 +307,15 @@ let result = lightWeightMap.getKeyAt(1); ...@@ -280,13 +307,15 @@ let result = lightWeightMap.getKeyAt(1);
setAll(map: LightWeightMap<K, V>): void setAll(map: LightWeightMap<K, V>): void
Adds all entries in a **LightWeightMap** instance to this container. Adds all elements in a **LightWeightMap** instance to this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| map | LightWeightMap<K, V> | Yes| **LightWeightMap** instance whose entries are to be added to the current container.| | map | LightWeightMap<K, V> | Yes| **LightWeightMap** instance whose elements are to be added to the current container.|
**Example** **Example**
...@@ -302,20 +331,22 @@ lightWeightMap.setAll(map); ...@@ -302,20 +331,22 @@ lightWeightMap.setAll(map);
### set ### set
set(key: K, value: V): Object set(key: K, value: V): Object
Adds an entry to this container. Adds an element to this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| key | K | Yes| Key of the entry to add.| | key | K | Yes| Key of the target element.|
| value | V | Yes| Value of the entry to add.| | value | V | Yes| Value of the target element.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| Object | Container that contains the new entry.| | Object | Container that contains the new element.|
**Example** **Example**
...@@ -329,19 +360,21 @@ let result = lightWeightMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123); ...@@ -329,19 +360,21 @@ let result = lightWeightMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
remove(key: K): V remove(key: K): V
Removes an entry with the specified key from this container. Removes an element with the specified key from this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| key | K | Yes| Key of the entry to remove.| | key | K | Yes| Target key.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| V | Value of the entry removed.| | V | Value of the element removed.|
**Example** **Example**
...@@ -357,19 +390,21 @@ lightWeightMap.remove("sdfs"); ...@@ -357,19 +390,21 @@ lightWeightMap.remove("sdfs");
removeAt(index: number): boolean removeAt(index: number): boolean
Removes an entry at the specified position from this container. Removes an element at the specified position from this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| index | number | Yes| Position index of the entry.| | index | number | Yes| Position index of the element.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| boolean | Returns **true** if the entry is removed successfully; returns **false** otherwise.| | boolean | Returns **true** if the element is removed successfully; returns **false** otherwise.|
**Example** **Example**
...@@ -385,14 +420,16 @@ let result = lightWeightMap.removeAt(1); ...@@ -385,14 +420,16 @@ let result = lightWeightMap.removeAt(1);
setValueAt(index: number, newValue: V): boolean setValueAt(index: number, newValue: V): boolean
Sets a value for an entry at the specified position in this container. Sets a value for an element at the specified position in this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| index | number | Yes| Position index of the entry.| | index | number | Yes| Position index of the target element.|
| newValue | V | Yes| Value of the entry to set.| | newValue | V | Yes| Value of the target element to set.|
**Return value** **Return value**
...@@ -414,13 +451,15 @@ lightWeightMap.setValueAt(1, 3546); ...@@ -414,13 +451,15 @@ lightWeightMap.setValueAt(1, 3546);
getValueAt(index: number): V getValueAt(index: number): V
Obtains the value of an entry at the specified position in this container. Obtains the value of an element at the specified position in this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| index | number | Yes| Position index of the entry.| | index | number | Yes| Position index of the element.|
**Return value** **Return value**
...@@ -444,6 +483,8 @@ clear(): void ...@@ -444,6 +483,8 @@ clear(): void
Clears this container and sets its length to **0**. Clears this container and sets its length to **0**.
**System capability**: SystemCapability.Utils.Lang
**Example** **Example**
```ts ```ts
...@@ -460,6 +501,8 @@ keys(): IterableIterator&lt;K&gt; ...@@ -460,6 +501,8 @@ keys(): IterableIterator&lt;K&gt;
Obtains an iterator that contains all the keys in this container. Obtains an iterator that contains all the keys in this container.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
| Type| Description| | Type| Description|
...@@ -475,7 +518,7 @@ lightWeightMap.set("sdfs", 356); ...@@ -475,7 +518,7 @@ lightWeightMap.set("sdfs", 356);
let iter = lightWeightMap.keys(); let iter = lightWeightMap.keys();
let temp = iter.next().value; let temp = iter.next().value;
while(temp != undefined) { while(temp != undefined) {
console.log(temp); console.log("value:" + temp);
temp = iter.next().value; temp = iter.next().value;
} }
``` ```
...@@ -487,6 +530,8 @@ values(): IterableIterator&lt;V&gt; ...@@ -487,6 +530,8 @@ values(): IterableIterator&lt;V&gt;
Obtains an iterator that contains all the values in this container. Obtains an iterator that contains all the values in this container.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
| Type| Description| | Type| Description|
...@@ -502,7 +547,7 @@ lightWeightMap.set("sdfs", 356); ...@@ -502,7 +547,7 @@ lightWeightMap.set("sdfs", 356);
let iter = lightWeightMap.values(); let iter = lightWeightMap.values();
let temp = iter.next().value; let temp = iter.next().value;
while(temp != undefined) { while(temp != undefined) {
console.log(temp); console.log("value:" + temp);
temp = iter.next().value; temp = iter.next().value;
} }
``` ```
...@@ -512,20 +557,22 @@ while(temp != undefined) { ...@@ -512,20 +557,22 @@ while(temp != undefined) {
forEach(callbackfn: (value?: V, key?: K, map?: LightWeightMap<K, V>) => void, thisArg?: Object): void forEach(callbackfn: (value?: V, key?: K, map?: LightWeightMap<K, V>) => void, thisArg?: Object): void
Uses a callback to traverse the entries in this container and obtain their position indexes. Uses a callback to traverse the elements in this container and obtain their position indexes.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| callbackfn | function | Yes| Callback invoked to traverse the entries in the container.| | callbackfn | function | Yes| Callback invoked to traverse the elements in the container.|
| thisArg | Object | No| Value to use when the callback is invoked.| | thisArg | Object | No| Value to use when the callback is invoked.|
callbackfn callbackfn
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| value | V | No| Value of the entry that is currently traversed.| | value | V | No| Value of the element that is currently traversed.|
| key | K | No| Key of the entry that is currently traversed.| | key | K | No| Key of the element that is currently traversed.|
| map | LightWeightMap<K, V> | No| Instance that invokes the **forEach** method.| | map | LightWeightMap<K, V> | No| Instance that invokes the **forEach** method.|
**Example** **Example**
...@@ -535,7 +582,7 @@ let lightWeightMap = new LightWeightMap(); ...@@ -535,7 +582,7 @@ let lightWeightMap = new LightWeightMap();
lightWeightMap.set("sdfs", 123); lightWeightMap.set("sdfs", 123);
lightWeightMap.set("dfsghsf", 357); lightWeightMap.set("dfsghsf", 357);
lightWeightMap.forEach((value, key) => { lightWeightMap.forEach((value, key) => {
console.log(value, key); console.log("value:" + value, key);
}); });
``` ```
...@@ -544,7 +591,9 @@ lightWeightMap.forEach((value, key) => { ...@@ -544,7 +591,9 @@ lightWeightMap.forEach((value, key) => {
entries(): IterableIterator<[K, V]> entries(): IterableIterator<[K, V]>
Obtains an iterator that contains all the entries in this container. Obtains an iterator that contains all the elements in this container.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
...@@ -561,8 +610,8 @@ lightWeightMap.set("sdfs", 356); ...@@ -561,8 +610,8 @@ lightWeightMap.set("sdfs", 356);
let iter = lightWeightMap.entries(); let iter = lightWeightMap.entries();
let temp = iter.next().value; let temp = iter.next().value;
while(temp != undefined) { while(temp != undefined) {
console.log(temp[0]); console.log("key:" + temp[0]);
console.log(temp[1]); console.log("value:" + temp[1]);
temp = iter.next().value; temp = iter.next().value;
} }
``` ```
...@@ -571,13 +620,15 @@ while(temp != undefined) { ...@@ -571,13 +620,15 @@ while(temp != undefined) {
toString(): String toString(): String
Concatenates the entries in this container into a string and returns the string. Concatenates the elements in this container into a string and returns the string.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| String | Returns a string.| | String | String obtained.|
**Example** **Example**
...@@ -594,6 +645,8 @@ Concatenates the entries in this container into a string and returns the string. ...@@ -594,6 +645,8 @@ Concatenates the entries in this container into a string and returns the string.
Obtains an iterator, each item of which is a JavaScript object. Obtains an iterator, each item of which is a JavaScript object.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
| Type| Description| | Type| Description|
...@@ -609,16 +662,16 @@ lightWeightMap.set("sdfs", 356); ...@@ -609,16 +662,16 @@ lightWeightMap.set("sdfs", 356);
// Method 1: // Method 1:
for (let item of lightWeightMap) { for (let item of lightWeightMap) {
console.log("key: " + item[0]); console.log("key:" + item[0]);
console.log("value: " + item[1]); console.log("value:" + item[1]);
} }
// Method 2: // Method 2:
let iter = lightWeightMap[Symbol.iterator](); let iter = lightWeightMap[Symbol.iterator]();
let temp = iter.next().value; let temp = iter.next().value;
while(temp != undefined) { while(temp != undefined) {
console.log(temp[0]); console.log("key:" + temp[0]);
console.log(temp[1]); console.log("value:" + temp[1]);
temp = iter.next().value; temp = iter.next().value;
} }
``` ```
# Nonlinear Container LightWeightSet # Nonlinear Container LightWeightSet
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br/> > **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. > 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.
**LightWeightSet** stores a set of values, each of which must be unique.
**LightWeightSet** is based on generics and uses a lightweight structure. Its default initial capacity is 8, and it has the capacity doubled in each expansion.
The values in such a set are searched using hash values, which are stored in an array.
Compared with **[HashSet](js-apis-hashset.md)**, which can also store values, **LightWeightSet** occupies less memory.
**Recommended use case**: Use **LightWeightSet** when you need a set that has only unique elements or need to deduplicate a set.
## Modules to Import ## Modules to Import
```ts ```ts
import LightWeightSet from '@ohos.util.LightWeightSet' import LightWeightSet from '@ohos.util.LightWeightSet';
``` ```
## System Capabilities
SystemCapability.Utils.Lang
## LightWeightSet ## LightWeightSet
### Attributes ### Attributes
**System capability**: SystemCapability.Utils.Lang
| Name| Type| Readable| Writable| Description| | Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- | -------- |
| length | number | Yes| No| Number of entries in a lightweight set (called container later).| | length | number | Yes| No| Number of elements in a lightweight set (called container later).|
### constructor ### constructor
...@@ -30,6 +39,8 @@ constructor() ...@@ -30,6 +39,8 @@ constructor()
A constructor used to create a **LightWeightSet** instance. A constructor used to create a **LightWeightSet** instance.
**System capability**: SystemCapability.Utils.Lang
**Example** **Example**
```ts ```ts
...@@ -41,7 +52,9 @@ let lightWeightSet = new LightWeightSet(); ...@@ -41,7 +52,9 @@ let lightWeightSet = new LightWeightSet();
isEmpty(): boolean isEmpty(): boolean
Checks whether this container is empty. Checks whether this container is empty (contains no element).
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
...@@ -60,19 +73,21 @@ let result = lightWeightSet.isEmpty(); ...@@ -60,19 +73,21 @@ let result = lightWeightSet.isEmpty();
add(obj: T): boolean add(obj: T): boolean
Adds an entry to this container. Adds an element to this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| obj | T | Yes| Entry to add.| | obj | T | Yes| Target element.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| boolean | Returns **true** if the entry is added successfully; returns **false** otherwise.| | boolean | Returns **true** if the element is added successfully; returns **false** otherwise.|
**Example** **Example**
...@@ -86,13 +101,15 @@ let result = lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf"); ...@@ -86,13 +101,15 @@ let result = lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
addAll(set: LightWeightSet&lt;T&gt;): boolean addAll(set: LightWeightSet&lt;T&gt;): boolean
Adds all entries in a **LightWeightSet** instance to this container. Adds all elements in a **LightWeightSet** instance to this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| set | LightWeightSet&lt;T&gt; | Yes| **LightWeightSet** instance whose entries are to be added to the current container.| | set | LightWeightSet&lt;T&gt; | Yes| **LightWeightSet** instance whose elements are to be added to the current container.|
**Example** **Example**
...@@ -110,7 +127,9 @@ let result = lightWeightSet.addAll(set); ...@@ -110,7 +127,9 @@ let result = lightWeightSet.addAll(set);
hasAll(set: LightWeightSet&lt;T&gt;): boolean hasAll(set: LightWeightSet&lt;T&gt;): boolean
Checks whether this container contains all entries of the specified **LightWeightSet** instance. Checks whether this container contains all elements of the specified **LightWeightSet** instance.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
...@@ -122,7 +141,7 @@ Checks whether this container contains all entries of the specified **LightWeigh ...@@ -122,7 +141,7 @@ Checks whether this container contains all entries of the specified **LightWeigh
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| boolean | Returns **true** if all the entries in the specified **LightWeightSet** instance are contained; returns **false** otherwise.| | boolean | Returns **true** if all the elements in the specified **LightWeightSet** instance are contained; returns **false** otherwise.|
**Example** **Example**
...@@ -142,11 +161,13 @@ has(key: T): boolean ...@@ -142,11 +161,13 @@ has(key: T): boolean
Checks whether this container has the specified key. Checks whether this container has the specified key.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| key| T | Yes| Key to query.| | key| T | Yes| Target key.|
**Return value** **Return value**
...@@ -170,6 +191,8 @@ equal(obj: Object): boolean ...@@ -170,6 +191,8 @@ equal(obj: Object): boolean
Checks whether this container contains objects of the same type as the specified **obj**. Checks whether this container contains objects of the same type as the specified **obj**.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
...@@ -199,11 +222,13 @@ increaseCapacityTo(minimumCapacity: number): void ...@@ -199,11 +222,13 @@ increaseCapacityTo(minimumCapacity: number): void
Increases the capacity of this container. Increases the capacity of this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| minimumCapacity | number | Yes| Minimum number of entries to accommodate in the container.| | minimumCapacity | number | Yes| Minimum number of elements to accommodate in the container.|
**Example** **Example**
...@@ -217,19 +242,21 @@ lightWeightSet.increaseCapacityTo(10); ...@@ -217,19 +242,21 @@ lightWeightSet.increaseCapacityTo(10);
getIndexOf(key: T): number getIndexOf(key: T): number
Obtains the position index of the entry with the specified key in this container. Obtains the position index of the element with the specified key in this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| key| T | Yes| Key of the entry to query.| | key| T | Yes| Key of the target element.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| number | Position index of the entry.| | number | Position index of the element.|
**Example** **Example**
...@@ -245,19 +272,21 @@ let result = lightWeightSet.getIndexOf("sdfs"); ...@@ -245,19 +272,21 @@ let result = lightWeightSet.getIndexOf("sdfs");
remove(key: T): T remove(key: T): T
Removes an entry of the specified key from this container. Removes an element of the specified key from this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| key| T | Yes| Key of the entry to remove.| | key| T | Yes| Key of the target element.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| T | Value of the entry removed.| | T | Value of the element removed.|
**Example** **Example**
...@@ -273,19 +302,21 @@ let result = lightWeightSet.remove("sdfs"); ...@@ -273,19 +302,21 @@ let result = lightWeightSet.remove("sdfs");
removeAt(index: number): boolean removeAt(index: number): boolean
Removes the entry at the specified position from this container. Removes the element at the specified position from this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| index | number | Yes| Position index of the entry.| | index | number | Yes| Position index of the element.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| boolean | Returns **true** if the entry is removed successfully; returns **false** otherwise.| | boolean | Returns **true** if the element is removed successfully; returns **false** otherwise.|
**Example** **Example**
...@@ -301,13 +332,15 @@ let result = lightWeightSet.removeAt(1); ...@@ -301,13 +332,15 @@ let result = lightWeightSet.removeAt(1);
getValueAt(index: number): T getValueAt(index: number): T
Obtains the value of the entry at the specified position in this container. Obtains the value of the element at the specified position in this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| index | number | Yes| Position index of the entry.| | index | number | Yes| Position index of the element.|
**Return value** **Return value**
...@@ -331,6 +364,8 @@ clear(): void ...@@ -331,6 +364,8 @@ clear(): void
Clears this container and sets its length to **0**. Clears this container and sets its length to **0**.
**System capability**: SystemCapability.Utils.Lang
**Example** **Example**
```ts ```ts
...@@ -345,7 +380,9 @@ lightWeightSet.clear(); ...@@ -345,7 +380,9 @@ lightWeightSet.clear();
toString(): String toString(): String
Obtains a string that contains all entries in this container. Obtains a string that contains all elements in this container.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
...@@ -369,6 +406,8 @@ toArray(): Array&lt;T&gt; ...@@ -369,6 +406,8 @@ toArray(): Array&lt;T&gt;
Obtains an array that contains all objects in this container. Obtains an array that contains all objects in this container.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
| Type| Description| | Type| Description|
...@@ -391,6 +430,8 @@ values(): IterableIterator&lt;T&gt; ...@@ -391,6 +430,8 @@ values(): IterableIterator&lt;T&gt;
Obtains an iterator that contains all the values in this container. Obtains an iterator that contains all the values in this container.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
| Type| Description| | Type| Description|
...@@ -416,20 +457,22 @@ while(index < lightWeightSet.length) { ...@@ -416,20 +457,22 @@ while(index < lightWeightSet.length) {
forEach(callbackfn: (value?: T, key?: T, set?: LightWeightSet&lt;T&gt;) => void, thisArg?: Object): void forEach(callbackfn: (value?: T, key?: T, set?: LightWeightSet&lt;T&gt;) => void, thisArg?: Object): void
Uses a callback to traverse the entries in this container and obtain their position indexes. Uses a callback to traverse the elements in this container and obtain their position indexes.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| callbackfn | function | Yes| Callback invoked to traverse the entries in the container.| | callbackfn | function | Yes| Callback invoked to traverse the elements in the container.|
| thisArg | Object | No| Value to use when the callback is invoked.| | thisArg | Object | No| Value to use when the callback is invoked.|
callbackfn callbackfn
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| value | T | No| Value of the entry that is currently traversed.| | value | T | No| Value of the element that is currently traversed.|
| key| T | No| Key of the entry that is currently traversed (same as **value**).| | key| T | No| Key of the element that is currently traversed (same as **value**).|
| set | LightWeightSet&lt;T&gt; | No| Instance that invokes the **forEach** method.| | set | LightWeightSet&lt;T&gt; | No| Instance that invokes the **forEach** method.|
**Example** **Example**
...@@ -439,7 +482,7 @@ let lightWeightSet = new LightWeightSet(); ...@@ -439,7 +482,7 @@ let lightWeightSet = new LightWeightSet();
lightWeightSet.add("sdfs"); lightWeightSet.add("sdfs");
lightWeightSet.add("dfsghsf"); lightWeightSet.add("dfsghsf");
lightWeightSet.forEach((value, key) => { lightWeightSet.forEach((value, key) => {
console.log(value, key); console.log("value:" + value, key);
}); });
``` ```
...@@ -448,7 +491,9 @@ lightWeightSet.forEach((value, key) => { ...@@ -448,7 +491,9 @@ lightWeightSet.forEach((value, key) => {
entries(): IterableIterator<[T, T]> entries(): IterableIterator<[T, T]>
Obtains an iterator that contains all the entries in this container. Obtains an iterator that contains all the elements in this container.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
...@@ -477,6 +522,8 @@ while(index < lightWeightSet.length) { ...@@ -477,6 +522,8 @@ while(index < lightWeightSet.length) {
Obtains an iterator, each item of which is a JavaScript object. Obtains an iterator, each item of which is a JavaScript object.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
| Type| Description| | Type| Description|
...@@ -492,14 +539,14 @@ lightWeightSet.add("sdfs"); ...@@ -492,14 +539,14 @@ lightWeightSet.add("sdfs");
// Method 1: // Method 1:
for (let item of lightWeightSet) { for (let item of lightWeightSet) {
console.log("value: " + item); console.log("value:" + item);
} }
// Method 2: // Method 2:
let iter = lightWeightSet[Symbol.iterator](); let iter = lightWeightSet[Symbol.iterator]();
let temp = iter.next().value; let temp = iter.next().value;
while(temp != undefined) { while(temp != undefined) {
console.log(temp); console.log("value:" + temp);
temp = iter.next().value; temp = iter.next().value;
} }
``` ```
# Linear Container LinkedList # Linear Container LinkedList
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br/> > **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. > 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.
**LinkedList** is implemented based on the doubly linked list. Each node of the doubly linked list has references pointing to the previous element and the next element. When querying an element, the system traverses the list from the beginning or end. **LinkedList** offers efficient insertion and removal operations but supports low query efficiency. **LinkedList** allows null elements.
Unlike **[List](js-apis-list.md)**, which is a singly linked list, **LinkedList** is a doubly linked list that supports insertion and removal at both ends.
**LinkedList** is less efficient in data access than **[ArrayList](js-apis-arraylist.md)**.
**Recommended use case**: Use **LinkedList** for frequent insertion and removal operations.
## Modules to Import ## Modules to Import
```ts ```ts
import LinkedList from '@ohos.util.LinkedList' import LinkedList from '@ohos.util.LinkedList';
``` ```
## System Capability
SystemCapability.Utils.Lang
## LinkedList ## LinkedList
### Attributes ### Attributes
**System capability**: SystemCapability.Utils.Lang
| Name| Type| Readable| Writable| Description| | Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- | -------- |
| length | number | Yes| No| Number of entries in a linked list (called container later).| | length | number | Yes| No| Number of elements in a linked list (called container later).|
### constructor ### constructor
...@@ -31,6 +38,8 @@ constructor() ...@@ -31,6 +38,8 @@ constructor()
A constructor used to create a **LinkedList** instance. A constructor used to create a **LinkedList** instance.
**System capability**: SystemCapability.Utils.Lang
**Example** **Example**
...@@ -43,19 +52,21 @@ let linkedList = new LinkedList(); ...@@ -43,19 +52,21 @@ let linkedList = new LinkedList();
add(element: T): boolean add(element: T): boolean
Adds an entry at the end of this container. Adds an element at the end of this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to add.| | element | T | Yes| Target element.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| boolean | Returns **true** if the entry is added successfully; returns **false** otherwise.| | boolean | Returns **true** if the element is added successfully; returns **false** otherwise.|
**Example** **Example**
...@@ -73,13 +84,15 @@ let result3 = linkedList.add(false); ...@@ -73,13 +84,15 @@ let result3 = linkedList.add(false);
addFirst(element: T): void addFirst(element: T): void
Adds an entry at the top of this container. Adds an element at the top of this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to add.| | element | T | Yes| Target element.|
**Example** **Example**
...@@ -97,14 +110,16 @@ linkedList.addFirst(false); ...@@ -97,14 +110,16 @@ linkedList.addFirst(false);
insert(index: number, element: T): void insert(index: number, element: T): void
Inserts an entry at the specified position in this container. Inserts an element at the specified position in this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to insert.| | element | T | Yes| Target element.|
| index | number | Yes| Index of the position where the entry is to be inserted.| | index | number | Yes| Index of the position where the element is to be inserted.|
**Example** **Example**
...@@ -119,19 +134,21 @@ linkedList.insert(2, true); ...@@ -119,19 +134,21 @@ linkedList.insert(2, true);
has(element: T): boolean has(element: T): boolean
Checks whether this container has the specified entry. Checks whether this container has the specified element.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to check.| | element | T | Yes| Target element.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| boolean | Returns **true** if the specified entry is contained; returns **false** otherwise.| | boolean | Returns **true** if the specified element is contained; returns **false** otherwise.|
**Example** **Example**
...@@ -146,19 +163,21 @@ let result = linkedList.has("Ahfbrgrbgnutfodgorrogorg"); ...@@ -146,19 +163,21 @@ let result = linkedList.has("Ahfbrgrbgnutfodgorrogorg");
get(index: number): T get(index: number): T
Obtains an entry at the specified position in this container. Obtains an element at the specified position in this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| index | number | Yes| Position index of the entry to query.| | index | number | Yes| Position index of the target element.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| T | Entry obtained.| | T | Element obtained.|
**Example** **Example**
...@@ -178,19 +197,21 @@ let result = linkedList.get(2); ...@@ -178,19 +197,21 @@ let result = linkedList.get(2);
getLastIndexOf(element: T): number getLastIndexOf(element: T): number
Obtains the index of the last occurrence of the specified entry in this container. Obtains the index of the last occurrence of the specified element in this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to check.| | element | T | Yes| Target element.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| number | Returns the position index if obtained; returns **-1** if the specified entry is not found.| | number | Returns the position index if obtained; returns **-1** otherwise.|
**Example** **Example**
...@@ -210,19 +231,21 @@ let result = linkedList.getLastIndexOf(2); ...@@ -210,19 +231,21 @@ let result = linkedList.getLastIndexOf(2);
getIndexOf(element: T): number getIndexOf(element: T): number
Obtains the index of the first occurrence of the specified entry in this container. Obtains the index of the first occurrence of the specified element in this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to check.| | element | T | Yes| Target element.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| number | Returns the position index if obtained; returns **-1** if the specified entry is not found.| | number | Returns the position index if obtained; returns **-1** otherwise.|
**Example** **Example**
...@@ -242,19 +265,21 @@ let result = linkedList.getIndexOf(2); ...@@ -242,19 +265,21 @@ let result = linkedList.getIndexOf(2);
removeByIndex(index: number): T removeByIndex(index: number): T
Removes an entry at the specified position from this container. Removes an element at the specified position from this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| index | number | Yes| Position index of the entry to remove.| | index | number | Yes| Position index of the target element.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| T | Entry removed.| | T | Element removed.|
**Example** **Example**
...@@ -272,13 +297,15 @@ let result = linkedList.removeByIndex(2); ...@@ -272,13 +297,15 @@ let result = linkedList.removeByIndex(2);
removeFirst(): T removeFirst(): T
Removes the first entry from this container. Removes the first element from this container.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| T | Entry removed.| | T | Element removed.|
**Example** **Example**
...@@ -296,13 +323,15 @@ let result = linkedList.removeFirst(); ...@@ -296,13 +323,15 @@ let result = linkedList.removeFirst();
removeLast(): T removeLast(): T
Removes the last entry from this container. Removes the last element from this container.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| T | Entry removed.| | T | Element removed.|
**Example** **Example**
...@@ -320,19 +349,21 @@ let result = linkedList.removeLast(); ...@@ -320,19 +349,21 @@ let result = linkedList.removeLast();
remove(element: T): boolean remove(element: T): boolean
Removes the first occurrence of the specified entry from this container. Removes the first occurrence of the specified element from this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to check.| | element | T | Yes| Target element.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| boolean | Returns **true** if the entry is removed successfully; returns **false** otherwise.| | boolean | Returns **true** if the element is removed successfully; returns **false** otherwise.|
**Example** **Example**
...@@ -349,19 +380,21 @@ let result = linkedList.remove(2); ...@@ -349,19 +380,21 @@ let result = linkedList.remove(2);
removeFirstFound(element: T): boolean removeFirstFound(element: T): boolean
Removes the first occurrence of the specified entry from this container. Removes the first occurrence of the specified element from this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to check.| | element | T | Yes| Target element.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| boolean | Returns **true** if the entry is removed successfully; returns **false** otherwise.| | boolean | Returns **true** if the element is removed successfully; returns **false** otherwise.|
**Example** **Example**
...@@ -378,19 +411,21 @@ let result = linkedList.removeFirstFound(4); ...@@ -378,19 +411,21 @@ let result = linkedList.removeFirstFound(4);
removeLastFound(element: T): boolean removeLastFound(element: T): boolean
Removes the last occurrence of the specified entry from this container. Removes the last occurrence of the specified element from this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to check.| | element | T | Yes| Target element.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| boolean | Returns **true** if the entry is removed successfully; returns **false** otherwise.| | boolean | Returns **true** if the element is removed successfully; returns **false** otherwise.|
**Example** **Example**
...@@ -409,6 +444,8 @@ clone(): LinkedList&lt;T&gt; ...@@ -409,6 +444,8 @@ clone(): LinkedList&lt;T&gt;
Clones this container and returns a copy. The modification to the copy does not affect the original instance. Clones this container and returns a copy. The modification to the copy does not affect the original instance.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
| Type| Description| | Type| Description|
...@@ -431,21 +468,23 @@ let result = linkedList.clone(); ...@@ -431,21 +468,23 @@ let result = linkedList.clone();
forEach(callbackfn: (value: T, index?: number, LinkedList?: LinkedList&lt;T&gt;) => void, forEach(callbackfn: (value: T, index?: number, LinkedList?: LinkedList&lt;T&gt;) => void,
thisArg?: Object): void thisArg?: Object): void
Uses a callback to traverse the entries in this container and obtain their position indexes. Uses a callback to traverse the elements in this container and obtain their position indexes.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| callbackfn | function | Yes| Callback invoked to traverse the entries in the container.| | callbackfn | function | Yes| Callback invoked to traverse the elements in the container.|
| thisArg | Object | No| Value to use when the callback is invoked.| | thisArg | Object | No| Value to use when the callback is invoked.|
callbackfn callbackfn
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| value | T | Yes| Value of the entry that is currently traversed.| | value | T | Yes| Value of the element that is currently traversed.|
| index | number | No| Position index of the entry that is currently traversed.| | index | number | No| Position index of the element that is currently traversed.|
| LinkedList | LinkedList&lt;T&gt; | No| Instance that invokes the **forEach** API.| | LinkedList | LinkedList&lt;T&gt; | No| Instance that invokes the **forEach** API.|
**Example** **Example**
...@@ -457,7 +496,7 @@ linkedList.add(4); ...@@ -457,7 +496,7 @@ linkedList.add(4);
linkedList.add(5); linkedList.add(5);
linkedList.add(4); linkedList.add(4);
linkedList.forEach((value, index) => { linkedList.forEach((value, index) => {
console.log(value, index); console.log("value:" + value, index);
}); });
``` ```
...@@ -467,6 +506,8 @@ clear(): void ...@@ -467,6 +506,8 @@ clear(): void
Clears this container and sets its length to **0**. Clears this container and sets its length to **0**.
**System capability**: SystemCapability.Utils.Lang
**Example** **Example**
```ts ```ts
...@@ -482,20 +523,22 @@ linkedList.clear(); ...@@ -482,20 +523,22 @@ linkedList.clear();
set(index: number, element: T): T set(index: number, element: T): T
Replaces an entry at the specified position in this container with a given entry. Replaces an element at the specified position in this container with a given element.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| index | number | Yes| Position index of the entry to replace.| | index | number | Yes| Position index of the target element.|
| element | T | Yes| Entry to be used for replacement.| | element | T | Yes| Element to be used for replacement.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| T | New entry.| | T | New element.|
**Example** **Example**
...@@ -514,6 +557,8 @@ convertToArray(): Array&lt;T&gt; ...@@ -514,6 +557,8 @@ convertToArray(): Array&lt;T&gt;
Converts this container into an array. Converts this container into an array.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
| Type| Description| | Type| Description|
...@@ -534,13 +579,15 @@ let result = linkedList.convertToArray(); ...@@ -534,13 +579,15 @@ let result = linkedList.convertToArray();
getFirst(): T getFirst(): T
Obtains the first entry in this container. Obtains the first element in this container.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| T | Returns the entry if obtained; returns **undefined** otherwise.| | T | Returns the element if obtained; returns **undefined** otherwise.|
**Example** **Example**
...@@ -557,13 +604,15 @@ let result = linkedList.getFirst(); ...@@ -557,13 +604,15 @@ let result = linkedList.getFirst();
getLast(): T getLast(): T
Obtains the last entry in this container. Obtains the last element in this container.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| T | Returns the entry if obtained; returns **undefined** otherwise.| | T | Returns the element if obtained; returns **undefined** otherwise.|
**Example** **Example**
...@@ -580,9 +629,10 @@ linkedList.getLast(); ...@@ -580,9 +629,10 @@ linkedList.getLast();
[Symbol.iterator]\(): IterableIterator&lt;T&gt; [Symbol.iterator]\(): IterableIterator&lt;T&gt;
Obtains an iterator, each item of which is a JavaScript object. Obtains an iterator, each item of which is a JavaScript object.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
| Type| Description| | Type| Description|
...@@ -600,14 +650,14 @@ linkedList.add(4); ...@@ -600,14 +650,14 @@ linkedList.add(4);
// Method 1: // Method 1:
for (let item of linkedList) { for (let item of linkedList) {
console.log(item); console.log("value:" + item);
} }
// Method 2: // Method 2:
let iter = linkedList[Symbol.iterator](); let iter = linkedList[Symbol.iterator]();
let temp = iter.next().value; let temp = iter.next().value;
while(temp != undefined) { while(temp != undefined) {
console.log(temp); console.log("value:" + temp);
temp = iter.next().value; temp = iter.next().value;
} }
``` ```
# Linear Container List # Linear Container List
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br/> > **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. > 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.
**List** is implemented based on the singly linked list. Each node has a reference pointing to the next element. When querying an element, the system traverses the list from the beginning. **List** offers efficient insertion and removal operations but supports low query efficiency. **List** allows null elements.
Unlike [LinkedList](js-apis-linkedlist.md), which is a doubly linked list, **List** is a singly linked list that does not support insertion or removal at both ends.
**Recommended use case**: Use **List** for frequent insertion and removal operations.
## Modules to Import ## Modules to Import
```ts ```ts
import List from '@ohos.util.List' import List from '@ohos.util.List';
``` ```
## System Capabilities
SystemCapability.Utils.Lang
## List ## List
### Attributes ### Attributes
**System capability**: SystemCapability.Utils.Lang
| Name| Type| Readable| Writable| Description| | Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- | -------- |
| length | number | Yes| No| Number of entries in a list (called container later).| | length | number | Yes| No| Number of elements in a list (called container later).|
### constructor ### constructor
...@@ -31,6 +34,8 @@ constructor() ...@@ -31,6 +34,8 @@ constructor()
A constructor used to create a **List** instance. A constructor used to create a **List** instance.
**System capability**: SystemCapability.Utils.Lang
**Example** **Example**
...@@ -43,19 +48,21 @@ let list = new List(); ...@@ -43,19 +48,21 @@ let list = new List();
add(element: T): boolean add(element: T): boolean
Adds an entry at the end of this container. Adds an element at the end of this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Value Type | Mandatory| Description| | Name| Value Type | Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| element | T | Yes| Element to add.| | element | T | Yes| Target element.|
**Return value** **Return value**
| Value Type | Description| | Value Type | Description|
| -------- | -------- | | -------- | -------- |
| boolean | Returns **true** if the entry is added successfully; returns **false** otherwise.| | boolean | Returns **true** if the element is added successfully; returns **false** otherwise.|
**Example** **Example**
...@@ -73,14 +80,16 @@ let result3 = list.add(false); ...@@ -73,14 +80,16 @@ let result3 = list.add(false);
insert(element: T, index: number): void insert(element: T, index: number): void
Inserts an entry at the specified position in this container. Inserts an element at the specified position in this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Value Type | Mandatory| Description| | Name| Value Type | Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| element | T | Yes| Element to insert.| | element | T | Yes| Target element.|
| index | number | Yes| Index of the position where the entry is to be inserted.| | index | number | Yes| Index of the position where the element is to be inserted.|
**Example** **Example**
...@@ -95,19 +104,21 @@ list.insert(true, 2); ...@@ -95,19 +104,21 @@ list.insert(true, 2);
has(element: T): boolean has(element: T): boolean
Checks whether this container has the specified entry. Checks whether this container has the specified element.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Value Type | Mandatory| Description| | Name| Value Type | Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to check.| | element | T | Yes| Target element.|
**Return value** **Return value**
| Value Type | Description| | Value Type | Description|
| -------- | -------- | | -------- | -------- |
| boolean | Returns **true** if the specified entry is contained; returns **false** otherwise.| | boolean | Returns **true** if the specified element is contained; returns **false** otherwise.|
**Example** **Example**
...@@ -122,19 +133,21 @@ let result1 = list.has("Ahfbrgrbgnutfodgorrogorg"); ...@@ -122,19 +133,21 @@ let result1 = list.has("Ahfbrgrbgnutfodgorrogorg");
get(index: number): T get(index: number): T
Obtains the entry at the specified position in this container. Obtains the element at the specified position in this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Value Type | Mandatory| Description| | Name| Value Type | Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| index | number | Yes| Position index of the entry to obtain.| | index | number | Yes| Position index of the target element.|
**Return value** **Return value**
| Value Type | Description| | Value Type | Description|
| -------- | -------- | | -------- | -------- |
| T | Entry obtained.| | T | Element obtained.|
**Example** **Example**
...@@ -154,19 +167,21 @@ let result = list.get(2); ...@@ -154,19 +167,21 @@ let result = list.get(2);
getLastIndexOf(element: T): number getLastIndexOf(element: T): number
Obtains the index of the last occurrence of the specified entry in this container. Obtains the index of the last occurrence of the specified element in this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Value Type | Mandatory| Description| | Name| Value Type | Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to obtain.| | element | T | Yes| Target element.|
**Return value** **Return value**
| Value Type | Description| | Value Type | Description|
| -------- | -------- | | -------- | -------- |
| number | Returns the position index if obtained; returns **-1** if the specified entry is not found.| | number | Returns the position index if obtained; returns **-1** otherwise.|
**Example** **Example**
...@@ -186,19 +201,21 @@ let result = list.getLastIndexOf(2); ...@@ -186,19 +201,21 @@ let result = list.getLastIndexOf(2);
getIndexOf(element: T): number getIndexOf(element: T): number
Obtains the index of the first occurrence of the specified entry in this container. Obtains the index of the first occurrence of the specified element in this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Value Type | Mandatory| Description| | Name| Value Type | Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to obtain.| | element | T | Yes| Target element.|
**Return value** **Return value**
| Value Type | Description| | Value Type | Description|
| -------- | -------- | | -------- | -------- |
| number | Returns the position index if obtained; returns **-1** if the specified entry is not found.| | number | Returns the position index if obtained; returns **-1** otherwise.|
**Example** **Example**
...@@ -221,6 +238,8 @@ equal(obj: Object): boolean ...@@ -221,6 +238,8 @@ equal(obj: Object): boolean
Compares whether a specified object is equal to this container. Compares whether a specified object is equal to this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Value Type | Mandatory| Description| | Name| Value Type | Mandatory| Description|
...@@ -254,19 +273,21 @@ let result = list.equal(obj2); ...@@ -254,19 +273,21 @@ let result = list.equal(obj2);
removeByIndex(index: number): T removeByIndex(index: number): T
Removes an entry at the specified position from this container. Removes an element at the specified position from this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Value Type | Mandatory| Description| | Name| Value Type | Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| index | number | Yes| Position index of the entry to remove.| | index | number | Yes| Position index of the target element.|
**Return value** **Return value**
| Value Type | Description| | Value Type | Description|
| -------- | -------- | | -------- | -------- |
| T | Entry removed.| | T | Element removed.|
**Example** **Example**
...@@ -284,19 +305,21 @@ let result = list.removeByIndex(2); ...@@ -284,19 +305,21 @@ let result = list.removeByIndex(2);
remove(element: T): boolean remove(element: T): boolean
Removes the first occurrence of the specified entry from this container. Removes the first occurrence of the specified element from this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Value Type | Mandatory| Description| | Name| Value Type | Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to remove.| | element | T | Yes| Target element.|
**Return value** **Return value**
| Value Type | Description| | Value Type | Description|
| -------- | -------- | | -------- | -------- |
| boolean | Returns **true** if the entry is removed successfully; returns **false** otherwise.| | boolean | Returns **true** if the element is removed successfully; returns **false** otherwise.|
**Example** **Example**
...@@ -314,7 +337,9 @@ let result = list.remove(2); ...@@ -314,7 +337,9 @@ let result = list.remove(2);
replaceAllElements(callbackfn: (value: T, index?: number, list?: List&lt;T&gt;) => T, replaceAllElements(callbackfn: (value: T, index?: number, list?: List&lt;T&gt;) => T,
thisArg?: Object): void thisArg?: Object): void
Replaces all entries in this container with new entries, and returns the new ones. Replaces all elements in this container with new elements, and returns the new ones.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
...@@ -327,8 +352,8 @@ callbackfn ...@@ -327,8 +352,8 @@ callbackfn
| Name| Value Type | Mandatory| Description| | Name| Value Type | Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| value | T | Yes| Value of the entry that is currently traversed.| | value | T | Yes| Value of the element that is currently traversed.|
| index | number | No| Position index of the entry that is currently traversed.| | index | number | No| Position index of the element that is currently traversed.|
| list | List&lt;T&gt; | No| Instance that invokes the **replaceAllElements** method.| | list | List&lt;T&gt; | No| Instance that invokes the **replaceAllElements** method.|
**Example** **Example**
...@@ -352,21 +377,23 @@ list.replaceAllElements((value, index) => { ...@@ -352,21 +377,23 @@ list.replaceAllElements((value, index) => {
forEach(callbackfn: (value: T, index?: number, List?: List&lt;T&gt;) => void, forEach(callbackfn: (value: T, index?: number, List?: List&lt;T&gt;) => void,
thisArg?: Object): void thisArg?: Object): void
Uses a callback to traverse the entries in this container and obtain their position indexes. Uses a callback to traverse the elements in this container and obtain their position indexes.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Value Type | Mandatory| Description| | Name| Value Type | Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| callbackfn | function | Yes| Callback invoked to traverse the entries in the container.| | callbackfn | function | Yes| Callback invoked to traverse the elements in the container.|
| thisArg | Object | No| Value to use when the callback is invoked.| | thisArg | Object | No| Value to use when the callback is invoked.|
callbackfn callbackfn
| Name| Value Type | Mandatory| Description| | Name| Value Type | Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| value | T | Yes| Value of the entry that is currently traversed.| | value | T | Yes| Value of the element that is currently traversed.|
| index | number | No| Position index of the entry that is currently traversed.| | index | number | No| Position index of the element that is currently traversed.|
| List | List&lt;T&gt; | No| Instance that invokes the **forEach** method.| | List | List&lt;T&gt; | No| Instance that invokes the **forEach** method.|
**Example** **Example**
...@@ -378,7 +405,7 @@ list.add(4); ...@@ -378,7 +405,7 @@ list.add(4);
list.add(5); list.add(5);
list.add(4); list.add(4);
list.forEach((value, index) => { list.forEach((value, index) => {
console.log(value, index); console.log("value: " + value, index);
}); });
``` ```
...@@ -387,20 +414,22 @@ list.forEach((value, index) => { ...@@ -387,20 +414,22 @@ list.forEach((value, index) => {
sort(comparator: (firstValue: T, secondValue: T) => number): void sort(comparator: (firstValue: T, secondValue: T) => number): void
Sorts entries in this container. Sorts elements in this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name | Type | Mandatory | Description | | Name| Value Type | Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| comparator | function | Yes | Callback invoked for sorting. | | comparator | function | Yes| Callback invoked for sorting.|
comparator comparator
| Name | Type | Mandatory | Description | | Name| Value Type | Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| firstValue | T | Yes | Previous entry. | | firstValue | T | Yes| Previous element.|
| secondValue | T | Yes | Next entry. | | secondValue | T | Yes| Next element.|
**Example** **Example**
...@@ -410,15 +439,17 @@ list.add(2); ...@@ -410,15 +439,17 @@ list.add(2);
list.add(4); list.add(4);
list.add(5); list.add(5);
list.add(4); list.add(4);
list.sort(a, (b => a - b)); list.sort((a, b) => a - b);
list.sort(a, (b => b - a)); list.sort((a, b) => b - a);
``` ```
### getSubList ### getSubList
getSubList(fromIndex: number, toIndex: number): List&lt;T&gt; getSubList(fromIndex: number, toIndex: number): List&lt;T&gt;
Obtains entries within a range in this container, including the entry at the start position but not that at the end position, and returns these entries as a new **List** instance. Obtains elements within a range in this container, including the element at the start position but not that at the end position, and returns these elements as a new **List** instance.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
...@@ -452,6 +483,8 @@ clear(): void ...@@ -452,6 +483,8 @@ clear(): void
Clears this container and sets its length to **0**. Clears this container and sets its length to **0**.
**System capability**: SystemCapability.Utils.Lang
**Example** **Example**
```ts ```ts
...@@ -467,20 +500,22 @@ list.clear(); ...@@ -467,20 +500,22 @@ list.clear();
set(index: number, element: T): T set(index: number, element: T): T
Replaces an entry at the specified position in this container with a given entry. Replaces an element at the specified position in this container with a given element.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Value Type | Mandatory| Description| | Name| Value Type | Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| index | number | Yes| Position index of the entry to replace.| | index | number | Yes| Position index of the target element.|
| element | T | Yes| Entry to be used for replacement.| | element | T | Yes| Element to be used for replacement.|
**Return value** **Return value**
| Value Type | Description| | Value Type | Description|
| -------- | -------- | | -------- | -------- |
| T | New entry.| | T | New element.|
**Example** **Example**
...@@ -500,11 +535,13 @@ convertToArray(): Array&lt;T&gt; ...@@ -500,11 +535,13 @@ convertToArray(): Array&lt;T&gt;
Converts this container into an array. Converts this container into an array.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
| Type | Description | | Value Type | Description|
| -------- | -------- | | -------- | -------- |
| Array&lt;T&gt; | Array obtained. | | Array&lt;T&gt; | Array obtained.|
**Example** **Example**
...@@ -521,7 +558,9 @@ let result = list.convertToArray(); ...@@ -521,7 +558,9 @@ let result = list.convertToArray();
isEmpty(): boolean isEmpty(): boolean
Checks whether this container is empty (contains no entry). Checks whether this container is empty (contains no element).
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
...@@ -544,13 +583,15 @@ let result = list.isEmpty(); ...@@ -544,13 +583,15 @@ let result = list.isEmpty();
getFirst(): T getFirst(): T
Obtains the first entry in this container. Obtains the first element in this container.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
| Value Type | Description| | Value Type | Description|
| -------- | -------- | | -------- | -------- |
| T | The first entry obtained.| | T | The first element obtained.|
**Example** **Example**
...@@ -567,13 +608,15 @@ let result = list.getFirst(); ...@@ -567,13 +608,15 @@ let result = list.getFirst();
getLast(): T getLast(): T
Obtains the last entry in this container. Obtains the last element in this container.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
| Value Type | Description| | Value Type | Description|
| -------- | -------- | | -------- | -------- |
| T | The last entry obtained.| | T | The last element obtained.|
**Example** **Example**
...@@ -590,9 +633,10 @@ let result = list.getLast(); ...@@ -590,9 +633,10 @@ let result = list.getLast();
[Symbol.iterator]\(): IterableIterator&lt;T&gt; [Symbol.iterator]\(): IterableIterator&lt;T&gt;
Obtains an iterator, each item of which is a JavaScript object. Obtains an iterator, each item of which is a JavaScript object.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
| Value Type | Description| | Value Type | Description|
...@@ -610,14 +654,14 @@ list.add(4); ...@@ -610,14 +654,14 @@ list.add(4);
// Method 1: // Method 1:
for (let item of list) { for (let item of list) {
console.log(item); console.log("value: " + item);
} }
// Method 2: // Method 2:
let iter = list[Symbol.iterator](); let iter = list[Symbol.iterator]();
let temp = iter.next().value; let temp = iter.next().value;
while(temp != undefined) { while(temp != undefined) {
console.log(temp); console.log("value: " + temp);
temp = iter.next().value; temp = iter.next().value;
} }
``` ```
# Nonlinear Container PlainArray # Nonlinear Container PlainArray
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br/> > **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. > 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.
**PlainArray** stores key-value (KV) pairs. Each key must be unique, be of the number type, and have only one value.
**PlainArray** is based on generics and uses a lightweight structure. Keys in the array are searched using binary search, which map to values in other arrays.
Both **PlainArray** and **[LightWeightMap](js-apis-lightweightmap.md)** are used to store KV pairs in the lightweight structure. However, the key type of **PlainArray** can only be **number**.
**Recommended use case**: Use **PlainArray** when you need to store KV pairs whose keys are of the **number** type.
## Modules to Import ## Modules to Import
```ts ```ts
import PlainArray from '@ohos.util.PlainArray' import PlainArray from '@ohos.util.PlainArray';
``` ```
## System Capability
SystemCapability.Utils.Lang
## PlainArray ## PlainArray
### Attributes ### Attributes
**System capability**: SystemCapability.Utils.Lang
| Name| Type| Readable| Writable| Description| | Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- | -------- |
| length | number | Yes| No| Number of entries in a plain array (called container later).| | length | number | Yes| No| Number of elements in a plain array (called container later).|
### constructor ### constructor
...@@ -30,6 +37,8 @@ constructor() ...@@ -30,6 +37,8 @@ constructor()
A constructor used to create a **PlainArray** instance. A constructor used to create a **PlainArray** instance.
**System capability**: SystemCapability.Utils.Lang
**Example** **Example**
```ts ```ts
...@@ -43,6 +52,8 @@ isEmpty(): boolean ...@@ -43,6 +52,8 @@ isEmpty(): boolean
Checks whether this container is empty. Checks whether this container is empty.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
| Type| Description| | Type| Description|
...@@ -63,11 +74,13 @@ has(key: number): boolean ...@@ -63,11 +74,13 @@ has(key: number): boolean
Checks whether this container contains the specified key. Checks whether this container contains the specified key.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type | Mandatory| Description| | Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| key | number | Yes| Key to check.| | key | number | Yes| Target key.|
**Return value** **Return value**
...@@ -91,11 +104,13 @@ get(key: number): T ...@@ -91,11 +104,13 @@ get(key: number): T
Obtains the value of the specified key in this container. Obtains the value of the specified key in this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type | Mandatory| Description| | Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| key | number | Yes| Key to query.| | key | number | Yes| Target key.|
**Return value** **Return value**
...@@ -117,19 +132,21 @@ let result = plainArray.get(1); ...@@ -117,19 +132,21 @@ let result = plainArray.get(1);
getIndexOfKey(key: number): number getIndexOfKey(key: number): number
Obtains the index of the first occurrence of an entry with the specified key in this container. Obtains the index of the first occurrence of an element with the specified key in this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type | Mandatory| Description| | Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| key | number | Yes| Key of the entry to obtain.| | key | number | Yes| Target key.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| number | Returns the position index if obtained; returns **-1** if the specified entry is not found.| | number | Returns the position index if obtained; returns **-1** otherwise.|
**Example** **Example**
...@@ -137,7 +154,7 @@ Obtains the index of the first occurrence of an entry with the specified key in ...@@ -137,7 +154,7 @@ Obtains the index of the first occurrence of an entry with the specified key in
let plainArray = new PlainArray(); let plainArray = new PlainArray();
plainArray.add(1, "sddfhf"); plainArray.add(1, "sddfhf");
plainArray.add(2, "sffdfhf"); plainArray.add(2, "sffdfhf");
let result = plainArray.getIndexOfKey("sdfs"); let result = plainArray.getIndexOfKey(2);
``` ```
...@@ -145,19 +162,21 @@ let result = plainArray.getIndexOfKey("sdfs"); ...@@ -145,19 +162,21 @@ let result = plainArray.getIndexOfKey("sdfs");
getIndexOfValue(value: T): number getIndexOfValue(value: T): number
Obtains the index of the first occurrence of an entry with the specified value in this container. Obtains the index of the first occurrence of an element with the specified value in this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type | Mandatory| Description| | Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| value | T | Yes| Value of the entry to obtain.| | value | T | Yes| Value of the target element.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| number | Returns the position index if obtained; returns **-1** if the specified entry is not found.| | number | Returns the position index if obtained; returns **-1** otherwise.|
**Example** **Example**
...@@ -173,19 +192,21 @@ let result = plainArray.getIndexOfValue("sddfhf"); ...@@ -173,19 +192,21 @@ let result = plainArray.getIndexOfValue("sddfhf");
getKeyAt(index: number): number getKeyAt(index: number): number
Obtains the key of the entry at the specified position in this container. Obtains the key of the element at the specified position in this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type | Mandatory| Description| | Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| index | number | Yes| Position index of the entry to obtain.| | index | number | Yes| Position index of the target element.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| number | Returns the key of the entry if obtained; returns **-1** otherwise.| | number | Returns the key of the element if obtained; returns **-1** otherwise.|
**Example** **Example**
...@@ -200,19 +221,21 @@ let result = plainArray.getKeyAt(1); ...@@ -200,19 +221,21 @@ let result = plainArray.getKeyAt(1);
getValueAt(index: number): T getValueAt(index: number): T
Obtains the value of an entry at the specified position in this container. Obtains the value of an element at the specified position in this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type | Mandatory| Description| | Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| index | number | Yes| Position index of the entry to obtain.| | index | number | Yes| Position index of the target element.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| T | Returns the value of the entry if obtained; returns **undefined** otherwise.| | T | Returns the value of the element if obtained; returns **undefined** otherwise.|
**Example** **Example**
...@@ -229,6 +252,8 @@ clone(): PlainArray&lt;T&gt; ...@@ -229,6 +252,8 @@ clone(): PlainArray&lt;T&gt;
Clones this container and returns a copy. The modification to the copy does not affect the original instance. Clones this container and returns a copy. The modification to the copy does not affect the original instance.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
| Type| Description| | Type| Description|
...@@ -249,14 +274,16 @@ let newPlainArray = plainArray.clone(); ...@@ -249,14 +274,16 @@ let newPlainArray = plainArray.clone();
add(key: number, value: T): void add(key: number, value: T): void
Adds an entry to this container. Adds an element to this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type | Mandatory| Description| | Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| key | number | Yes| Key of the entry to add.| | key | number | Yes| Key of the target element.|
| value | T | Yes| Value of the entry to add.| | value | T | Yes| Value of the target element.|
**Example** **Example**
...@@ -270,19 +297,21 @@ plainArray.add(1, "sddfhf"); ...@@ -270,19 +297,21 @@ plainArray.add(1, "sddfhf");
remove(key: number): T remove(key: number): T
Removes an entry with the specified key from this container. Removes an element with the specified key from this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type | Mandatory| Description| | Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| key | number | Yes| Key of the entry to remove.| | key | number | Yes| Target key.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| T | Value of the entry removed.| | T | Value of the element removed.|
**Example** **Example**
...@@ -299,19 +328,21 @@ let result = plainArray.remove(2); ...@@ -299,19 +328,21 @@ let result = plainArray.remove(2);
removeAt(index: number): T removeAt(index: number): T
Removes an entry at the specified position from this container. Removes an element at the specified position from this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type | Mandatory| Description| | Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| index | number | Yes| Position index of the entry to remove.| | index | number | Yes| Position index of the target element.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| T | Entry removed.| | T | Element removed.|
**Example** **Example**
...@@ -328,20 +359,22 @@ let result = plainArray.removeAt(1); ...@@ -328,20 +359,22 @@ let result = plainArray.removeAt(1);
removeRangeFrom(index: number, size: number): number removeRangeFrom(index: number, size: number): number
Removes entries in a specified range from this container. Removes elements in a specified range from this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type | Mandatory| Description| | Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| index | number | Yes| Start position of the entries to remove.| | index | number | Yes| Start position of the elements to remove.|
| size | number | Yes| Number of entries to remove.| | size | number | Yes| Number of elements to remove.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| number | Number of entries removed.| | number | Number of elements removed.|
**Example** **Example**
...@@ -357,14 +390,16 @@ let result = plainArray.removeRangeFrom(1, 3); ...@@ -357,14 +390,16 @@ let result = plainArray.removeRangeFrom(1, 3);
setValueAt(index: number, value: T): void setValueAt(index: number, value: T): void
Sets a value for an entry at the specified position in this container. Sets a value for an element at the specified position in this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type | Mandatory| Description| | Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| index | number | Yes| Position index of the entry.| | index | number | Yes| Position index of the target element.|
| value | T | Yes| Value of the entry to set.| | value | T | Yes| Value of the target element.|
**Example** **Example**
...@@ -380,7 +415,9 @@ plainArray.setValueAt(1, 3546); ...@@ -380,7 +415,9 @@ plainArray.setValueAt(1, 3546);
toString(): String toString(): String
Obtains a string that contains all entries in this container. Obtains a string that contains all elements in this container.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
...@@ -404,6 +441,8 @@ clear(): void ...@@ -404,6 +441,8 @@ clear(): void
Clears this container and sets its length to **0**. Clears this container and sets its length to **0**.
**System capability**: SystemCapability.Utils.Lang
**Example** **Example**
```ts ```ts
...@@ -418,20 +457,22 @@ plainArray.clear(); ...@@ -418,20 +457,22 @@ plainArray.clear();
forEach(callbackfn: (value: T, index?: number, PlainArray?: PlainArray&lt;T&gt;) => void, thisArg?: Object): void forEach(callbackfn: (value: T, index?: number, PlainArray?: PlainArray&lt;T&gt;) => void, thisArg?: Object): void
Uses a callback to traverse the entries in this container and obtain their position indexes. Uses a callback to traverse the elements in this container and obtain their position indexes.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type | Mandatory| Description| | Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| callbackfn | function | Yes| Callback invoked to traverse the entries in the container.| | callbackfn | function | Yes| Callback invoked to traverse the elements in the container.|
| thisArg | Object | No| Value to use when the callback is invoked.| | thisArg | Object | No| Value to use when the callback is invoked.|
callbackfn callbackfn
| Name| Type | Mandatory| Description| | Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| value | T | Yes| Value of the entry that is currently traversed.| | value | T | Yes| Value of the element that is currently traversed.|
| index | number | No| Key of the entry that is currently traversed.| | index | number | No| Key of the element that is currently traversed.|
| PlainArray | PlainArray&lt;T&gt;| No| Instance that invokes the **forEach** API.| | PlainArray | PlainArray&lt;T&gt;| No| Instance that invokes the **forEach** API.|
**Example** **Example**
...@@ -440,8 +481,8 @@ callbackfn ...@@ -440,8 +481,8 @@ callbackfn
let plainArray = new PlainArray(); let plainArray = new PlainArray();
plainArray.add(1, "sddfhf"); plainArray.add(1, "sddfhf");
plainArray.add(2, "sffdfhf"); plainArray.add(2, "sffdfhf");
plainArray.forEach((value, key) => { plainArray.forEach((value, index) => {
console.log(value, key); console.log("value:" + value, index);
}); });
``` ```
...@@ -452,6 +493,8 @@ plainArray.forEach((value, key) => { ...@@ -452,6 +493,8 @@ plainArray.forEach((value, key) => {
Obtains an iterator, each item of which is a JavaScript object. Obtains an iterator, each item of which is a JavaScript object.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
| Type| Description| | Type| Description|
...@@ -467,16 +510,16 @@ plainArray.add(2, "sffdfhf"); ...@@ -467,16 +510,16 @@ plainArray.add(2, "sffdfhf");
// Method 1: // Method 1:
for (let item of plainArray) { for (let item of plainArray) {
console.log("index: " + item[0]); console.log("index:" + item[0]);
console.log("value: " + item[1]); console.log("value:" + item[1]);
} }
// Method 2: // Method 2:
let iter = plainArray[Symbol.iterator](); let iter = plainArray[Symbol.iterator]();
let temp = iter.next().value; let temp = iter.next().value;
while(temp != undefined) { while(temp != undefined) {
console.log(temp[0]); console.log("index:" + temp[0]);
console.log(temp[1]); console.log("value:" + temp[1]);
temp = iter.next().value; temp = iter.next().value;
} }
``` ```
# Linear Container Queue # Linear Container Queue
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br/> > **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. > 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](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.
**Recommended use case**: Use **Queue** in FIFO scenarios.
## Modules to Import ## Modules to Import
```ts ```ts
import Queue from '@ohos.util.Queue' import Queue from '@ohos.util.Queue';
``` ```
## System Capabilities
SystemCapability.Utils.Lang
## Queue ## Queue
### Attributes ### Attributes
**System capability**: SystemCapability.Utils.Lang
| Name| Type| Readable| Writable| Description| | Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- | -------- |
| length | number | Yes| No| Number of entries in a queue (called container later).| | length | number | Yes| No| Number of elements in a queue (called container later).|
### constructor ### constructor
...@@ -31,6 +34,8 @@ constructor() ...@@ -31,6 +34,8 @@ constructor()
A constructor used to create a **Queue** instance. A constructor used to create a **Queue** instance.
**System capability**: SystemCapability.Utils.Lang
**Example** **Example**
```ts ```ts
...@@ -42,19 +47,21 @@ let queue = new Queue(); ...@@ -42,19 +47,21 @@ let queue = new Queue();
add(element: T): boolean add(element: T): boolean
Adds an entry at the end of this container. Adds an element at the end of this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to add.| | element | T | Yes| Target element.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| boolean | Returns **true** if the entry is added successfully; returns **false** otherwise.| | boolean | Returns **true** if the element is added successfully; returns **false** otherwise.|
**Example** **Example**
...@@ -73,13 +80,15 @@ let result3 = queue.add(c); ...@@ -73,13 +80,15 @@ let result3 = queue.add(c);
pop(): T pop(): T
Removes the first entry from this container. Removes the first element from this container.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| T | Entry removed.| | T | Element removed.|
**Example** **Example**
...@@ -97,13 +106,15 @@ let result = queue.pop(); ...@@ -97,13 +106,15 @@ let result = queue.pop();
getFirst(): T getFirst(): T
Obtains the first entry of this container. Obtains the first element of this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| T | The first entry obtained.| | T | The first element obtained.|
**Example** **Example**
...@@ -121,21 +132,23 @@ let result = queue.getFirst(); ...@@ -121,21 +132,23 @@ let result = queue.getFirst();
forEach(callbackfn: (value: T, index?: number, Queue?: Queue&lt;T&gt;) => void, forEach(callbackfn: (value: T, index?: number, Queue?: Queue&lt;T&gt;) => void,
thisArg?: Object): void thisArg?: Object): void
Uses a callback to traverse the entries in this container and obtain their position indexes. Uses a callback to traverse the elements in this container and obtain their position indexes.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| callbackfn | function | Yes| Callback invoked to traverse the entries in the container.| | callbackfn | function | Yes| Callback invoked to traverse the elements in the container.|
| thisArg | Object | No| Value to use when the callback is invoked.| | thisArg | Object | No| Value to use when the callback is invoked.|
callbackfn callbackfn
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| value | T | Yes| Value of the entry that is currently traversed.| | value | T | Yes| Value of the element that is currently traversed.|
| index | number | No| Position index of the entry that is currently traversed.| | index | number | No| Position index of the element that is currently traversed.|
| Queue | Queue&lt;T&gt; | No| Instance that invokes the **forEach** method.| | Queue | Queue&lt;T&gt; | No| Instance that invokes the **forEach** method.|
**Example** **Example**
...@@ -147,7 +160,7 @@ queue.add(4); ...@@ -147,7 +160,7 @@ queue.add(4);
queue.add(5); queue.add(5);
queue.add(4); queue.add(4);
queue.forEach((value, index) => { queue.forEach((value, index) => {
console.log(value, index); console.log("value:" + value, index);
}); });
``` ```
...@@ -156,14 +169,15 @@ queue.forEach((value, index) => { ...@@ -156,14 +169,15 @@ queue.forEach((value, index) => {
[Symbol.iterator]\(): IterableIterator&lt;T&gt; [Symbol.iterator]\(): IterableIterator&lt;T&gt;
Obtains an iterator, each item of which is a JavaScript object. Obtains an iterator, each item of which is a JavaScript object.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
| Type | Description | | Type| Description|
| -------- | -------- | | -------- | -------- |
| IterableIterator&lt;T&gt; | Iterator obtained. | | IterableIterator&lt;T&gt; | Iterator obtained.|
**Example** **Example**
```ts ```ts
...@@ -175,14 +189,14 @@ queue.add(4); ...@@ -175,14 +189,14 @@ queue.add(4);
// Method 1: // Method 1:
for (let item of queue) { for (let item of queue) {
console.log(item); console.log("value:" + item);
} }
// Method 2: // Method 2:
let iter = queue[Symbol.iterator](); let iter = queue[Symbol.iterator]();
let temp = iter.next().value; let temp = iter.next().value;
while(temp != undefined) { while(temp != undefined) {
console.log(temp); console.log("value:" + temp);
temp = iter.next().value; temp = iter.next().value;
} }
``` ```
# Linear Container Stack # Linear Container Stack
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br/> > **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. > 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.
**Stack** is implemented based on the array data structure. It follows the principle Last Out First In (LOFI) and supports data insertion and removal at one end.
Unlike **[Queue](js-apis-queue.md)**, which is implemented based on the queue data structure and supports insertion at one end and removal at the other end, **Stack** supports insertion and removal at the same end.
**Recommended use case**: Use **Stack** in LOFI scenarios.
## Modules to Import ## Modules to Import
```ts ```ts
import Stack from '@ohos.util.Stack' import Stack from '@ohos.util.Stack';
``` ```
## System Capabilities
SystemCapability.Utils.Lang
## Stack ## Stack
### Attributes ### Attributes
**System capability**: SystemCapability.Utils.Lang
| Name| Type| Readable| Writable| Description| | Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- | -------- |
| length | number | Yes| No| Number of entries in a stack (called container later).| | length | number | Yes| No| Number of elements in a stack (called container later).|
### constructor ### constructor
...@@ -31,6 +36,8 @@ constructor() ...@@ -31,6 +36,8 @@ constructor()
A constructor used to create a **Stack** instance. A constructor used to create a **Stack** instance.
**System capability**: SystemCapability.Utils.Lang
**Example** **Example**
```ts ```ts
...@@ -42,13 +49,15 @@ let stack = new Stack(); ...@@ -42,13 +49,15 @@ let stack = new Stack();
push(item: T): T push(item: T): T
Adds an entry at the top of this container. Adds an element at the top of this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| item | T | Yes| Element to add.| | item | T | Yes| Target element.|
**Return value** **Return value**
...@@ -72,13 +81,15 @@ let result3 = stack.push(c); ...@@ -72,13 +81,15 @@ let result3 = stack.push(c);
pop(): T pop(): T
Removes the top entry from this container. Removes the top element from this container.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| T | Entry removed.| | T | Element removed.|
**Example** **Example**
...@@ -96,13 +107,15 @@ let result = stack.pop(); ...@@ -96,13 +107,15 @@ let result = stack.pop();
peek(): T peek(): T
Obtains the top entry of this container. Obtains the top element of this container.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| T | Entry obtained.| | T | Element obtained.|
**Example** **Example**
...@@ -119,19 +132,21 @@ let result = stack.peek(); ...@@ -119,19 +132,21 @@ let result = stack.peek();
locate(element: T): number locate(element: T): number
Obtains the index of the first occurrence of the specified entry in this container. Obtains the index of the first occurrence of the specified element in this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to query.| | element | T | Yes| Target element.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| number | Returns the position index if obtained; returns **-1** if the specified entry is not found.| | number | Returns the position index if obtained; returns **-1** otherwise.|
**Example** **Example**
...@@ -149,21 +164,23 @@ let result = stack.locate(2); ...@@ -149,21 +164,23 @@ let result = stack.locate(2);
forEach(callbackfn: (value: T, index?: number, stack?: Stack&lt;T&gt;) => void, forEach(callbackfn: (value: T, index?: number, stack?: Stack&lt;T&gt;) => void,
thisArg?: Object): void thisArg?: Object): void
Uses a callback to traverse the entries in this container and obtain their position indexes. Uses a callback to traverse the elements in this container and obtain their position indexes.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| callbackfn | function | Yes| Callback invoked to traverse the entries in the container.| | callbackfn | function | Yes| Callback invoked to traverse the elements in the container.|
| thisArg | Object | No| Value to use when the callback is invoked.| | thisArg | Object | No| Value to use when the callback is invoked.|
callbackfn callbackfn
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| value | T | Yes| Value of the entry that is currently traversed.| | value | T | Yes| Value of the element that is currently traversed.|
| index | number | No| Position index of the entry that is currently traversed.| | index | number | No| Position index of the element that is currently traversed.|
| stack | Stack&lt;T&gt; | No| Instance that invokes the **forEach** method.| | stack | Stack&lt;T&gt; | No| Instance that invokes the **forEach** method.|
**Example** **Example**
...@@ -175,7 +192,7 @@ stack.push(4); ...@@ -175,7 +192,7 @@ stack.push(4);
stack.push(5); stack.push(5);
stack.push(4); stack.push(4);
stack.forEach((value, index) => { stack.forEach((value, index) => {
console.log(value, index); console.log("value:" + value, index);
}); });
``` ```
...@@ -183,7 +200,9 @@ stack.forEach((value, index) => { ...@@ -183,7 +200,9 @@ stack.forEach((value, index) => {
isEmpty(): boolean isEmpty(): boolean
Checks whether this container is empty (contains no entries). Checks whether this container is empty (contains no elements).
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
...@@ -206,9 +225,10 @@ let result = stack.isEmpty(); ...@@ -206,9 +225,10 @@ let result = stack.isEmpty();
[Symbol.iterator]\(): IterableIterator&lt;T&gt; [Symbol.iterator]\(): IterableIterator&lt;T&gt;
Obtains an iterator, each item of which is a JavaScript object. Obtains an iterator, each item of which is a JavaScript object.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
| Type| Description| | Type| Description|
...@@ -225,14 +245,14 @@ stack.push(4); ...@@ -225,14 +245,14 @@ stack.push(4);
// Method 1: // Method 1:
for (let item of stack) { for (let item of stack) {
console.log(item); console.log("value:" + item);
} }
// Method 2: // Method 2:
let iter = stack[Symbol.iterator](); let iter = stack[Symbol.iterator]();
let temp = iter.next().value; let temp = iter.next().value;
while(temp != undefined) { while(temp != undefined) {
console.log(temp); console.log("value:" + temp);
temp = iter.next().value; temp = iter.next().value;
} }
``` ```
# Nonlinear Container TreeMap # Nonlinear Container TreeMap
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br/> > **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. > 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.
**TreeMap** stores key-value (KV) pairs. Each key must be unique and have only one value.
**TreeMap** is implemented using a red-black tree, which is a binary search tree where keys are stored in sorted order for efficient insertion and removal.
**[HashMap](js-apis-treemap.md)** is faster in accessing data than **TreeMap**, because the former accesses data based on the hash code of the key, whereas the latter stores and accesses the keys in sorted order.
Recommended use case: Use **TreeMap** when you need to store KV pairs in sorted order.
## Modules to Import ## Modules to Import
```ts ```ts
import TreeMap from '@ohos.util.TreeMap' import TreeMap from '@ohos.util.TreeMap';
``` ```
## System Capabilities
SystemCapability.Utils.Lang
## TreeMap ## TreeMap
### Attributes ### Attributes
**System capability**: SystemCapability.Utils.Lang
| Name| Type| Readable| Writable| Description| | Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- | -------- |
| length | number | Yes| No| Number of entries in a tree map (called container later).| | length | number | Yes| No| Number of elements in a tree map (called container later).|
### constructor ### constructor
...@@ -30,6 +35,8 @@ constructor(comparator?:(firstValue: K, secondValue: K) => boolean) ...@@ -30,6 +35,8 @@ constructor(comparator?:(firstValue: K, secondValue: K) => boolean)
A constructor used to create a **TreeMap** instance. A constructor used to create a **TreeMap** instance.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
...@@ -47,7 +54,9 @@ let treeMap = new TreeMap(); ...@@ -47,7 +54,9 @@ let treeMap = new TreeMap();
isEmpty(): boolean isEmpty(): boolean
Checks whether this container is empty (contains no entry). Checks whether this container is empty (contains no element).
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
...@@ -69,11 +78,13 @@ hasKey(key: K): boolean ...@@ -69,11 +78,13 @@ hasKey(key: K): boolean
Checks whether this container has the specified key. Checks whether this container has the specified key.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| key | K | Yes| Key to check.| | key | K | Yes| Target key.|
**Return value** **Return value**
...@@ -97,11 +108,13 @@ hasValue(value: V): boolean ...@@ -97,11 +108,13 @@ hasValue(value: V): boolean
Checks whether this container has the specified value. Checks whether this container has the specified value.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| value | V | Yes| Value to check.| | value | V | Yes| Target value.|
**Return value** **Return value**
...@@ -125,11 +138,13 @@ get(key: K): V ...@@ -125,11 +138,13 @@ get(key: K): V
Obtains the value of the specified key in this container. Obtains the value of the specified key in this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| key | K | Yes| Key to query.| | key | K | Yes| Target key.|
**Return value** **Return value**
...@@ -153,6 +168,8 @@ getFirstKey(): K ...@@ -153,6 +168,8 @@ getFirstKey(): K
Obtains the first key in this container. Obtains the first key in this container.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
| Type| Description| | Type| Description|
...@@ -175,6 +192,8 @@ getLastKey(): K ...@@ -175,6 +192,8 @@ getLastKey(): K
Obtains the last key in this container. Obtains the last key in this container.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
| Type| Description| | Type| Description|
...@@ -195,13 +214,15 @@ let result = treeMap.getLastKey(); ...@@ -195,13 +214,15 @@ let result = treeMap.getLastKey();
setAll(map: TreeMap<K, V>): void setAll(map: TreeMap<K, V>): void
Adds all entries in a **TreeMap** instance to this container. Adds all elements in a **TreeMap** instance to this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| map | TreeMap<K, V> | Yes| **TreeMap** instance whose entries are to be added to the current container.| | map | TreeMap<K, V> | Yes| **TreeMap** instance whose elements are to be added to the current container.|
**Example** **Example**
...@@ -218,20 +239,22 @@ treeMap.setAll(map); ...@@ -218,20 +239,22 @@ treeMap.setAll(map);
set(key: K, value: V): Object set(key: K, value: V): Object
Adds an entry to this container. Adds an element to this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| key | K | Yes| Key of the entry to add.| | key | K | Yes| Key of the target element.|
| value | V | Yes| Value of the entry to add.| | value | V | Yes| Value of the target element.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| Object | Container that contains the new entry.| | Object | Container that contains the new element.|
**Example** **Example**
...@@ -245,19 +268,21 @@ treeMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123); ...@@ -245,19 +268,21 @@ treeMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
remove(key: K): V remove(key: K): V
Removes the entry with the specified key from this container. Removes the element with the specified key from this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| key | K | Yes| Key of the entry to remove.| | key | K | Yes| Target key.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| V | Value of the entry removed.| | V | Value of the element removed.|
**Example** **Example**
...@@ -275,6 +300,8 @@ getLowerKey(key: K): K ...@@ -275,6 +300,8 @@ getLowerKey(key: K): K
Obtains the key that is placed in front of the input key in this container. Obtains the key that is placed in front of the input key in this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
...@@ -304,6 +331,8 @@ getHigherKey(key: K): K ...@@ -304,6 +331,8 @@ getHigherKey(key: K): K
Obtains the key that is placed next to the input key in this container. Obtains the key that is placed next to the input key in this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
...@@ -330,20 +359,22 @@ let result = treeMap.getHigherKey("sdfs"); ...@@ -330,20 +359,22 @@ let result = treeMap.getHigherKey("sdfs");
replace(key: K, newValue: V): boolean replace(key: K, newValue: V): boolean
Replaces an entry in this container. Replaces an element in this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| key | K | Yes| Key of the entry to replace.| | key | K | Yes| Key of the target element.|
| newValue | V | Yes| New value of the entry.| | newValue | V | Yes| New value of the element.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| boolean | Returns **true** if the entry is replaced successfully; returns **false** otherwise.| | boolean | Returns **true** if the element is replaced successfully; returns **false** otherwise.|
**Example** **Example**
...@@ -360,6 +391,8 @@ clear(): void ...@@ -360,6 +391,8 @@ clear(): void
Clears this container and sets its length to **0**. Clears this container and sets its length to **0**.
**System capability**: SystemCapability.Utils.Lang
**Example** **Example**
```ts ```ts
...@@ -376,6 +409,8 @@ keys(): IterableIterator&lt;K&gt; ...@@ -376,6 +409,8 @@ keys(): IterableIterator&lt;K&gt;
Obtains an iterator that contains all the keys in this container. Obtains an iterator that contains all the keys in this container.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
| Type| Description| | Type| Description|
...@@ -391,7 +426,7 @@ treeMap.set("sdfs", 356); ...@@ -391,7 +426,7 @@ treeMap.set("sdfs", 356);
let iter = treeMap.keys(); let iter = treeMap.keys();
let temp = iter.next().value; let temp = iter.next().value;
while(temp != undefined) { while(temp != undefined) {
console.log(temp); console.log("value:" + temp);
temp = iter.next().value; temp = iter.next().value;
} }
``` ```
...@@ -403,6 +438,8 @@ values(): IterableIterator&lt;V&gt; ...@@ -403,6 +438,8 @@ values(): IterableIterator&lt;V&gt;
Obtains an iterator that contains all the values in this container. Obtains an iterator that contains all the values in this container.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
| Type| Description| | Type| Description|
...@@ -418,7 +455,7 @@ treeMap.set("sdfs", 356); ...@@ -418,7 +455,7 @@ treeMap.set("sdfs", 356);
let iter = treeMap.values(); let iter = treeMap.values();
let temp = iter.next().value; let temp = iter.next().value;
while(temp != undefined) { while(temp != undefined) {
console.log(temp); console.log("value:" + temp);
temp = iter.next().value; temp = iter.next().value;
} }
``` ```
...@@ -428,20 +465,22 @@ while(temp != undefined) { ...@@ -428,20 +465,22 @@ while(temp != undefined) {
forEach(callbackfn: (value?: V, key?: K, map?: TreeMap<K, V>) => void, thisArg?: Object): void forEach(callbackfn: (value?: V, key?: K, map?: TreeMap<K, V>) => void, thisArg?: Object): void
Uses a callback to traverse the entries in this container and obtain their position indexes. Uses a callback to traverse the elements in this container and obtain their position indexes.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| callbackfn | function | Yes| Callback invoked to traverse the entries in the container.| | callbackfn | function | Yes| Callback invoked to traverse the elements in the container.|
| thisArg | Object | No| Value to use when the callback is invoked.| | thisArg | Object | No| Value to use when the callback is invoked.|
callbackfn callbackfn
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| value | V | No| Value of the entry that is currently traversed.| | value | V | No| Value of the element that is currently traversed.|
| key | K | No| Key of the entry that is currently traversed.| | key | K | No| Key of the element that is currently traversed.|
| map | TreeMap<K, V> | No| Instance that invokes the **forEach** method.| | map | TreeMap<K, V> | No| Instance that invokes the **forEach** method.|
**Example** **Example**
...@@ -451,7 +490,7 @@ let treeMap = new TreeMap(); ...@@ -451,7 +490,7 @@ let treeMap = new TreeMap();
treeMap.set("sdfs", 123); treeMap.set("sdfs", 123);
treeMap.set("dfsghsf", 357); treeMap.set("dfsghsf", 357);
treeMap.forEach((value, key) => { treeMap.forEach((value, key) => {
console.log(value, key); console.log("value:" + value, key);
}); });
``` ```
...@@ -460,7 +499,9 @@ treeMap.forEach((value, key) => { ...@@ -460,7 +499,9 @@ treeMap.forEach((value, key) => {
entries(): IterableIterator<[K, V]> entries(): IterableIterator<[K, V]>
Obtains an iterator that contains all the entries in this container. Obtains an iterator that contains all the elements in this container.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
...@@ -477,8 +518,8 @@ treeMap.set("sdfs", 356); ...@@ -477,8 +518,8 @@ treeMap.set("sdfs", 356);
let iter = treeMap.entries(); let iter = treeMap.entries();
let temp = iter.next().value; let temp = iter.next().value;
while(temp != undefined) { while(temp != undefined) {
console.log(temp[0]); console.log("key:" + temp[0]);
console.log(temp[1]); console.log("value:" + temp[1]);
temp = iter.next().value; temp = iter.next().value;
} }
``` ```
...@@ -488,9 +529,10 @@ while(temp != undefined) { ...@@ -488,9 +529,10 @@ while(temp != undefined) {
[Symbol.iterator]\(): IterableIterator&lt;[K, V]&gt; [Symbol.iterator]\(): IterableIterator&lt;[K, V]&gt;
Obtains an iterator, each item of which is a JavaScript object. Obtains an iterator, each item of which is a JavaScript object.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
...@@ -505,16 +547,16 @@ treeMap.set("sdfs", 356); ...@@ -505,16 +547,16 @@ treeMap.set("sdfs", 356);
// Method 1: // Method 1:
for (let item of treeMap) { for (let item of treeMap) {
console.log("key: " + item[0]); console.log("key:" + item[0]);
console.log("value: " + item[1]); console.log("value:" + item[1]);
} }
// Method 2: // Method 2:
let iter = treeMap[Symbol.iterator](); let iter = treeMap[Symbol.iterator]();
let temp = iter.next().value; let temp = iter.next().value;
while(temp != undefined) { while(temp != undefined) {
console.log(temp[0]); console.log("key:" + temp[0]);
console.log(temp[1]); console.log("value:" + temp[1]);
temp = iter.next().value; temp = iter.next().value;
} }
``` ```
# Nonlinear Container TreeSet # Nonlinear Container TreeSet
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br/> > **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. > 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.
**TreeSet** is implemented based on **[TreeMap](js-apis-treemap.md)**. In **TreeSet**, only **value** objects are processed. **TreeSet** can be used to store values, each of which must be unique.
**[HashSet](js-apis-hashset.md)** stores data in a random order, whereas **TreeSet** stores data in sorted order. Both of them allows only unique elements. However, null values are allowed in **HashSet**, but not allowed in **TreeSet**.
Recommended use case: Use **TreeSet** when you need to store data in sorted order.
## Modules to Import ## Modules to Import
```ts ```ts
import TreeSet from '@ohos.util.TreeSet' import TreeSet from '@ohos.util.TreeSet';
``` ```
## System Capabilities
SystemCapability.Utils.Lang
## TreeSet ## TreeSet
### Attributes ### Attributes
**System capability**: SystemCapability.Utils.Lang
| Name| Type| Readable| Writable| Description| | Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- | -------- |
| length | number | Yes| No| Number of entries in a tree set (called container later).| | length | number | Yes| No| Number of elements in a tree set (called container later).|
### constructor ### constructor
...@@ -30,6 +33,8 @@ constructor(comparator?:(firstValue: T, secondValue: T) => boolean) ...@@ -30,6 +33,8 @@ constructor(comparator?:(firstValue: T, secondValue: T) => boolean)
A constructor used to create a **TreeSet** instance. A constructor used to create a **TreeSet** instance.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
...@@ -47,7 +52,9 @@ let treeSet = new TreeSet(); ...@@ -47,7 +52,9 @@ let treeSet = new TreeSet();
isEmpty(): boolean isEmpty(): boolean
Checks whether this container is empty (contains no entry). Checks whether this container is empty (contains no element).
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
...@@ -69,11 +76,13 @@ has(value: T): boolean ...@@ -69,11 +76,13 @@ has(value: T): boolean
Checks whether this container has the specified value. Checks whether this container has the specified value.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| value | T | Yes| Value to query.| | value | T | Yes| Target value.|
**Return value** **Return value**
...@@ -95,7 +104,9 @@ let result1 = treeSet.has(123); ...@@ -95,7 +104,9 @@ let result1 = treeSet.has(123);
getFirstValue(): T getFirstValue(): T
Obtains the value of the first entry in this container. Obtains the value of the first element in this container.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
...@@ -117,7 +128,9 @@ let result = treeSet.getFirstValue(); ...@@ -117,7 +128,9 @@ let result = treeSet.getFirstValue();
getLastValue(): T getLastValue(): T
Obtains the value of the last entry in this container. Obtains the value of the last element in this container.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
...@@ -139,19 +152,21 @@ let result = treeSet.getLastValue(); ...@@ -139,19 +152,21 @@ let result = treeSet.getLastValue();
add(value: T): boolean add(value: T): boolean
Adds an entry to this container. Adds an element to this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| value | T | Yes| Entry to add.| | value | T | Yes| Target element.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| boolean | Returns **true** if the entry is added successfully; returns **false** otherwise.| | boolean | Returns **true** if the element is added successfully; returns **false** otherwise.|
**Example** **Example**
...@@ -165,19 +180,21 @@ let result = treeSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf"); ...@@ -165,19 +180,21 @@ let result = treeSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
remove(value: T): boolean remove(value: T): boolean
Removes the entry with the specified key from this container. Removes the element with the specified key from this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| value | T | Yes| Key of the entry to remove.| | value | T | Yes| Key of the target element.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| boolean | Returns **true** if the entry is removed successfully; returns **false** otherwise.| | boolean | Returns **true** if the element is removed successfully; returns **false** otherwise.|
**Example** **Example**
...@@ -195,6 +212,8 @@ getLowerValue(key: T): T ...@@ -195,6 +212,8 @@ getLowerValue(key: T): T
Obtains the value that is placed in front of the input key in this container. Obtains the value that is placed in front of the input key in this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
...@@ -224,6 +243,8 @@ getHigherValue(key: T): T ...@@ -224,6 +243,8 @@ getHigherValue(key: T): T
Obtains the value that is placed next to the input key in this container. Obtains the value that is placed next to the input key in this container.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
...@@ -251,13 +272,15 @@ let result = treeSet.getHigherValue("sdfs"); ...@@ -251,13 +272,15 @@ let result = treeSet.getHigherValue("sdfs");
popFirst(): T popFirst(): T
Removes the first entry in this container. Removes the first element in this container.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| T | Entry removed.| | T | Element removed.|
**Example** **Example**
...@@ -273,13 +296,15 @@ let result = treeSet.popFirst(); ...@@ -273,13 +296,15 @@ let result = treeSet.popFirst();
popLast(): T popLast(): T
Removes the last entry in this container. Removes the last element in this container.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| T | Entry removed.| | T | Element removed.|
**Example** **Example**
...@@ -297,6 +322,8 @@ clear(): void ...@@ -297,6 +322,8 @@ clear(): void
Clears this container and sets its length to **0**. Clears this container and sets its length to **0**.
**System capability**: SystemCapability.Utils.Lang
**Example** **Example**
```ts ```ts
...@@ -313,6 +340,8 @@ values(): IterableIterator&lt;T&gt; ...@@ -313,6 +340,8 @@ values(): IterableIterator&lt;T&gt;
Obtains an iterator that contains all the values in this container. Obtains an iterator that contains all the values in this container.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
| Type| Description| | Type| Description|
...@@ -328,7 +357,7 @@ treeSet.add("sdfs"); ...@@ -328,7 +357,7 @@ treeSet.add("sdfs");
let iter = treeSet.values(); let iter = treeSet.values();
let temp = iter.next().value; let temp = iter.next().value;
while(temp != undefined) { while(temp != undefined) {
console.log(temp); console.log("value:" + temp);
temp = iter.next().value; temp = iter.next().value;
} }
``` ```
...@@ -338,20 +367,22 @@ while(temp != undefined) { ...@@ -338,20 +367,22 @@ while(temp != undefined) {
forEach(callbackfn: (value?: T, key?: T, set?: TreeSet&lt;T&gt;) => void, thisArg?: Object): void forEach(callbackfn: (value?: T, key?: T, set?: TreeSet&lt;T&gt;) => void, thisArg?: Object): void
Uses a callback to traverse the entries in this container and obtain their position indexes. Uses a callback to traverse the elements in this container and obtain their position indexes.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| callbackfn | function | Yes| Callback invoked to traverse the entries in the container.| | callbackfn | function | Yes| Callback invoked to traverse the elements in the container.|
| thisArg | Object | No| Value to use when the callback is invoked.| | thisArg | Object | No| Value to use when the callback is invoked.|
callbackfn callbackfn
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| value | T | No| Value of the entry that is currently traversed.| | value | T | No| Value of the element that is currently traversed.|
| key | T | No| Key of the entry that is currently traversed (same as **value**).| | key | T | No| Key of the element that is currently traversed (same as **value**).|
| set | TreeSet&lt;T&gt; | No| Instance that invokes the **forEach** method.| | set | TreeSet&lt;T&gt; | No| Instance that invokes the **forEach** method.|
**Example** **Example**
...@@ -361,7 +392,7 @@ let treeSet = new TreeSet(); ...@@ -361,7 +392,7 @@ let treeSet = new TreeSet();
treeSet.add("sdfs"); treeSet.add("sdfs");
treeSet.add("dfsghsf"); treeSet.add("dfsghsf");
treeSet.forEach((value, key) => { treeSet.forEach((value, key) => {
console.log(value, key) console.log("value:" + value, key)
}); });
``` ```
...@@ -370,7 +401,9 @@ treeSet.forEach((value, key) => { ...@@ -370,7 +401,9 @@ treeSet.forEach((value, key) => {
entries(): IterableIterator<[T, T]> entries(): IterableIterator<[T, T]>
Obtains an iterator that contains all the entries in this container. Obtains an iterator that contains all the elements in this container.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
...@@ -387,8 +420,8 @@ treeSet.add("sdfs"); ...@@ -387,8 +420,8 @@ treeSet.add("sdfs");
let iter = treeSet.entries(); let iter = treeSet.entries();
let temp = iter.next().value; let temp = iter.next().value;
while(temp != undefined) { while(temp != undefined) {
console.log(temp[0]); console.log("key:" + temp[0]);
console.log(temp[1]); console.log("value:" + temp[1]);
temp = iter.next().value; temp = iter.next().value;
} }
``` ```
...@@ -398,9 +431,10 @@ while(temp != undefined) { ...@@ -398,9 +431,10 @@ while(temp != undefined) {
[Symbol.iterator]\(): IterableIterator&lt;T&gt; [Symbol.iterator]\(): IterableIterator&lt;T&gt;
Obtains an iterator, each item of which is a JavaScript object. Obtains an iterator, each item of which is a JavaScript object.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
| Type| Description| | Type| Description|
...@@ -416,14 +450,14 @@ treeSet.add("sdfs"); ...@@ -416,14 +450,14 @@ treeSet.add("sdfs");
// Method 1: // Method 1:
for (let item of treeSet) { for (let item of treeSet) {
console.log("value: " + item); console.log("value:" + item);
} }
// Method 2: // Method 2:
let iter = treeSet[Symbol.iterator](); let iter = treeSet[Symbol.iterator]();
let temp = iter.next().value; let temp = iter.next().value;
while(temp != undefined) { while(temp != undefined) {
console.log(temp); console.log("value:" + temp);
temp = iter.next().value; temp = iter.next().value;
} }
``` ```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册