collections.Map
一種非線性數據結構。
文檔中存在泛型的使用,涉及以下泛型標記符:
- K:Key,鍵
- V:Value,值
K和V類型都需為Sendable類型。
屬性
元服務API:從API version 12 開始,該接口支持在元服務中使用。
系統能力:?SystemCapability.Utils.Lang
名稱 | 類型 | 只讀 | 可選 | 說明 |
---|---|---|---|---|
size | number | 是 | 否 | Map的元素個數。 |
constructor
constructor(entries?: readonly (readonly [K, V])[] | null)
構造函數,用于創建ArkTS Map對象。
元服務API:從API version 12 開始,該接口支持在元服務中使用。
系統能力:?SystemCapability.Utils.Lang
參數:
參數名 | 類型 | 必填 | 說明 |
---|---|---|---|
entries | [K, V][] | null | 否 | 鍵值對數組或其它可迭代對象。默認值為null,創建一個空Map對象。 |
錯誤碼:
錯誤碼ID | 錯誤信息 |
---|---|
10200012 | The ArkTS Map's constructor cannot be directly invoked. |
示例:
// 正例1:
const myMap = new collections.Map<number, number>();// 正例2:
const myMap = new collections.Map<number, string>([
[1, "one"],
[2, "two"],
[3, "three"],
]);// 反例:
@Sendable
class SharedClass {
constructor() {
}
}
let sObj = new SharedClass();
const myMap1: collections.Map<number, SharedClass> = new collections.Map<number, SharedClass>([[1, sObj]]);
// Type arguments of generic "Sendable" type must be a "Sendable" data type (arkts-sendable-generic-types)
let obj = new Object();
const myMap2: collections.Map<number, Object> = new collections.Map<number, Object>([[1, obj]]);
entries
entries(): IterableIterator<[K, V]>
返回一個Map迭代器對象,該對象包含了此Map中的每個元素的[key, value]對。
元服務API:從API version 12 開始,該接口支持在元服務中使用。
系統能力:?SystemCapability.Utils.Lang
返回值:
類型 | 說明 |
---|---|
IterableIterator<[K, V]> | 返回一個Map迭代器對象。 |
錯誤碼:
錯誤碼ID | 錯誤信息 |
---|---|
10200011 | The entries method cannot be bound with non-sendable. |
示例:
// 例1:
const myMap = new collections.Map<number, string>([
[0, "foo"],
[1, "bar"]
]);const iterator = myMap.entries();
// Expected output: [0, "foo"]
console.info(iterator.next().value);
// Expected output: [1, "bar"]
console.info(iterator.next().value);// 例2:
const myMap: collections.Map<number, string> = new collections.Map<number, string>([
[0, "one"],
[1, "two"],
[2, "three"],
[3, "four"]
]);
const entriesIter: IterableIterator<[number, string]> = myMap.entries();
for (const entry of entriesIter) {
if (entry[1].startsWith('t')) {
myMap.delete(entry[0]);
}
}
// Expected output: 2
console.info("size:" + myMap.size);
keys
keys(): IterableIterator<K>
返回一個Map迭代器對象,該對象包含了此Map中每個元素的鍵。
元服務API:從API version 12 開始,該接口支持在元服務中使用。
系統能力:?SystemCapability.Utils.Lang
返回值:
類型 | 說明 |
---|---|
IterableIterator<K> | 返回一個Map迭代器對象。 |
錯誤碼:
錯誤碼ID | 錯誤信息 |
---|---|
10200011 | The keys method cannot be bound with non-sendable. |
示例:
const myMap = new collections.Map<number, string>([
[0, "foo"],
[1, "bar"]
]);const iterator = myMap.keys();
// Expected output: 0
console.info(iterator.next().value);
// Expected output: 1
console.info(iterator.next().value);
values
values(): IterableIterator<V>
返回一個Map迭代器對象,該對象包含此Map中每個元素的值。
元服務API:從API version 12 開始,該接口支持在元服務中使用。
系統能力:?SystemCapability.Utils.Lang
返回值:
類型 | 說明 |
---|---|
IterableIterator<V> | 返回一個Map迭代器對象。 |
錯誤碼:
錯誤碼ID | 錯誤信息 |
---|---|
10200011 | The values method cannot be bound with non-sendable. |
示例:
const myMap = new collections.Map<number, string>([
[0, "foo"],
[1, "bar"]
]);const iterator = myMap.values();
// Expected output: "foo"
console.info(iterator.next().value);
// Expected output: "bar"
console.info(iterator.next().value);
clear
clear(): void
刪除該Map中的所有元素。
元服務API:從API version 12 開始,該接口支持在元服務中使用。
系統能力:?SystemCapability.Utils.Lang
錯誤碼:
以下錯誤碼的詳細介紹請參見語言基礎類庫錯誤碼。
錯誤碼ID | 錯誤信息 |
---|---|
10200011 | The clear method cannot be bound with non-sendable. |
10200201 | Concurrent modification exception. |
示例:
const myMap = new collections.Map<number, string>([
[0, "foo"],
[1, "bar"]
]);
// Expected output: 2
console.info("size:" + myMap.size);
myMap.clear();
// Expected output: 0
console.info("size:" + myMap.size);
delete
delete(key: K): boolean
刪除該Map中指定元素。
元服務API:從API version 12 開始,該接口支持在元服務中使用。
系統能力:?SystemCapability.Utils.Lang
參數:
參數名 | 類型 | 必填 | 說明 |
---|---|---|---|
key | K | 是 | 待刪除元素的鍵。 |
返回值:
類型 | 說明 |
---|---|
boolean | 如果元素存在并已被刪除,則為true;否則該元素不存在,返回false。 |
錯誤碼:
錯誤碼ID | 錯誤信息 |
---|---|
10200011 | The delete method cannot be bound with non-sendable. |
10200201 | Concurrent modification exception. |
示例:
const myMap = new collections.Map<string, string>([
["hello", "world"],
]);
// Expected result: true
console.info("result:" + myMap.delete("hello"));
// Expected result: false
console.info("result:" + myMap.has("hello"));
// Expected result: false
console.info("result:" + myMap.delete("hello"));
forEach
forEach(callbackFn: (value: V, key: K, map: Map<K, V>) => void): void
按插入順序對該Map中的每個鍵/值對執行一次回調函數。
元服務API:從API version 12 開始,該接口支持在元服務中使用。
系統能力:?SystemCapability.Utils.Lang
參數:
參數名 | 類型 | 必填 | 說明 |
---|---|---|---|
callbackFn | (value: V, key: K, map: Map<K, V>) => void | 是 | 回調函數。 |
callbackFn的參數說明:
參數名 | 類型 | 必填 | 說明 |
---|---|---|---|
value | V | 否 | 當前遍歷到的元素鍵值對的值。 |
key | K | 否 | 當前遍歷到的元素鍵值對的鍵。 |
map | Map<K, V> | 否 | 當前map實例對象。 |
錯誤碼:
錯誤碼ID | 錯誤信息 |
---|---|
10200011 | The forEach method cannot be bound with non-sendable. |
10200201 | Concurrent modification exception. |
示例:
// 正例:
new collections.Map<string, number>([
['foo', 0],
['bar', 1],
['baz', 2],
]).forEach((value, key, map) => {
console.info(`m[${key}] = ${value}`);
});// 反例:
new collections.Map<string, number>([
['foo', 0],
['bar', 1],
['baz', 2],
]).forEach((value, key, map) => {
// Throw exception `Concurrent modification exception.`
map.delete(key);
});
get
get(key: K): V | undefined
返回該Map中的指定元素。
元服務API:從API version 12 開始,該接口支持在元服務中使用。
系統能力:?SystemCapability.Utils.Lang
參數:
參數名 | 類型 | 必填 | 說明 |
---|---|---|---|
key | K | 是 | 指定key。 |
返回值:
類型 | 說明 |
---|---|
V | 與指定鍵相關聯的元素,如果鍵在Map對象中找不到,則返回undefined。 |
錯誤碼:
錯誤碼ID | 錯誤信息 |
---|---|
10200011 | The get method cannot be bound with non-sendable. |
10200201 | Concurrent modification exception. |
示例:
const myMap = new collections.Map<string, string>([
["hello", "world"],
]);
// Expected output: "world"
console.info(myMap.get("hello"));
// Expected output: undefined
console.info(myMap.get("world"));
has
has(key: K): boolean
判斷該Map中是否存在指定元素。
元服務API:從API version 12 開始,該接口支持在元服務中使用。
系統能力:?SystemCapability.Utils.Lang
返回值:
類型 | 說明 |
---|---|
boolean | 如果存在指定元素,則返回true,否則返回false。 |
錯誤碼:
錯誤碼ID | 錯誤信息 |
---|---|
10200011 | The has method cannot be bound with non-sendable. |
10200201 | Concurrent modification exception. |
示例:
const myMap = new collections.Map<string, string>([
["hello", "world"],
]);
// Expected output: true
console.info("result:" + myMap.has("hello"));
// Expected output: false
console.info("result:" + myMap.has("world"));
set
set(key: K, value: V): Map<K, V>
向該Map添加或更新一個指定的鍵值對。
元服務API:從API version 12 開始,該接口支持在元服務中使用。
系統能力:?SystemCapability.Utils.Lang
返回值:
類型 | 說明 |
---|---|
Map<K, V> | Map對象 |
錯誤碼:
錯誤碼ID | 錯誤信息 |
---|---|
10200011 | The set method cannot be bound with non-sendable. |
10200201 | Concurrent modification exception. |
示例:
// 正例:
const myMap = new collections.Map<string, string>();
myMap.set("foo", "bar")// 反例:
let obj = new Object();
const myMap: collections.Map<string, Object> = new collections.Map<string, Object>();
// Type arguments of generic "Sendable" type must be a "Sendable" data type (arkts-sendable-generic-types)
myMap.set("foo", obj);