提交 3208d5e0 编写于 作者: W wusongqing

added docs

Signed-off-by: Nwusongqing <wusongqing@huawei.com>
上级 56c524d0
# Linear Container ArrayList
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
## Modules to Import
```
import ArrayList from '@ohos.util.ArrayList'
```
## System Capabilities
SystemCapability.Utils.Lang
## ArrayList
### Attributes
| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
| length | number | Yes| No| Number of entries in an array list (called container later).|
### constructor
constructor()
A constructor used to create an **ArrayList** instance.
**Example**
```
let arrayList = new ArrayList();
```
### add
add(element: T): boolean
Adds an entry at the end of this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to add.|
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the entry is added successfully; returns **false** otherwise.|
**Example**
```
let arrayList = new ArrayList();
let result = arrayList.add("a");
let result1 = arrayList.add(1);
let b = [1, 2, 3];
let result2 = arrayList.add(b);
let c = {name: "lala", age: "13"};
let result3 = arrayList.add(false);
```
### insert
insert(element: T, index: number): void
Inserts an entry at the specified position in this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to insert.|
| index | number | Yes| Index of the position where the entry is to be inserted.|
**Example**
```
let arrayList = new ArrayList();
arrayList.insert("A", 0);
arrayList.insert(0, 1);
arrayList.insert(true, 2);
```
### has
has(element: T): boolean
Checks whether this container has the specified entry.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to check.|
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the specified entry is contained; returns **false** otherwise.|
**Example**
```
let arrayList = new ArrayList();
let result = arrayList.has("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
arrayList.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
let result1 = arrayList.has("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
```
### getIndexOf
getIndexOf(element: T): number
Obtains the index of the first occurrence of the specified entry in this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to query.|
**Return value**
| Type| Description|
| -------- | -------- |
| number | Returns the position index if obtained; returns **-1** if the specified entry is not found.|
**Example**
```
let arrayList = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(2);
arrayList.add(1);
arrayList.add(2);
arrayList.add(4);
let result = arrayList.getIndexOf(2);
```
### getLastIndexOf
getLastIndexOf(element: T): number
Obtains the index of the last occurrence of the specified entry in this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to query.|
**Return value**
| Type| Description|
| -------- | -------- |
| number | Returns the position index if obtained; returns **-1** if the specified entry is not found.|
**Example**
```
let arrayList = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(2);
arrayList.add(1);
arrayList.add(2);
arrayList.add(4);
let result = arrayList.getLastIndexOf(2);
```
### removeByIndex
removeByIndex(index: number): T
Removes an entry with the specified position from this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| index | number | Yes| Position index of the entry to remove.|
**Return value**
| Type| Description|
| -------- | -------- |
| T | Entry removed.|
**Example**
```
let arrayList = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(2);
arrayList.add(4);
let result = arrayList.removeByIndex(2);
```
### remove
remove(element: T): boolean
Removes the first occurrence of the specified entry from this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to remove.|
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the entry is removed successfully; returns **false** otherwise.|
**Example**
```
let arrayList = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(4);
let result = arrayList.remove(2);
```
### removeByRange
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.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| fromIndex | number | Yes| Index of the start position.|
| toIndex | number | Yes| Index of the end position.|
**Example**
```
let arrayList = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(4);
arrayList.removeByRange(2, 4);
arrayList.removeByRange(4, 3);
arrayList.removeByRange(2, 6);
```
### replaceAllElements
replaceAllElements(callbackfn: (value: T, index?: number, arrlist?: ArrayList&lt;T&gt;) => T,
thisArg?: Object): void
Replaces all entries in this container with new entries, and returns the new ones.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callbackfn | function | Yes| Callback invoked for the replacement.|
| thisArg | Object | No| Value to use when the callback is invoked.|
callbackfn
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| value | T | Yes| Value of the entry that is currently traversed.|
| index | number | No| Position index of the entry that is currently traversed.|
| arrlist | ArrayList&lt;T&gt; | No| Instance that invokes the **replaceAllElements** method.|
**Example**
```
let arrayList = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(4);
arrayList.replaceAllElements((value, index) => {
return value = 2 * value;
});
arrayList.replaceAllElements((value, index) => {
return value = value - 2;
});
```
### forEach
forEach(callbackfn: (value: T, index?: number, arrlist?: ArrayList&lt;T&gt;) => void,
thisArg?: Object): void
Uses a callback to traverse the entries in this container and obtain their position indexes.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callbackfn | function | Yes| Callback invoked to traverse the entries in the container.|
| thisArg | Object | No| Value to use when the callback is invoked.|
callbackfn
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| value | T | Yes| Value of the entry that is currently traversed.|
| index | number | No| Position index of the entry that is currently traversed.|
| arrlist | ArrayList&lt;T&gt; | No| Instance that invokes the **forEach** method.|
**Example**
```
let arrayList = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(4);
arrayList.forEach((value, index) => {
console.log(value, index);
});
```
### sort
sort(comparator?: (firstValue: T, secondValue: T) => number): void
Sorts entries in this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| comparator | function | No| Callback invoked for sorting.|
comparator
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| firstValue | T | Yes| Previous entry.|
| secondValue | T | Yes| Next entry.|
**Example**
```
let arrayList = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(4);
arrayList.sort(a, (b => a - b));
arrayList.sort(a, (b => b - a));
arrayList.sort();
```
### subArrayList
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.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| fromIndex | number | Yes| Index of the start position.|
| toIndex | number | Yes| Index of the end position.|
**Return value**
| Type| Description|
| -------- | -------- |
| ArrayList&lt;T&gt; | New **ArrayList** instance obtained.|
**Example**
```
let arrayList = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(4);
let result1 = arrayList.subArrayList(2, 4);
let result2 = arrayList.subArrayList(4, 3);
let result3 = arrayList.subArrayList(2, 6);
```
### clear
clear(): void
Clears this container and sets its length to **0**.
**Example**
```
let arrayList = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(4);
arrayList.clear();
```
### clone
clone(): ArrayList&lt;T&gt;
Clones this container and returns a copy. The modification to the copy does not affect the original instance.
**Return value**
| Type| Description|
| -------- | -------- |
| ArrayList&lt;T&gt; | New **ArrayList** instance obtained.|
**Example**
```
let arrayList = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(4);
let result = arrayList.clone();
```
### getCapacity
getCapacity(): number
Obtains the capacity of this container.
**Return value**
| Type| Description|
| -------- | -------- |
| number | Capacity obtained.|
**Example**
```
let arrayList = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(4);
let result = arrayList.getCapacity();
```
### convertToArray
convertToArray(): Array&lt;T&gt;
Converts this container into an array.
**Return value**
| Type| Description|
| -------- | -------- |
| Array&lt;T&gt; | Array obtained.|
**Example**
```
let arrayList = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(4);
let result = arrayList.convertToArray();
```
### isEmpty
isEmpty(): boolean
Checks whether this container is empty (contains no entry).
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the container is empty; returns **false** otherwise.|
**Example**
```
let arrayList = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(4);
let result = arrayList.isEmpty();
```
### increaseCapacityTo
increaseCapacityTo(newCapacity: number): void
Increases the capacity of this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| newCapacity | number | Yes| New capacity.|
**Example**
```
let arrayList = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(4);
arrayList.increaseCapacityTo(2);
arrayList.increaseCapacityTo(8);
```
### trimToCurrentLength
trimToCurrentLength(): void
Trims the capacity of this container to its current length.
**Example**
```
let arrayList = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(4);
arrayList.trimToCurrentLength();
```
### [Symbol.iterator]
[Symbol.iterator]\(): IterableIterator&lt;T&gt;
Obtains an iterator, each item of which is a JavaScript object.
**Return value**
| Type| Description|
| -------- | -------- |
| IterableIterator&lt;T&gt; | Iterator obtained.|
**Example**
```
let arrayList = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(4);
// Method 1:
for (let item of arrayList) {
console.log(item);
}
// Method 2:
let iter = arrayList[Symbol.iterator]();
let temp = iter.next().value;
while(temp != undefined) {
console.log(temp);
temp = iter.next().value;
}
```
# Linear Container Deque
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
## Modules to Import
```
import Deque from '@ohos.util.Deque'
```
## System Capabilities
SystemCapability.Utils.Lang
## Deque
### Attributes
| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
| length | number | Yes| No| Number of entries in a double-ended queue (deque, called container later).|
### constructor
constructor()
A constructor used to create a **Deque** instance.
**Example**
```
let deque = new Deque();
```
### insertFront
insertFront(element: T): void
Inserts an entry at the front of this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to insert.|
**Example**
```
let deque = new Deque;
deque.insertFront("a");
deque.insertFront(1);
let b = [1, 2, 3];
deque.insertFront(b);
let c = {name : "lala", age : "13"};
deque.insertFront(false);
```
### insertEnd
insertEnd(element: T): void
Inserts an entry at the end of this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to insert.|
**Example**
```
let deque = new Deque;
deque.insertEnd("a");
deque.insertEnd(1);
let b = [1, 2, 3];
deque.insertEnd(b);
let c = {name : "lala", age : "13"};
deque.insertEnd(false);
```
### has
has(element: T): boolean
Checks whether this container has the specified entry.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to check.|
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the specified entry is contained; returns **false** otherwise.|
**Example**
```
let deque = new Deque();
let result = deque.has("Ahfbrgrbgnutfodgorrogorg");
deque.insertFront("Ahfbrgrbgnutfodgorrogorg");
let result1 = deque.has("Ahfbrgrbgnutfodgorrogorg");
```
### popFirst
popFirst(): T
Removes the first entry of this container.
**Return value**
| Type| Description|
| -------- | -------- |
| T | Entry removed.|
**Example**
```
let deque = new Deque();
deque.insertFront(2);
deque.insertFront(4);
deque.insertEnd(5);
deque.insertFront(2);
deque.insertFront(4);
let result = deque.popFirst();
```
### popLast
popLast(): T
Removes the last entry of this container.
**Return value**
| Type| Description|
| -------- | -------- |
| T | Entry removed.|
**Example**
```
let deque = new Deque();
deque.insertFront(2);
deque.insertEnd(4);
deque.insertFront(5);
deque.insertFront(2);
deque.insertFront(4);
deque.popLast();
let result = deque.popLast();
```
### forEach
forEach(callbackfn: (value: T, index?: number, deque?: Deque&lt;T&gt;) => void,
thisArg?: Object): void
Uses a callback to traverse the entries in this container and obtain their position indexes.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callbackfn | function | Yes| Callback invoked to traverse the entries in the container.|
| thisArg | Object | No| Value to use when the callback is invoked.|
callbackfn
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| value | T | Yes| Value of the element that is currently traversed.|
| index | number | No| Position index of the entry that is currently traversed.|
| deque | Deque&lt;T&gt; | No| Instance that invokes the **forEach** method.|
**Example**
```
let deque = new Deque();
deque.insertFront(2);
deque.insertEnd(4);
deque.insertFront(5);
deque.insertEnd(4);
deque.forEach((value, index) => {
console.log(value, index);
});
```
### getFirst
getFirst(): T;
Obtains the first entry of this container.
**Return value**
| Type| Description|
| -------- | -------- |
| T | Entry obtained.|
**Example**
```
let deque = new Deque();
deque.insertEnd(2);
deque.insertEnd(4);
deque.insertFront(5);
deque.insertFront(4);
let result = deque.getFirst();
```
### getLast
getLast(): T
Obtains the last entry of this container.
**Return value**
| Type| Description|
| -------- | -------- |
| T | Entry obtained.|
**Example**
```
let deque = new Deque();
deque.insertFront(2);
deque.insertFront(4);
deque.insertFront(5);
deque.insertFront(4);
let result = deque.getLast();
```
### [Symbol.iterator]
[Symbol.iterator]\(): IterableIterator&lt;T&gt;
Obtains an iterator, each item of which is a JavaScript object.
**Return value**
| Type| Description|
| -------- | -------- |
| IterableIterator&lt;T&gt; | Iterator obtained.|
**Example**
```
let deque = new Deque();
deque.insertFront(2);
deque.insertFront(4);
deque.insertFront(5);
deque.insertFront(4);
// Method 1:
for (let item of deque) {
console.log(item);
}
// Method 2:
let iter = deque[Symbol.iterator]();
let temp = iter.next().value;
while(temp != undefined) {
console.log(temp);
temp = iter.next().value;
}
```
# Nonlinear Container HashSet
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
## Modules to Import
```
import HashSet from '@ohos.util.HashSet';
```
## System Capabilities
SystemCapability.Utils.Lang
## HashSet
### Attributes
| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
| length | number | Yes| No| Number of entries in a hash set (called container later).|
### constructor
constructor()
A constructor used to create a **HashSet** instance.
**Example**
```
let hashSet = new HashSet();
```
### isEmpty
isEmpty(): boolean
Checks whether this container is empty (contains no entry).
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the container is empty; returns **false** otherwise.|
**Example**
```
const hashSet = new HashSet();
hashSet.isEmpty();
```
### has
has(value: T): boolean
Checks whether this container contains the specified entry.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| value | T | Yes| Entry to check.|
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the specified entry is contained; returns **false** otherwise.|
**Example**
```
let hashSet = new HashSet();
let result = hashSet.has("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
hashSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
let result1 = hashSet.has("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
```
### add
add(value: T): boolean
Adds an entry to this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| value | T | Yes| Entry to add.|
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the entry is added successfully; returns **false** otherwise.|
**Example**
```
let hashSet = new HashSet();
let result = hashSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
```
### remove
remove(value: T): boolean
Removes an entry from this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| value | T | Yes| Entry to remove.|
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the entry is removed successfully; returns **false** otherwise.|
**Example**
```
let hashSet = new HashSet();
hashSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
hashSet.add("sdfs");
let result = hashSet.remove("sdfs");
```
### clear
clear(): void
Clears this container and sets its length to **0**.
**Example**
```
let hashSet = new HashSet();
hashSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
hashSet.add("sdfs");
hashSet.clear();
```
### values
values(): IterableIterator&lt;T&gt;
Obtains an iterator that contains all the values in this container.
**Return value**
| Type| Description|
| -------- | -------- |
| IterableIterator&lt;T&gt; | Iterator obtained.|
**Example**
```
let hashSet = new HashSet();
hashSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
hashSet.add("sdfs");
let iter = hashSet.values();
let temp = iter.next().value;
while(temp != undefined) {
console.log(temp);
temp = iter.next().value;
}
```
### forEach
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.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callbackfn | function | Yes| Callback invoked to traverse the entries in the container.|
| thisArg | Object | No| Value to use when the callback is invoked.|
callbackfn
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| value | T | Yes| Value of the entry that is currently traversed.|
| key | T | No| Key of the entry that is currently traversed (same as **value**).|
| set | HashSet&lt;T&gt; | No| Instance that invokes the **forEach** method.|
**Example**
```
let hashSet = new HashSet();
hashSet.add("sdfs");
hashSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
hashSet.forEach((value, key) => {
console.log(value, key);
});
```
### entries
entries(): IterableIterator<[T, T]>
Obtains an iterator that contains all the entries in this container.
**Return value**
| Type| Description|
| -------- | -------- |
| IterableIterator<[T, T]> | Iterator obtained.|
**Example**
```
let hashSet = new HashSet();
hashSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
hashSet.add("sdfs");
let iter = hashSet.entries();
let temp = iter.next().value;
while(temp != undefined) {
console.log(temp[0]);
console.log(temp[1]);
temp = iter.next().value;
}
```
### [Symbol.iterator]
[Symbol.iterator]\(): IterableIterator&lt;T&gt;
Obtains an iterator, each item of which is a JavaScript object.
**Return value**
| Type| Description|
| -------- | -------- |
| IterableIterator&lt;T&gt; | Iterator obtained.|
**Example**
```
let hashSet = new HashSet();
hashSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
hashSet.add("sdfs");
// Method 1:
for (let item of hashSet) {
console.log("value: " + item);
}
// Method 2:
let iter = hashSet[Symbol.iterator]();
let temp = iter.next().value;
while(temp != undefined) {
console.log(temp);
temp = iter.next().value;
}
```
# Nonlinear Container LightWeightSet
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
## Modules to Import
```
import LightWeightSet from '@ohos.util.LightWeightSet'
```
## System Capabilities
SystemCapability.Utils.Lang
## LightWeightSet
### Attributes
| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
| length | number | Yes| No| Number of entries in a lightweight set (called container later).|
### constructor
constructor()
A constructor used to create a **LightWeightSet** instance.
**Example**
```
let lightWeightSet = new LightWeightSet();
```
### isEmpty
isEmpty(): boolean
Checks whether this container is empty.
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the container is empty; returns **false** otherwise.|
**Example**
```
const lightWeightSet = new LightWeightSet();
let result = lightWeightSet.isEmpty();
```
### add
add(obj: T): boolean
Adds an entry to this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| obj | T | Yes| Entry to add.|
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the entry is added successfully; returns **false** otherwise.|
**Example**
```
let lightWeightSet = new LightWeightSet();
let result = lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
```
### addAll
addAll(set: LightWeightSet&lt;T&gt;): boolean
Adds all entries in a **LightWeightSet** instance to this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| set | LightWeightSet&lt;T&gt; | Yes| **LightWeightSet** instance whose entries are to be added to the current container.|
**Example**
```
let lightWeightSet = new LightWeightSet();
lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
lightWeightSet.add("sdfs");
let set = new LightWeightSet();
set.add("sfage");
let result = lightWeightSet.addAll(set);
```
### hasAll
hasAll(set: LightWeightSet&lt;T&gt;): boolean
Checks whether this container contains all entries of the specified **LightWeightSet** instance.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| set | LightWeightSet&lt;T&gt; | Yes| **LightWeightSet** instance to be used for comparison.|
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if all the entries in the specified **LightWeightSet** instance are contained; returns **false** otherwise.|
**Example**
```
let lightWeightSet = new LightWeightSet();
lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
lightWeightSet.add("sdfs");
let set = new LightWeightSet();
set.add("sdfs");
let result = lightWeightSet.hasAll(set);
```
### has
has(key: T): boolean
Checks whether this container has the specified key.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key| T | Yes| Key to query.|
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the specified key is contained; returns **false** otherwise.|
**Example**
```
let lightWeightSet = new LightWeightSet();
let result = lightWeightSet.has(123);
lightWeightSet.add(123);
let result = lightWeightSet.has(123);
```
### equal
equal(obj: Object): boolean
Checks whether this container contains objects of the same type as the specified **obj**.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| obj | Object | Yes| Object to be used for comparison.|
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the container contains objects of the same type as the specified **obj**; returns **false** otherwise.|
**Example**
```
let lightWeightSet = new LightWeightSet();
lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
lightWeightSet.add("sdfs");
let obj = {"Ahfbrgrbgnutfodgorrogorgrogofdfdf", "sdfs"};
let result = lightWeightSet.equal(obj);
```
### increaseCapacityTo
increaseCapacityTo(minimumCapacity: number): void
Increases the capacity of this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| minimumCapacity | number | Yes| Minimum number of entries to accommodate in the container.|
**Example**
```
let lightWeightSet = new LightWeightSet();
lightWeightSet.increaseCapacityTo(10);
```
### getIndexOf
getIndexOf(key: T): number
Obtains the position index of the entry with the specified key in this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | T | Yes| Key of the entry to query.|
**Return value**
| Type| Description|
| -------- | -------- |
| number | Position index of the entry.|
**Example**
```
let lightWeightSet = new LightWeightSet();
lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
lightWeightSet.add("sdfs");
let result = lightWeightSet.getIndexOf("sdfs");
```
### remove
remove(key: T): T
Removes an entry of the specified key from this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | T | Yes| Key of the entry to remove.|
**Return value**
| Type| Description|
| -------- | -------- |
| T | Value of the entry removed.|
**Example**
```
let lightWeightSet = new LightWeightSet();
lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
lightWeightSet.add("sdfs");
let result = lightWeightSet.remove("sdfs");
```
### removeAt
removeAt(index: number): boolean
Removes the entry at the specified position from this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| index | number | Yes| Position index of the entry to remove.|
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the entry is removed successfully; returns **false** otherwise.|
**Example**
```
let lightWeightSet = new LightWeightSet();
lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
lightWeightSet.add("sdfs");
let result = lightWeightSet.removeAt(1);
```
### getValueAt
getValueAt(index: number): T
Obtains the value of the entry at the specified position in this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| index | number | Yes| Position index of the entry.|
**Return value**
| Type| Description|
| -------- | -------- |
| T | Value obtained.|
**Parameters**
```
let lightWeightSet = new LightWeightSet();
lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
lightWeightSet.add("sdfs");
let result = lightWeightSet.getValueAt(1);
```
### clear
clear(): void
Clears this container and sets its length to **0**.
**Example**
```
let lightWeightSet = new LightWeightSet();
lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
lightWeightSet.add("sdfs");
lightWeightSet.clear();
```
### toString
toString(): String
Obtains a string that contains all entries in this container.
**Return value**
| Type| Description|
| -------- | -------- |
| String | String obtained.|
**Example**
```
let lightWeightSet = new LightWeightSet();
lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
lightWeightSet.add("sdfs");
let result = lightWeightSet.toString();
```
### toArray
toArray(): Array&lt;T&gt;
Obtains an array that contains all objects in this container.
**Return value**
| Type| Description|
| -------- | -------- |
| Array&lt;T&gt; | Array obtained.|
**Example**
```
let lightWeightSet = new LightWeightSet();
lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
lightWeightSet.add("sdfs");
let result = lightWeightSet.toArray();
```
### values
values(): IterableIterator&lt;T&gt;
Obtains an iterator that contains all the values in this container.
**Return value**
| Type| Description|
| -------- | -------- |
| IterableIterator&lt;T&gt; | Iterator obtained.|
**Example**
```
let lightWeightSet = new LightWeightSet();
lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
lightWeightSet.add("sdfs");
let iter = lightWeightSet.values();
let index = 0;
while(index < lightWeightSet.length) {
console.log(JSON.stringify(iter.next().value));
index++;
}
```
### forEach
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.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callbackfn | function | Yes| Callback invoked to traverse the entries in the container.|
| thisArg | Object | No| Value to use when the callback is invoked.|
callbackfn
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| value | T | Yes| Value of the entry that is currently traversed.|
| key | T | No| Key of the entry that is currently traversed (same as **value**).|
| set | LightWeightSet&lt;T&gt; | No| Instance that invokes the **forEach** method.|
**Example**
```
let lightWeightSet = new LightWeightSet();
lightWeightSet.add("sdfs");
lightWeightSet.add("dfsghsf");
lightWeightSet.forEach((value, key) => {
console.log(value, key);
});
```
### entries
entries(): IterableIterator<[T, T]>
Obtains an iterator that contains all the entries in this container.
**Return value**
| Type| Description|
| -------- | -------- |
| IterableIterator<[T, T]> | Iterator obtained.|
**Example**
```
let lightWeightSet = new LightWeightSet();
lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
lightWeightSet.add("sdfs");
let iter = lightWeightSet.entries();
let index = 0;
while(index < lightWeightSet.length) {
console.log(JSON.stringify(iter.next().value));
index++;
}
```
### [Symbol.iterator]
[Symbol.iterator]\(): IterableIterator&lt;T&gt;
Obtains an iterator, each item of which is a JavaScript object.
**Return value**
| Type| Description|
| -------- | -------- |
| IterableIterator&lt;T&gt; | Iterator obtained.|
**Example**
```
let lightWeightSet = new LightWeightSet();
lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
lightWeightSet.add("sdfs");
// Method 1:
for (let item of lightWeightSet) {
console.log("value: " + item);
}
// Method 2:
let iter = lightWeightSet[Symbol.iterator]();
let temp = iter.next().value;
while(temp != undefined) {
console.log(temp);
temp = iter.next().value;
}
```
# Linear Container List
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
## Modules to Import
```
import List from '@ohos.util.List'
```
## System Capabilities
SystemCapability.Utils.Lang
## List
### Attributes
| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
| length | number | Yes| No| Number of entries in a list (called container later).|
### constructor
constructor(head?: NodeObj&lt;T&gt;)
A constructor used to create a **List** instance.
**Parameters**
| Name| Value Type | Readable| Description|
| -------- | -------- | -------- | -------- |
| head | NodeObj&lt;T&gt; | Yes| Node object, including **element** and **next**.|
**Example**
```
let list = new List();
```
### add
add(element: T): boolean
Adds an entry at the end of this container.
**Parameters**
| Name| Value Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
| element | T | Yes| Element to add.|
**Return value**
| Value Type | Description|
| -------- | -------- |
| boolean | Returns **true** if the entry is added successfully; returns **false** otherwise.|
**Example**
```
let list = new List;
let result = list.add("a");
let result1 = list.add(1);
let b = [1, 2, 3];
list.add(b);
let c = {name : "lala", age : "13"};
let result3 = list.add(false);
```
### insert
insert(element: T, index: number): void
Inserts an entry at the specified position in this container.
**Parameters**
| Name| Value Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
| element | T | Yes| Element to insert.|
| index | number | Yes| Index of the position where the entry is to be inserted.|
**Example**
```
let list = new List();
list.insert("A", 0);
list.insert(0, 1);
list.insert(true, 2);
```
### has
has(element: T): boolean
Checks whether this container has the specified entry.
**Parameters**
| Name| Value Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to query.|
**Return value**
| Value Type | Description|
| -------- | -------- |
| boolean | Returns **true** if the specified entry is contained; returns **false** otherwise.|
**Example**
```
let list = new List();
let result = list.has("Ahfbrgrbgnutfodgorrogorg");
list.add("Ahfbrgrbgnutfodgorrogorg");
let result1 = list.has("Ahfbrgrbgnutfodgorrogorg");
```
### get
get(index: number): T
Obtains the entry at the specified position in this container.
**Parameters**
| Name| Value Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
| index | number | Yes| Position index of the entry to obtain.|
**Return value**
| Value Type | Description|
| -------- | -------- |
| T | Entry obtained.|
**Example**
```
let list = new List();
list.add(2);
list.add(4);
list.add(5);
list.add(2);
list.add(1);
list.add(2);
list.add(4);
let result = list.get(2);
```
### getLastIndexOf
getLastIndexOf(element: T): number
Obtains the index of the last occurrence of the specified entry in this container.
**Parameters**
| Name| Value Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to query.|
**Return value**
| Value Type | Description|
| -------- | -------- |
| number | Returns the position index if obtained; returns **-1** if the specified entry is not found.|
**Example**
```
let list = new List();
list.add(2);
list.add(4);
list.add(5);
list.add(2);
list.add(1);
list.add(2);
list.add(4);
let result = list.getLastIndexOf(2);
```
### getIndexOf
getIndexOf(element: T): number
Obtains the index of the first occurrence of the specified entry in this container.
**Parameters**
| Name| Value Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to query.|
**Return value**
| Value Type | Description|
| -------- | -------- |
| number | Returns the position index if obtained; returns **-1** if the specified entry is not found.|
**Example**
```
let list = new List();
list.add(2);
list.add(4);
list.add(5);
list.add(2);
list.add(1);
list.add(2);
list.add(4);
list.getIndexOf(2);
let result = list.getIndexOf(2);
```
### equal
equal(obj: Object): boolean
Compares whether a specified object is equal to this container.
**Parameters**
| Name| Value Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
| obj | Object | Yes| Object used for comparison.|
**Return value**
| Value Type | Description|
| -------- | -------- |
| boolean | Returns **true** if the two are equal; returns **false** otherwise.|
**Example**
```
let list = new List();
list.add(2);
list.add(4);
list.add(5);
list.add(2);
let obj1 = new List();
obj1.add(2);
obj1.add(4);
obj1.add(5);
list.equal(obj1);
let obj2 = {name : "lala", age : "13"};
let result = list.equal(obj2);
```
### removeByIndex
removeByIndex(index: number): T
Removes an entry at the specified position from this container.
**Parameters**
| Name| Value Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
| index | number | Yes| Position index of the entry to remove.|
**Return value**
| Value Type | Description|
| -------- | -------- |
| T | Entry removed.|
**Example**
```
let list = new List();
list.add(2);
list.add(4);
list.add(5);
list.add(2);
list.add(4);
let result = list.removeByIndex(2);
```
### remove
remove(element: T): boolean
Removes the first occurrence of the specified entry from this container.
**Parameters**
| Name| Value Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to remove.|
**Return value**
| Value Type | Description|
| -------- | -------- |
| boolean | Returns **true** if the entry is removed successfully; returns **false** otherwise.|
**Example**
```
let list = new List();
list.add(2);
list.add(4);
list.add(5);
list.add(4);
let result = list.remove(2);
```
### replaceAllElements
replaceAllElements(callbackfn: (value: T, index?: number, list?: List&lt;T&gt;) => T,
thisArg?: Object): void
Replaces all entries in this container with new entries, and returns the new ones.
**Parameters**
| Name| Value Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callbackfn | function | Yes| Callback invoked for the replacement.|
| thisArg | Object | No| Value to use when the callback is invoked.|
callbackfn
| Name| Value Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
| value | T | Yes| Value of the entry that is currently traversed.|
| index | number | No| Position index of the entry that is currently traversed.|
| list | List&lt;T&gt; | No| Instance that invokes the **replaceAllElements** method.|
**Example**
```
let list = new List();
list.add(2);
list.add(4);
list.add(5);
list.add(4);
list.replaceAllElements((value, index) => {
return value = 2 * value;
});
list.replaceAllElements((value, index) => {
return value = value - 2;
});
```
### forEach
forEach(callbackfn: (value: T, index?: number, List?: List&lt;T&gt;) => void,
thisArg?: Object): void
Uses a callback to traverse the entries in this container and obtain their position indexes.
**Parameters**
| Name| Value Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callbackfn | function | Yes| Callback invoked to traverse the entries in the container.|
| thisArg | Object | No| Value to use when the callback is invoked.|
callbackfn
| Name| Value Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
| value | T | Yes| Value of the entry that is currently traversed.|
| index | number | No| Position index of the entry that is currently traversed.|
| List | List&lt;T&gt; | No| Instance that invokes the **forEach** method.|
**Example**
```
let list = new List();
list.add(2);
list.add(4);
list.add(5);
list.add(4);
list.forEach((value, index) => {
console.log(value, index);
});
```
### sort
sort(comparator: (firstValue: T, secondValue: T) => number): void
Sorts entries in this container.
**Parameters**
| Name| Value Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
| comparator | function | Yes| Callback invoked for sorting.|
comparator
| Name| Value Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
| firstValue | T | Yes| Previous entry.|
| secondValue | T | Yes| Next entry.|
**Example**
```
let list = new List();
list.add(2);
list.add(4);
list.add(5);
list.add(4);
list.sort(a, (b => a - b));
list.sort(a, (b => b - a));
```
### getSubList
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.
**Parameters**
| Name| Value Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
| fromIndex | number | Yes| Index of the start position.|
| toIndex | number | Yes| Index of the end position.|
**Return value**
| Value Type | Description|
| -------- | -------- |
| List&lt;T&gt; | New **List** instance obtained.|
**Example**
```
let list = new List();
list.add(2);
list.add(4);
list.add(5);
list.add(4);
let result = list.subList(2, 4);
let result1 = list.subList(4, 3);
let result2 = list.subList(2, 6);
```
### clear
clear(): void
Clears this container and sets its length to **0**.
**Example**
```
let list = new List();
list.add(2);
list.add(4);
list.add(5);
list.add(4);
list.clear();
```
### set
set(index: number, element: T): T
Replaces an entry at the specified position in this container with a given entry.
**Parameters**
| Name| Value Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
| index | number | Yes| Position index of the entry to replace.|
| element | T | Yes| Entry to be used for replacement.|
**Return value**
| Value Type | Description|
| -------- | -------- |
| T | New entry.|
**Example**
```
let list = new List();
list.add(2);
list.add(4);
list.add(5);
list.add(4);
list.set(2, "b");
```
### convertToArray
convertToArray(): Array&lt;T&gt;
Converts this container into an array.
**Return value**
| Value Type | Description|
| -------- | -------- |
| Array&lt;T&gt; | Array obtained.|
**Example**
```
let list = new List();
list.add(2);
list.add(4);
list.add(5);
list.add(4);
let result = list.convertToArray();
```
### isEmpty
isEmpty(): boolean
Checks whether this container is empty (contains no entry).
**Return value**
| Value Type | Description|
| -------- | -------- |
| boolean | Returns **true** if the container is empty; returns **false** otherwise.|
**Example**
```
let list = new List();
list.add(2);
list.add(4);
list.add(5);
list.add(4);
let result = list.isEmpty();
```
### getFirst
getFirst(): T
Obtains the first entry in this container.
**Return value**
| Value Type | Description|
| -------- | -------- |
| T | The first entry obtained.|
**Example**
```
let list = new Vector();
list.add(2);
list.add(4);
list.add(5);
list.add(4);
let result = list.getFirst();
```
### getLast
getLast(): T
Obtains the last entry in this container.
**Return value**
| Value Type | Description|
| -------- | -------- |
| T | The last entry obtained.|
**Example**
```
let list = new Vector();
list.add(2);
list.add(4);
list.add(5);
list.add(4);
let result = list.getLast();
```
### [Symbol.iterator]
[Symbol.iterator]\(): IterableIterator&lt;T&gt;;
Obtains an iterator, each item of which is a JavaScript object.
**Return value**
| Value Type | Description|
| -------- | -------- |
| IterableIterator&lt;T&gt; | Iterator obtained.|
**Example**
```
let list = new List();
list.add(2);
list.add(4);
list.add(5);
list.add(4);
// Method 1:
for (let item of list) {
console.log(item);
}
// Method 2:
let iter = list[Symbol.iterator]();
let temp = iter.next().value;
while(temp != undefined) {
console.log(temp);
temp = iter.next().value;
}
```
# Linear Container Queue
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
## Modules to Import
```
import Queue from '@ohos.util.Queue'
```
## System Capabilities
SystemCapability.Utils.Lang
## Queue
### Attributes
| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
| length | number | Yes| No| Number of entries in a queue (called container later).|
### constructor
constructor()
A constructor used to create a **Queue** instance.
**Example**
```
let queue = new Queue();
```
### add
add(element: T): boolean
Adds an entry at the end of this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to add.|
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the entry is added successfully; returns **false** otherwise.|
**Example**
```
let queue = new Queue();
let result = queue.add("a");
let result1 = queue.add(1);
queue.add(1);
let b = [1, 2, 3];
queue.add(b);
let c = {name : "lala", age : "13"};
let result3 = queue.add(c);
```
### pop
pop(): T
Removes the first entry from this container.
**Return value**
| Type| Description|
| -------- | -------- |
| T | Entry removed.|
**Example**
```
let queue = new Queue();
queue.add(2);
queue.add(4);
queue.add(5);
queue.add(2);
queue.add(4);
let result = queue.pop();
```
### getFirst
getFirst(): T
Obtains the first entry of this container.
**Parameters**
| Type| Description|
| -------- | -------- |
| T | The first entry obtained.|
**Example**
```
let queue = new Queue();
queue.add(2);
queue.add(4);
queue.add(5);
queue.add(2);
let result = queue.getFirst();
```
### forEach
forEach(callbackfn: (value: T, index?: number, Queue?: Queue&lt;T&gt;) => void,
thisArg?: Object): void
Uses a callback to traverse the entries in this container and obtain their position indexes.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callbackfn | function | Yes| Callback invoked to traverse the entries in the container.|
| thisArg | Object | No| Value to use when the callback is invoked.|
callbackfn
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| value | T | Yes| Value of the entry that is currently traversed.|
| index | number | No| Position index of the entry that is currently traversed.|
| Queue | Queue&lt;T&gt; | No| Instance that invokes the **forEach** method.|
**Example**
```
let queue = new Queue();
queue.add(2);
queue.add(4);
queue.add(5);
queue.add(4);
queue.forEach((value, index) => {
console.log(value, index);
});
```
### [Symbol.iterator]
[Symbol.iterator]\(): IterableIterator&lt;T&gt;
Obtains an iterator, each item of which is a JavaScript object.
**Return value**
| Type| Description|
| -------- | -------- |
| IterableIterator&lt;T&gt; | Iterator obtained.|
**Example**
```
let queue = new Queue();
queue.add(2);
queue.add(4);
queue.add(5);
queue.add(4);
// Method 1:
for (let item of queue) {
console.log(item);
}
// Method 2:
let iter = queue[Symbol.iterator]();
let temp = iter.next().value;
while(temp != undefined) {
console.log(temp);
temp = iter.next().value;
}
```
# Linear Container Stack
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
## Modules to Import
```
import Stack from '@ohos.util.Stack'
```
## System Capabilities
SystemCapability.Utils.Lang
## Stack
### Attributes
| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
| length | number | Yes| No| Number of entries in a stack (called container later).|
### constructor
constructor()
A constructor used to create a **Stack** instance.
**Example**
```
let stack = new Stack();
```
### push
push(item: T): T
Adds an entry at the top of this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| item | T | Yes| Element to add.|
**Return value**
| Type| Description|
| -------- | -------- |
| T | Element added.|
**Example**
```
let stack = new Stack();
let result = stack.push("a");
let result1 = stack.push(1);
let b = [1, 2, 3];
stack.push(b);
let c = {name : "lala", age : "13"};
let result3 = stack.push(c);
```
### pop
pop(): T
Removes the top entry from this container.
**Return value**
| Type| Description|
| -------- | -------- |
| T | Entry removed.|
**Example**
```
let stack = new Stack();
stack.push(2);
stack.push(4);
stack.push(5);
stack.push(2);
stack.push(4);
let result = stack.pop();
```
### peek
peek(): T
Obtains the top entry of this container.
**Return value**
| Type| Description|
| -------- | -------- |
| T | Entry obtained.|
**Example**
```
let stack = new Stack();
stack.push(2);
stack.push(4);
stack.push(5);
stack.push(2);
let result = stack.peek();
```
### locate
locate(element: T): number
Obtains the index of the first occurrence of the specified entry in this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to query.|
**Return value**
| Type| Description|
| -------- | -------- |
| number | Returns the position index if obtained; returns **-1** if the specified entry is not found.|
**Example**
```
let stack = new Stack();
stack.push(2);
stack.push(4);
stack.push(5);
stack.push(2);
let result = stack.locate(2);
```
### forEach
forEach(callbackfn: (value: T, index?: number, stack?: Stack&lt;T&gt;) => void,
thisArg?: Object): void
Uses a callback to traverse the entries in this container and obtain their position indexes.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callbackfn | function | Yes| Callback invoked to traverse the entries in the container.|
| thisArg | Object | No| Value to use when the callback is invoked.|
callbackfn
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| value | T | Yes| Value of the entry that is currently traversed.|
| index | number | No| Position index of the entry that is currently traversed.|
| stack | Stack&lt;T&gt; | No| Instance that invokes the **forEach** method.|
**Example**
```
let stack = new Stack();
stack.push(2);
stack.push(4);
stack.push(5);
stack.push(4);
stack.forEach((value, index) => {
console.log(value, index);
});
```
### isEmpty
isEmpty(): boolean
Checks whether this container is empty (contains no entries).
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the container is empty; returns **false** otherwise.|
**Example**
```
let stack = new Stack();
stack.push(2);
stack.push(4);
stack.push(5);
stack.push(4);
let result = stack.isEmpty();
```
### [Symbol.iterator]
[Symbol.iterator]\(): IterableIterator&lt;T&gt;
Obtains an iterator, each item of which is a JavaScript object.
**Return value**
| Type| Description|
| -------- | -------- |
| IterableIterator&lt;T&gt; | Iterator obtained.|
**Example**
```
let stack = new Stack();
stack.push(2);
stack.push(4);
stack.push(5);
stack.push(4);
// Method 1:
for (let item of stack) {
console.log(item);
}
// Method 2:
let iter = stack[Symbol.iterator]();
let temp = iter.next().value;
while(temp != undefined) {
console.log(temp);
temp = iter.next().value;
}
```
# Nonlinear Container TreeMap
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
## Modules to Import
```
import TreeMap from '@ohos.util.TreeMap'
```
## System Capabilities
SystemCapability.Utils.Lang
## TreeMap
### Attributes
| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
| length | number | Yes| No| Number of entries in a tree map (called container later).|
### constructor
constructor(comparator?:(firstValue: K, secondValue: K) => boolean)
A constructor used to create a **TreeMap** instance.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| comparator | function | No| Custom comparator.|
**Example**
```
let treeMap = new TreeMap();
```
### isEmpty
isEmpty(): boolean;
Checks whether this container is empty (contains no entry).
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the container is empty; returns **false** otherwise.|
**Example**
```
const treeMap = new TreeMap();
let result = treeMap.isEmpty();
```
### hasKey
hasKey(key: K): boolean;
Checks whether this container has the specified key.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | K | Yes| Key to query.|
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the specified key is contained; returns **false** otherwise.|
**Example**
```
let treeMap = new TreeMap();
let result = treeMap.hasKey("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
treeMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
let result1 = treeMap.hasKey("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
```
### hasValue
hasValue(value: V): boolean
Checks whether this container has the specified value.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| value | V | Yes| Value to query.|
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the specified value is contained; returns **false** otherwise.|
**Example**
```
let treeMap = new TreeMap();
let result = treeMap.hasValue(123);
treeMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
let result1 = treeMap.hasValue(123);
```
### get
get(key: K): V
Obtains the value of the specified key in this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | K | Yes| Key to query.|
**Return value**
| Type| Description|
| -------- | -------- |
| V | Value of the key.|
**Example**
```
let treeMap = new TreeMap();
treeMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
treeMap.set("sdfs", 356);
let result = treeMap.get("sdfs");
```
### getFirstKey
getFirstKey(): K;
Obtains the first key in this container.
**Return value**
| Type| Description|
| -------- | -------- |
| K | Key obtained.|
**Example**
```
let treeMap = new TreeMap();
treeMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
treeMap.set("sdfs", 356);
let result = treeMap.getFirstKey();
```
### getLastKey
getLastKey(): K;
Obtains the last key in this container.
**Return value**
| Type| Description|
| -------- | -------- |
| K | Key obtained.|
**Example**
```
let treeMap = new TreeMap();
treeMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
treeMap.set("sdfs", 356);
let result = treeMap.getLastKey();
```
### setAll
setAll(map: TreeMap<K, V>): void
Adds all entries in a **TreeMap** instance to this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| map | TreeMap<K, V> | Yes| **TreeMap** instance whose entries are to be added to the current container.|
**Example**
```
let treeMap = new TreeMap();
treeMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
treeMap.set("sdfs", 356);
let map = new TreeMap();
treeMap.setAll(map);
```
### set
set(key: K, value: V): Object
Adds an entry to this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | K | Yes| Key of the entry to add.|
| value | V | Yes| Value of the entry to add.|
**Return value**
| Type| Description|
| -------- | -------- |
| Object | Container that contains the new entry.|
**Example**
```
let treeMap = new TreeMap();
treeMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
```
### remove
remove(key: K): V;
Removes the entry with the specified key from this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | K | Yes| Key of the entry to remove.|
**Return value**
| Type| Description|
| -------- | -------- |
| V | Value of the entry removed.|
**Example**
```
let treeMap = new TreeMap();
treeMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
treeMap.set("sdfs", 356);
treeMap.remove("sdfs");
```
### getLowerKey
getLowerKey(key: K): K
Obtains the key that is placed in front of the input key in this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | K | Yes| Input key.|
**Return value**
| Type| Description|
| -------- | -------- |
| K | Key obtained.|
**Example**
```
let treeMap = new TreeMap();
treeMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
treeMap.set("sdfs", 356);
treeMap.set("zdfgsd", 356);
let result = treeMap.getLowerKey("sdfs");
```
### getHigherKey
getHigherKey(key: K): K
Obtains the key that is placed next to the input key in this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | K | Yes| Input key.|
**Return value**
| Type| Description|
| -------- | -------- |
| K | Key obtained.|
**Example**
```
let treeMap = new TreeMap();
treeMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
treeMap.set("sdfs", 356);
treeMap.set("zdfgsd", 356);
let result = treeMap.getHigherKey("sdfs");
```
### replace
replace(key: K, newValue: V): boolean
Replaces an entry in this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | K | Yes| Key of the entry to replace.|
| newValue | V | Yes| New value of the entry.|
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the entry is replaced successfully; returns **false** otherwise.|
**Example**
```
let treeMap = new TreeMap();
treeMap.set("sdfs", 123);
let result = treeMap.replace("sdfs", 357);
```
### clear
clear(): void
Clears this container and sets its length to **0**.
**Example**
```
let treeMap = new TreeMap();
treeMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
treeMap.set("sdfs", 356);
treeMap.clear();
```
### keys
keys(): IterableIterator&lt;K&gt;
Obtains an iterator that contains all the keys in this container.
**Return value**
| Type| Description|
| -------- | -------- |
| IterableIterator&lt;K&gt; | Iterator obtained.|
**Example**
```
let treeMap = new TreeMap();
treeMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
treeMap.set("sdfs", 356);
let iter = treeMap.keys();
let temp = iter.next().value;
while(temp != undefined) {
console.log(temp);
temp = iter.next().value;
}
```
### values
values(): IterableIterator&lt;V&gt;
Obtains an iterator that contains all the values in this container.
**Return value**
| Type| Description|
| -------- | -------- |
| IterableIterator&lt;V&gt; | Iterator obtained.|
**Example**
```
let treeMap = new TreeMap();
treeMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
treeMap.set("sdfs", 356);
let iter = treeMap.values();
let temp = iter.next().value;
while(temp != undefined) {
console.log(temp);
temp = iter.next().value;
}
```
### forEach
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.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callbackfn | function | Yes| Callback invoked to traverse the entries in the container.|
| thisArg | Object | No| Value to use when the callback is invoked.|
callbackfn
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| value | V | Yes| Value of the entry that is currently traversed.|
| key | K | Yes| Key of the entry that is currently traversed.|
| map | TreeMap<K, V> | No| Instance that invokes the **forEach** method.|
**Example**
```
let treeMap = new TreeMap();
treeMap.set("sdfs", 123);
treeMap.set("dfsghsf", 357);
treeMap.forEach((value, key) => {
console.log(value, key);
});
```
### entries
entries(): IterableIterator<[K, V]>
Obtains an iterator that contains all the entries in this container.
**Return value**
| Type| Description|
| -------- | -------- |
| IterableIterator<[K, V]> | Iterator obtained.|
**Example**
```
let treeMap = new TreeMap();
treeMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
treeMap.set("sdfs", 356);
let iter = treeMap.entries();
let temp = iter.next().value;
while(temp != undefined) {
console.log(temp[0]);
console.log(temp[1]);
temp = iter.next().value;
}
```
### [Symbol.iterator]
[Symbol.iterator]\(): IterableIterator&lt;[K, V]&gt;;
Obtains an iterator, each item of which is a JavaScript object.
**Return value**
| Type| Description|
| -------- | -------- |
| IterableIterator<[K, V]> | Iterator obtained.|
**Example**
```
let treeMap = new TreeMap();
treeMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
treeMap.set("sdfs", 356);
// Method 1:
for (let item of treeMap) {
console.log("key: " + item[0]);
console.log("value: " + item[1]);
}
// Method 2:
let iter = treeMap[Symbol.iterator]();
let temp = iter.next().value;
while(temp != undefined) {
console.log(temp[0]);
console.log(temp[1]);
temp = iter.next().value;
}
```
# Nonlinear Container TreeSet
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
## Modules to Import
```
import TreeSet from '@ohos.util.TreeSet'
```
## System Capabilities
SystemCapability.Utils.Lang
## TreeSet
### Attributes
| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
| length | number | Yes| No| Number of entries in a tree set (called container later).|
### constructor
constructor(comparator?:(firstValue: T, secondValue: T) => boolean)
A constructor used to create a **TreeSet** instance.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| comparator | function | No| Custom comparator.|
**Example**
```
let treeSet = new TreeSet();
```
### isEmpty
isEmpty(): boolean
Checks whether this container is empty (contains no entry).
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the container is empty; returns **false** otherwise.|
**Example**
```
const treeSet = new TreeSet();
let result = treeSet.isEmpty();
```
### has
has(value: T): boolean
Checks whether this container has the specified value.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| value | T | Yes| Value to query.|
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the specified value is contained; returns **false** otherwise.|
**Example**
```
let treeSet = new TreeSet();
treeSet.has(123);
treeSet.add(123);
let result1 = treeSet.has(123);
```
### getFirstValue
getFirstValue(): T
Obtains the value of the first entry in this container.
**Return value**
| Type| Description|
| -------- | -------- |
| T | Value obtained.|
**Example**
```
let treeSet = new TreeSet();
treeSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
treeSet.add("sdfs");
let result = treeSet.getFirstValue();
```
### getLastValue
getLastValue(): T
Obtains the value of the last entry in this container.
**Return value**
| Type| Description|
| -------- | -------- |
| T | Value obtained.|
**Example**
```
let treeSet = new TreeSet();
treeSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
treeSet.add("sdfs");
let result = treeSet.getLastValue();
```
### add
add(value: T): boolean
Adds an entry to this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| value | T | Yes| Entry to add.|
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the entry is added successfully; returns **false** otherwise.|
**Example**
```
let treeSet = new TreeSet();
let result = treeSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
```
### remove
remove(value: T): boolean;
Removes the entry with the specified key from this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| value | T | Yes| Key of the entry to remove.|
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the entry is removed successfully; returns **false** otherwise.|
**Example**
```
let treeSet = new TreeSet();
treeSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
treeSet.add("sdfs");
let result = treeSet.remove("sdfs");
```
### getLowerValue
getLowerValue(key: T): T
Obtains the value that is placed in front of the input key in this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | T | Yes| Input key.|
**Return value**
| Type| Description|
| -------- | -------- |
| T | Value obtained.|
**Example**
```
let treeSet = new TreeSet();
treeSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
treeSet.add("sdfs");
treeSet.add("zdfgsd");
let result = treeSet.getLowerValue("sdfs");
```
### getHigherValue
getHigherValue(key: T): T
Obtains the value that is placed next to the input key in this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | T | Yes| Input key.|
**Return value**
| Type| Description|
| -------- | -------- |
| T | Value obtained.|
**Example**
```
let treeSet = new TreeSet();
treeSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
treeSet.add("sdfs");
treeSet.add("zdfgsd");
let result = treeSet.getHigherValue("sdfs");
```
### popFirst
popFirst(): T
Removes the first entry in this container.
**Return value**
| Type| Description|
| -------- | -------- |
| T | Entry removed.|
**Return value**
```
let treeSet = new TreeSet();
treeSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
treeSet.add("sdfs");
let result = treeSet.popFirst();
```
### popLast
popLast(): T
Removes the last entry in this container.
**Return value**
| Type| Description|
| -------- | -------- |
| T | Entry removed.|
**Return value**
```
let treeSet = new TreeSet();
treeSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
treeSet.add("sdfs");
let result = treeSet.popLast();
```
### clear
clear(): void
Clears this container and sets its length to **0**.
**Example**
```
let treeSet = new TreeSet();
treeSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
treeSet.add("sdfs");
treeSet.clear();
```
### values
values(): IterableIterator&lt;T&gt;
Obtains an iterator that contains all the values in this container.
**Return value**
| Type| Description|
| -------- | -------- |
| IterableIterator&lt;T&gt; | Iterator obtained.|
**Example**
```
let treeSet = new TreeSet();
treeSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
treeSet.add("sdfs");
let iter = treeSet.values();
let temp = iter.next().value;
while(temp != undefined) {
console.log(temp);
temp = iter.next().value;
}
```
### forEach
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.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callbackfn | function | Yes| Callback invoked to traverse the entries in the container.|
| thisArg | Object | No| Value to use when the callback is invoked.|
callbackfn
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| value | T | Yes| Value of the entry that is currently traversed.|
| key | T | No| Key of the entry that is currently traversed (same as **value**).|
| set | TreeSet&lt;T&gt; | No| Instance that invokes the **forEach** method.|
**Example**
```
let treeSet = new TreeSet();
treeSet.add("sdfs");
treeSet.add("dfsghsf");
treeSet.forEach((value, key) => {
console.log(value, key)
});
```
### entries
entries(): IterableIterator<[T, T]>
Obtains an iterator that contains all the entries in this container.
**Return value**
| Type| Description|
| -------- | -------- |
| IterableIterator<[T, T]> | Iterator obtained.|
**Example**
```
let treeSet = new TreeSet();
treeSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
treeSet.add("sdfs");
let iter = treeSet.entries();
let temp = iter.next().value;
while(temp != undefined) {
console.log(temp[0]);
console.log(temp[1]);
temp = iter.next().value;
}
```
### [Symbol.iterator]
[Symbol.iterator]\(): IterableIterator&lt;T&gt;
Obtains an iterator, each item of which is a JavaScript object.
**Return value**
| Type| Description|
| -------- | -------- |
| IterableIterator&lt;T&gt; | Iterator obtained.|
**Example**
```
let treeSet = new TreeSet();
treeSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
treeSet.add("sdfs");
// Method 1:
for (let item of treeSet) {
console.log("value: " + item);
}
// Method 2:
let iter = treeSet[Symbol.iterator]();
let temp = iter.next().value;
while(temp != undefined) {
console.log(temp);
temp = iter.next().value;
}
```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册