1e41f4b71Sopenharmony_ci# Nonlinear Containers 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ciNonlinear containers, underpinned by hash tables or red-black trees, implement a data structure that enables quick search. There are several types of nonlinear containers: **HashMap**, **HashSet**, **TreeMap**, **TreeSet**, **LightWeightMap**, **LightWeightSet**, and **PlainArray**. The types of **key** and **value** in nonlinear containers must meet the ECMA standard. 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci## HashMap 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci[HashMap](../reference/apis-arkts/js-apis-hashmap.md) is used to store a set of associated key-value (KV) pairs. In a hash map, each key is unique and corresponds to a value. 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci**HashMap** uses generics. In a hash map, a key is located based on its hash code. The initial capacity of a hash map is 16, and it has capacity doubled in each dynamic expansion. The bottom layer of **HashMap** is implemented based on a hash table. It uses chaining to avoid collisions in hash tables. 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci**HashMap** is faster in accessing data than [TreeMap](../reference/apis-arkts/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. 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci[HashSet](../reference/apis-arkts/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. 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ciYou are advised to use **HashMap** when you need to quickly access, remove, and insert KV pairs. 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci**HashMap** provides the following Create, Read, Update, and Delete (CRUD) APIs. 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci| Operation | Description | 22e41f4b71Sopenharmony_ci| -------- | ------ | 23e41f4b71Sopenharmony_ci| Create | Use **set(key: K, value: V)** to add an element (a KV pair) to this container. | 24e41f4b71Sopenharmony_ci| Read | Use **get(key: K)** to obtain the value of the specified key. | 25e41f4b71Sopenharmony_ci| Read | Use **keys()** to return an iterator that contains all the keys in this container. | 26e41f4b71Sopenharmony_ci| Read | Use **values()** to return an iterator that contains all the values in this container. | 27e41f4b71Sopenharmony_ci| Read | Use **entries()** to return an iterator that contains all the elements in this container. | 28e41f4b71Sopenharmony_ci| Read | Use **forEach(callbackFn: (value?: V, key?: K, map?: HashMap<K, V>) => void, thisArg?: Object)** to traverse the elements in this container. | 29e41f4b71Sopenharmony_ci| Read | Use **\[Symbol.iterator]():IterableIterator<[K,V]>** for data access. | 30e41f4b71Sopenharmony_ci| Update | Use **replace(key: K, newValue: V)** to change the value of the specified key. | 31e41f4b71Sopenharmony_ci| Update | Use **forEach(callbackFn: (value?: V, key?: K, map?: HashMap<K, V>) => void, thisArg?: Object)** to modify an element in this container. | 32e41f4b71Sopenharmony_ci| Delete | Use **remove(key: K)** to remove an element with the specified key. | 33e41f4b71Sopenharmony_ci| Delete | Use **clear()** to clear this container. | 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci## HashSet 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci[HashSet](../reference/apis-arkts/js-apis-hashset.md) is used to store a set of values, each of which is unique in a hash set. 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci**HashSet** uses generics. In a hash set, a value is located based on its hash code. The initial capacity of a hash set is 16, and it has capacity doubled in each dynamic expansion. The type of **value** must comply with the ECMA standard. The bottom layer of **HashSet** is implemented based on a hash table. It uses chaining to avoid collisions in hash tables. 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci**HashSet** is implemented based on [HashMap](../reference/apis-arkts/js-apis-hashmap.md). In **HashSet**, only the **value** object is processed. 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ciUnlike [TreeSet](../reference/apis-arkts/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 allow only unique elements. However, null values are allowed in **HashSet**, but not in **TreeSet**, because null values may affect the order of elements in the container. 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_ciYou are advised to use **HashSet** when you need a set that has only unique elements or need to deduplicate a set. 47e41f4b71Sopenharmony_ci 48e41f4b71Sopenharmony_ci**HashSet** provides the following CRUD APIs. 49e41f4b71Sopenharmony_ci 50e41f4b71Sopenharmony_ci| Operation | Description | 51e41f4b71Sopenharmony_ci| -------- | ------ | 52e41f4b71Sopenharmony_ci| Create | Use **add(value: T)** to add a value to this container. | 53e41f4b71Sopenharmony_ci| Read | Use **values()** to return an iterator that contains all the values in this container. | 54e41f4b71Sopenharmony_ci| Read | Use **entries()** to return an iterator that contains all the elements in this container. | 55e41f4b71Sopenharmony_ci| Read | Use **forEach(callbackFn: (value?: T, key?: T, set?: HashSet\<T>) => void, thisArg?: Object)** to traverse the elements in this container. | 56e41f4b71Sopenharmony_ci| Read | Use **\[Symbol.iterator]():IterableIterator<T>** for data access. | 57e41f4b71Sopenharmony_ci| Update | Use **forEach(callbackFn: (value?: T, key?: T, set?: HashSet\<T>) => void, thisArg?: Object)** to change a value in this container. | 58e41f4b71Sopenharmony_ci| Delete | Use **remove(value: T)** to remove a value. | 59e41f4b71Sopenharmony_ci| Delete | Use **clear()** to clear this container. | 60e41f4b71Sopenharmony_ci 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ci## TreeMap 63e41f4b71Sopenharmony_ci 64e41f4b71Sopenharmony_ci[TreeMap](../reference/apis-arkts/js-apis-treemap.md) is used to store a set of associated KV pairs. In a tree map, each key is unique and corresponds to a value. 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ci**TreeMap** uses generics, and the keys in a tree map are ordered. The bottom layer of **TreeMap** is a binary tree, which supports quick search of KV pairs through the children (left child and right child) of the tree. The type of **key** must comply with the ECMA standard. Keys in a tree map are stored in order. The bottom layer of **TreeMap** is implemented based on the red-black tree and supports quick insertion and removal. 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ci[HashMap](../reference/apis-arkts/js-apis-hashmap.md) is faster in accessing data than **TreeMap**, because the former accesses the keys based on the hash codes, whereas the latter stores and accesses the keys in sorted order. 69e41f4b71Sopenharmony_ci 70e41f4b71Sopenharmony_ciYou are advised to use **TreeMap** when you need to store KV pairs in sorted order. 71e41f4b71Sopenharmony_ci 72e41f4b71Sopenharmony_ci**TreeMap** provides the following CRUD APIs. 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ci| Operation | Description | 75e41f4b71Sopenharmony_ci| ------- | ------ | 76e41f4b71Sopenharmony_ci| Create | Use **set(key: K, value: V)** to add an element (a KV pair) to this container. | 77e41f4b71Sopenharmony_ci| Read | Use **get(key: K)** to obtain the value of the specified key. | 78e41f4b71Sopenharmony_ci| Read | Use **getFirstKey()** to obtain the first key in this container. | 79e41f4b71Sopenharmony_ci| Read | Use **getLastKey()** to obtain the last key in this container. | 80e41f4b71Sopenharmony_ci| Read | Use **keys()** to return an iterator that contains all the keys in this container. | 81e41f4b71Sopenharmony_ci| Read | Use **values()** to return an iterator that contains all the values in this container. | 82e41f4b71Sopenharmony_ci| Read | Use **entries()** to return an iterator that contains all the elements in this container. | 83e41f4b71Sopenharmony_ci| Read | Use **forEach(callbackFn: (value?: V, key?: K, map?: TreeMap<K, V>) => void, thisArg?: Object)** to traverse the elements in this container. | 84e41f4b71Sopenharmony_ci| Read | Use **\[Symbol.iterator]():IterableIterator\<[K,V]>** for data access. | 85e41f4b71Sopenharmony_ci| Update | Use **replace(key: K, newValue: V)** to change the value of the specified key. | 86e41f4b71Sopenharmony_ci| Update | Use **forEach(callbackFn: (value?: V, key?: K, map?: TreeMap<K, V>) => void, thisArg?: Object)** to modify an element in this container. | 87e41f4b71Sopenharmony_ci| Delete | Use **remove(key: K)** to remove an element with the specified key. | 88e41f4b71Sopenharmony_ci| Delete | Use **clear()** to clear this container. | 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ci## TreeSet 92e41f4b71Sopenharmony_ci 93e41f4b71Sopenharmony_ci[TreeSet](../reference/apis-arkts/js-apis-treeset.md) is used to store a set of values, each of which is unique in a tree set. 94e41f4b71Sopenharmony_ci 95e41f4b71Sopenharmony_ci**TreeSet** uses generics, and the values in a tree set are ordered. The bottom layer of **TreeSet** is a binary tree, which supports quick search of a value through the children (left child and right child) of the tree. The type of **value** must meet the ECMA standard. Values in a tree set are stored in order. The bottom layer of **TreeSet** is implemented based on the red-black tree and supports quick insertion and removal. 96e41f4b71Sopenharmony_ci 97e41f4b71Sopenharmony_ci**TreeSet** is implemented based on [TreeMap](../reference/apis-arkts/js-apis-treemap.md). In **TreeSet**, only **value** objects are processed. **TreeSet** can be used to store values, each of which must be unique. 98e41f4b71Sopenharmony_ci 99e41f4b71Sopenharmony_ci[HashSet](../reference/apis-arkts/js-apis-hashset.md) stores data in a random order, whereas **TreeSet** stores data in sorted order. Both of them allow only unique elements. However, null values are allowed in **HashSet**, but not in **TreeSet**, because null values may affect the order of elements in the container. 100e41f4b71Sopenharmony_ci 101e41f4b71Sopenharmony_ciYou are advised to use **TreeSet** when you need to store data in sorted order. 102e41f4b71Sopenharmony_ci 103e41f4b71Sopenharmony_ci**TreeSet** provides the following CRUD APIs. 104e41f4b71Sopenharmony_ci 105e41f4b71Sopenharmony_ci| Operation | Description | 106e41f4b71Sopenharmony_ci| -------- | ------ | 107e41f4b71Sopenharmony_ci| Create | Use **add(value: T)** to add a value to this container. | 108e41f4b71Sopenharmony_ci| Read | Use **values()** to return an iterator that contains all the values in this container. | 109e41f4b71Sopenharmony_ci| Read | Use **entries()** to return an iterator that contains all the elements in this container. | 110e41f4b71Sopenharmony_ci| Read | Use **getFirstValue()** to obtain the first value in this container. | 111e41f4b71Sopenharmony_ci| Read | Use **getLastValue()** to obtain the last value in this container. | 112e41f4b71Sopenharmony_ci| Read | Use **forEach(callbackFn: (value?: T, key?: T, set?: TreeSet\<T>) => void, thisArg?: Object)** to traverse the elements in this container. | 113e41f4b71Sopenharmony_ci| Read | Use **\[Symbol.iterator]():IterableIterator<T>** for data access. | 114e41f4b71Sopenharmony_ci| Update | Use **forEach(callbackFn: (value?: T, key?: T, set?: TreeSet\<T>) => void, thisArg?: Object)** to change a value in this container. | 115e41f4b71Sopenharmony_ci| Delete | Use **remove(value: T)** to remove a value. | 116e41f4b71Sopenharmony_ci| Delete | Use **clear()** to clear this container. | 117e41f4b71Sopenharmony_ci 118e41f4b71Sopenharmony_ci 119e41f4b71Sopenharmony_ci## LightWeightMap 120e41f4b71Sopenharmony_ci 121e41f4b71Sopenharmony_ci[LightWeightMap](../reference/apis-arkts/js-apis-lightweightmap.md) is used to store a set of associated KV pairs. In a lightweight map, each key is unique and corresponds to a value. **LightWeightMap** uses generics and a more lightweight structure. It uses the hash code to uniquely identify a key at the bottom layer. It uses linear probing to avoid collisions. In a lightweight map, a key is located by using the hash code and binary search algorithm. The hash code is stored in an array and mapped to a key and its value in another array. The type of **key** must comply with the ECMA standard. 122e41f4b71Sopenharmony_ci 123e41f4b71Sopenharmony_ciThe default initial capacity of a lightweight map is 8, and it has capacity doubled in each expansion. 124e41f4b71Sopenharmony_ci 125e41f4b71Sopenharmony_ciCompared with [HashMap](../reference/apis-arkts/js-apis-hashmap.md), which can also store KV pairs, **LightWeightMap** occupies less memory. 126e41f4b71Sopenharmony_ci 127e41f4b71Sopenharmony_ciYou are advised to use **LightWeightMap** when you need to store and access **KV pairs**. 128e41f4b71Sopenharmony_ci 129e41f4b71Sopenharmony_ci**LightWeightMap** provides the following CRUD APIs. 130e41f4b71Sopenharmony_ci 131e41f4b71Sopenharmony_ci| Operation | Description | 132e41f4b71Sopenharmony_ci| -------- | ------ | 133e41f4b71Sopenharmony_ci| Create | Use **set(key: K, value: V)** to add an element (a KV pair) to this container. | 134e41f4b71Sopenharmony_ci| Read | Use **get(key: K)** to obtain the value of the specified key. | 135e41f4b71Sopenharmony_ci| Read | Use **getIndexOfKey(key: K)** to obtain the index of the specified key. | 136e41f4b71Sopenharmony_ci| Read | Use **getIndexOfValue(value: V)** to obtain the index of the first occurrence of the specified value. | 137e41f4b71Sopenharmony_ci| Read | Use **keys()** to return an iterator that contains all the keys in this container. | 138e41f4b71Sopenharmony_ci| Read | Use **values()** to return an iterator that contains all the values in this container. | 139e41f4b71Sopenharmony_ci| Read | Use **entries()** to return an iterator that contains all the elements in this container. | 140e41f4b71Sopenharmony_ci| Read | Use **getKeyAt(index: number)** to obtain the key of an element at a given position (specified by **index**). | 141e41f4b71Sopenharmony_ci| Read | Use **getValueAt(index: number)** to obtain the value of an element at a given position (specified by **index**). | 142e41f4b71Sopenharmony_ci| Read | Use **forEach(callbackFn: (value?: V, key?: K, map?: LightWeightMap<K, V>) => void, thisArg?: Object)** to traverse the elements in this container. | 143e41f4b71Sopenharmony_ci| Read | Use **\[Symbol.iterator]():IterableIterator<[K,V]>** for data access. | 144e41f4b71Sopenharmony_ci| Update | Use **setValueAt(index: number, newValue: V)** to change the value of an element at a given position (specified by **index**). | 145e41f4b71Sopenharmony_ci| Update | Use **forEach(callbackFn: (value?: V, key?: K, map?: LightWeightMap<K, V>) => void, thisArg?: Object)** to modify an element in this container. | 146e41f4b71Sopenharmony_ci| Delete | Use **remove(key: K)** to remove an element with the specified key. | 147e41f4b71Sopenharmony_ci| Delete | Use **removeAt(index: number)** to remove an element at a given position (specified by **index**). | 148e41f4b71Sopenharmony_ci| Delete | Use **clear()** to clear this container. | 149e41f4b71Sopenharmony_ci 150e41f4b71Sopenharmony_ci 151e41f4b71Sopenharmony_ci## LightWeightSet 152e41f4b71Sopenharmony_ci 153e41f4b71Sopenharmony_ci[LightWeightSet](../reference/apis-arkts/js-apis-lightweightset.md) is used to store a set of values, each of which is unique in a lightweight set. 154e41f4b71Sopenharmony_ci 155e41f4b71Sopenharmony_ci**LightWeightSet** uses generics and a lightweight structure. Its default initial capacity is 8, and it has capacity doubled in each expansion. In a lightweight set, a value is located by using the hash code and binary search algorithm. The hash code is stored in an array and mapped to a value in another array. The type of **value** must comply with the ECMA standard. 156e41f4b71Sopenharmony_ci 157e41f4b71Sopenharmony_ci**LightWeightSet** uses the hash code to uniquely identify a value at the bottom layer. It uses linear probing to avoid collisions and adopts the binary search algorithm. 158e41f4b71Sopenharmony_ci 159e41f4b71Sopenharmony_ciCompared with [HashSet](../reference/apis-arkts/js-apis-hashset.md), which can also store values, **LightWeightSet** occupies less memory. 160e41f4b71Sopenharmony_ci 161e41f4b71Sopenharmony_ciYou are advised to use **LightWeightSet** when you need a set that has only unique elements or need to deduplicate a set. 162e41f4b71Sopenharmony_ci 163e41f4b71Sopenharmony_ci**LightWeightSet** provides the following CRUD APIs. 164e41f4b71Sopenharmony_ci 165e41f4b71Sopenharmony_ci| Operation | Description | 166e41f4b71Sopenharmony_ci| -------- | ------ | 167e41f4b71Sopenharmony_ci| Create | Use **add(obj: T)** to add a value to this container. | 168e41f4b71Sopenharmony_ci| Read | Use **getIndexOf(key: T)** to obtain the index of a key. | 169e41f4b71Sopenharmony_ci| Read | Use **values()** to return an iterator that contains all the values in this container. | 170e41f4b71Sopenharmony_ci| Read | Use **entries()** to return an iterator that contains all the elements in this container. | 171e41f4b71Sopenharmony_ci| Read | Use **getValueAt(index: number)** to obtain the value of an element at a given position (specified by **index**). | 172e41f4b71Sopenharmony_ci| Read | Use **forEach(callbackFn: (value?: T, key?: T, set?: LightWeightSet\<T>) => void, thisArg?: Object)** to traverse the elements in this container. | 173e41f4b71Sopenharmony_ci| Read | Use **\[Symbol.iterator]():IterableIterator<T>** for data access. | 174e41f4b71Sopenharmony_ci| Update | Use **forEach(callbackFn: (value?: T, key?: T, set?: LightWeightSet\<T>) => void, thisArg?: Object)** to change a value in this container. | 175e41f4b71Sopenharmony_ci| Delete | Use **remove(key: K)** to remove an element with the specified key. | 176e41f4b71Sopenharmony_ci| Delete | Use **removeAt(index: number)** to remove an element at a given position (specified by **index**). | 177e41f4b71Sopenharmony_ci| Delete | Use **clear()** to clear this container. | 178e41f4b71Sopenharmony_ci 179e41f4b71Sopenharmony_ci 180e41f4b71Sopenharmony_ci## PlainArray 181e41f4b71Sopenharmony_ci 182e41f4b71Sopenharmony_ci[PlainArray](../reference/apis-arkts/js-apis-plainarray.md) is used to store a set of associated KV pairs. In a plain array, each key is unique, corresponds to a value, and is of the number type. **PlainArray** uses generics and a more lightweight structure. In a plain array, a key is located by using the binary search algorithm and is mapped to a value in another array. 183e41f4b71Sopenharmony_ci 184e41f4b71Sopenharmony_ciThe default initial capacity of a plain array is 16, and it has capacity doubled in each expansion. 185e41f4b71Sopenharmony_ci 186e41f4b71Sopenharmony_ciBoth **PlainArray** and [LightWeightMap](../reference/apis-arkts/js-apis-lightweightmap.md) are used to store KV pairs in the lightweight structure. However, the key type of **PlainArray** can only be **number**. 187e41f4b71Sopenharmony_ci 188e41f4b71Sopenharmony_ciYou are advised to use PlainArray when you need to store KV pairs whose keys are of the number type. 189e41f4b71Sopenharmony_ci 190e41f4b71Sopenharmony_ci**PlainArray** provides the following CRUD APIs. 191e41f4b71Sopenharmony_ci 192e41f4b71Sopenharmony_ci| Operation | Description | 193e41f4b71Sopenharmony_ci| -------- | ------ | 194e41f4b71Sopenharmony_ci| Create | Use **add(key: number,value: T)** to add an element (a KV pair) to this container. | 195e41f4b71Sopenharmony_ci| Read | Use **get(key: number)** to obtain the value of the specified key. | 196e41f4b71Sopenharmony_ci| Read | Use **getIndexOfKey(key: number)** to obtain the index of the specified key. | 197e41f4b71Sopenharmony_ci| Read | Use **getIndexOfValue(value: T)** to obtain the index of the specified value. | 198e41f4b71Sopenharmony_ci| Read | Use **getKeyAt(index: number)** to obtain the key of an element at a given position (specified by **index**). | 199e41f4b71Sopenharmony_ci| Read | Use **getValueAt(index: number)** to obtain the value of an element at a given position (specified by **index**). | 200e41f4b71Sopenharmony_ci| Read | Use **forEach(callbackFn: (value: T, index?: number, PlainArray?: PlainArray\<T>) => void, thisArg?: Object)** to traverse the elements in this container. | 201e41f4b71Sopenharmony_ci| Read | Use **\[Symbol.iterator]():IterableIterator<[number, T]>** for data access. | 202e41f4b71Sopenharmony_ci| Update | Use **setValueAt(index:number, value: T)** to change the value of an element at a given position (specified by **index**). | 203e41f4b71Sopenharmony_ci| Update | Use **forEach(callbackFn: (value: T, index?: number, PlainArray?: PlainArray\<T>) => void, thisArg?: Object)** to modify an element in this container. | 204e41f4b71Sopenharmony_ci| Delete | Use **remove(key: number)** to remove an element with the specified key. | 205e41f4b71Sopenharmony_ci| Delete | Use **removeAt(index: number)** to remove an element at a given position (specified by **index**). | 206e41f4b71Sopenharmony_ci| Delete | Use **removeRangeFrom(index: number, size: number)** to remove elements in a specified range. | 207e41f4b71Sopenharmony_ci| Delete | Use **clear()** to clear this container. | 208e41f4b71Sopenharmony_ci 209e41f4b71Sopenharmony_ci 210e41f4b71Sopenharmony_ci## Use of Nonlinear Containers 211e41f4b71Sopenharmony_ci 212e41f4b71Sopenharmony_ciRefer to the code snippet below to add, access, and modify elements in **HashMap**, **TreeMap**, **LightWeightMap**, **Stack**, and **PlainArray**. 213e41f4b71Sopenharmony_ci 214e41f4b71Sopenharmony_ci 215e41f4b71Sopenharmony_ci```ts 216e41f4b71Sopenharmony_ci// HashMap 217e41f4b71Sopenharmony_ciimport { HashMap } from '@kit.ArkTS'; // Import the HashMap module. 218e41f4b71Sopenharmony_ci 219e41f4b71Sopenharmony_cilet hashMap1: HashMap<string, number> = new HashMap(); 220e41f4b71Sopenharmony_cihashMap1.set('a', 123); 221e41f4b71Sopenharmony_cilet hashMap2: HashMap<number, number> = new HashMap(); 222e41f4b71Sopenharmony_cihashMap2.set(4, 123); // Add an element. 223e41f4b71Sopenharmony_ciconsole.info(`result: ${hashMap2.hasKey(4)}`); // Check whether an element is contained. 224e41f4b71Sopenharmony_ciconsole.info(`result: ${hashMap1.get('a')}`); // Access an element. 225e41f4b71Sopenharmony_ci 226e41f4b71Sopenharmony_ci// TreeMap 227e41f4b71Sopenharmony_ciimport { TreeMap } from '@kit.ArkTS'; // Import the TreeMap module. 228e41f4b71Sopenharmony_ci 229e41f4b71Sopenharmony_cilet treeMap: TreeMap<string, number> = new TreeMap(); 230e41f4b71Sopenharmony_citreeMap.set('a', 123); 231e41f4b71Sopenharmony_citreeMap.set('6', 356); // Add an element. 232e41f4b71Sopenharmony_ciconsole.info(`result: ${treeMap.get('a')}`); // Access an element. 233e41f4b71Sopenharmony_ciconsole.info(`result: ${treeMap.getFirstKey()}`); // Access the first element. 234e41f4b71Sopenharmony_ciconsole.info(`result: ${treeMap.getLastKey()}`); // Access the last element. 235e41f4b71Sopenharmony_ci 236e41f4b71Sopenharmony_ci// LightWeightMap 237e41f4b71Sopenharmony_ciimport { LightWeightMap } from '@kit.ArkTS'; // Import the LightWeightMap module. 238e41f4b71Sopenharmony_ci 239e41f4b71Sopenharmony_cilet lightWeightMap: LightWeightMap<string, number> = new LightWeightMap(); 240e41f4b71Sopenharmony_cilightWeightMap.set('x', 123); 241e41f4b71Sopenharmony_cilightWeightMap.set('8', 356); // Add an element. 242e41f4b71Sopenharmony_ciconsole.info(`result: ${lightWeightMap.get('a')}`); // Access an element. 243e41f4b71Sopenharmony_ciconsole.info(`result: ${lightWeightMap.get('x')}`); // Access an element. 244e41f4b71Sopenharmony_ciconsole.info(`result: ${lightWeightMap.getIndexOfKey('8')}`); // Access an element. 245e41f4b71Sopenharmony_ci 246e41f4b71Sopenharmony_ci// PlainArray 247e41f4b71Sopenharmony_ciimport { PlainArray } from '@kit.ArkTS'; // Import the PlainArray module. 248e41f4b71Sopenharmony_ci 249e41f4b71Sopenharmony_cilet plainArray: PlainArray<string> = new PlainArray(); 250e41f4b71Sopenharmony_ciplainArray.add(1, 'sdd'); 251e41f4b71Sopenharmony_ciplainArray.add(2,'sff'); // Add an element. 252e41f4b71Sopenharmony_ciconsole.info(`result: ${plainArray.get(1)}`); // Access an element. 253e41f4b71Sopenharmony_ciconsole.info(`result: ${plainArray.getKeyAt(1)}`); // Access an element. 254e41f4b71Sopenharmony_ci``` 255