• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Behavior Differences Between ArkTS Collections APIs and Native APIs
2
3ArkTS provides [Collections](../reference/apis-arkts/js-apis-arkts-collections.md) related to sendable data. The behavior of the Collections APIs is different from that of native APIs, as described below.
4
5## Array
6
7| Native API| ArkTS Collections API| Behavior Difference Exists| Different Behavior in ArkTS Collections|
8| --------------------------------- | ---- | ---- | ----------------- |
9| length: number |  readonly length: number  | Yes| To prevent the spread of undefined, it is not allowed to set **length**.|
10| new(arrayLength ?: number): any[] |  static create(arrayLength: number, initialValue: T): Array  | Yes| To prevent the spread of undefined, a constructor must be provided with an initial value.|
11| new \<T>(arrayLength: number): T[] |  constructor()  | No| / |
12| new \<T>(...items: T[]): T[] |  constructor(first: T, ...left: T[])  | Yes| To prevent the spread of undefined, a constructor must be provided with an initial value. In inheritance scenarios, this constructor cannot be called to construct an object.|
13| from\<T>(arrayLike: ArrayLike\<T>): T[] |  static from\<T>(arrayLike: ArrayLike\<T>): Array\<T>  | No| / |
14| pop(): T \| undefined |  pop(): T \| undefined  | Yes| It is not allowed to add, delete, or modify elements during traversal or access. Otherwise, an exception is thrown.|
15| push(...items: T[]): number |  push(...items: T[]): number  | Yes| It is not allowed to add, delete, or modify elements during traversal or access. Otherwise, an exception is thrown.|
16| concat(...items: ConcatArray\<T>[]): T[] |  concat(...items: ConcatArray\<T>[]): Array\<T>  | Yes| It is not allowed to add, delete, or modify elements during traversal or access. Otherwise, an exception is thrown.|
17| concat(...items: (T \| ConcatArray\<T>)[]): T[] |  concat(...items: ConcatArray\<T>[]): Array\<T>  | Yes| It is not allowed to add, delete, or modify elements during traversal or access. Otherwise, an exception is thrown.|
18| join(separator?: string): string |  join(separator?: string): string  | No| / |
19| shift(): T \| undefined |  shift(): T \| undefined  | Yes| It is not allowed to add, delete, or modify elements during traversal or access. Otherwise, an exception is thrown.|
20| slice(start?: number, end?: number): T[] |  slice(start?: number, end?: number): Array\<T>  | No| / |
21| sort(compareFn?: (a: T, b: T) => number): this |  sort(compareFn?: (a: T, b: T) => number): Array\<T>  | Yes| 1. It is not allowed to add, delete, or modify elements during traversal or access. Otherwise, an exception is thrown.<br>2. In inheritance scenarios, the actual return value type cannot be obtained.|
22| unshift(...items: T[]): number |  unshift(...items: T[]): number  | Yes| It is not allowed to add, delete, or modify elements during traversal or access. Otherwise, an exception is thrown.|
23| indexOf(searchElement: T, fromIndex?: number): number |  indexOf(searchElement: T, fromIndex?: number): number  | No| / |
24| forEach(callbackfn: (value: T, index: number, array: T[]) => void, thisArg?: any): void |  forEach(callbackFn: (value: T, index: number, array: Array\<T>) => void): void  | Yes| ArkTS does not support **this**. As a result, **thisArg** is not supported.|
25| map\<U>(callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): U[] |  map\<U>(callbackFn: (value: T, index: number, array: Array\<T>) => U): Array\<U>  | Yes| ArkTS does not support **this**. As a result, **thisArg** is not supported.|
26| filter(predicate: (value: T, index: number, array: T[]) => unknown, thisArg?: any): T[] |  filter(predicate: (value: T, index: number, array: Array\<T>) => boolean): Array\<T>  | Yes| ArkTS does not support **this**. As a result, **thisArg** is not supported.|
27| reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T): T |  reduce(callbackFn: (previousValue: T, currentValue: T, currentIndex: number, array: Array\<T>) => T): T  | No| / |
28| reduce\<U>(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U |  reduce\<U>(callbackFn: (previousValue: U, currentValue: T, currentIndex: number, array: Array\<T>) => U, initialValue: U): U | No| / |
29| [n: number]: T |  [index: number]: T  | Yes| It is not allowed to add, delete, or modify elements during traversal or access. Otherwise, an exception is thrown.|
30| findIndex(predicate: (value: T, index: number, obj: T[]) => unknown, thisArg?: any): number |  findIndex(predicate: (value: T, index: number, obj: Array\<T>) => boolean): number  | Yes| ArkTS does not support **this**. As a result, **thisArg** is not supported.|
31| fill(value: T, start?: number, end?: number): this |  fill(value: T, start?: number, end?: number): Array\<T>  | Yes| 1. It is not allowed to add, delete, or modify elements during traversal or access. Otherwise, an exception is thrown.<br>2. In inheritance scenarios, the actual return value type cannot be obtained.|
32| entries(): IterableIterator\<[number, T]> | entries(): IterableIterator\<[number, T]>  | No| / |
33| keys(): IterableIterator\<number> | keys(): IterableIterator\<number>  | No| / |
34| values(): IterableIterator\<T> | values(): IterableIterator\<T>  | No| / |
35| includes(searchElement: T, fromIndex?: number): boolean | includes(searchElement: T, fromIndex?: number): boolean  | No| / |
36| at(index: number): T \| undefined | at(index: number): T \| undefined  | No| / |
37
38## ArrayBuffer
39
40| Native API| ArkTS Collections API| Behavior Difference Exists| Different Behavior in ArkTS Collections|
41| --------------------------------- | ---- | ---- | ----------------- |
42| readonly byteLength: number | readonly byteLength: number  | No| / |
43| slice(begin: number, end?: number): ArrayBuffer | slice(begin: number, end?: number): ArrayBuffer  | No| / |
44| new(byteLength: number): ArrayBuffer | constructor(byteLength: number)  | No| / |
45
46## TypedArray (Int8Array Used as an Example)
47
48| Native API| ArkTS Collections API| Behavior Difference Exists| Different Behavior in ArkTS Collections|
49| --------------------------------- | ---- | ---- | ----------------- |
50| readonly buffer: ArrayBufferLike | readonly buffer: ArrayBuffer | No| / |
51| readonly byteLength: number | readonly byteLength: number  | No| / |
52| readonly byteOffset: number | readonly byteOffset: number | No| / |
53| readonly length: number | readonly length: number | No| / |
54| readonly BYTES_PER_ELEMENT: number | static readonly BYTES_PER_ELEMENT: number | No| / |
55| copyWithin(target: number, start: number, end?: number): this | copyWithin(target: number, start: number, end?: number): Int8Array | Yes| It is not allowed to add, delete, or modify elements during traversal or access. Otherwise, an exception is thrown.|
56| every(predicate: (value: number, index: number, array: Int8Array) => unknown, thisArg?: any): boolean | every(predicate: TypedArrayPredicateFn\<number, Int8Array>): boolean | Yes| 1. It is not allowed to add, delete, or modify elements during traversal or access. Otherwise, an exception is thrown.<br>2. ArkTS does not support **this**. As a result, **thisArg** is not supported.|
57| fill(value: number, start?: number, end?: number): this | fill(value: number, start?: number, end?: number): Int8Array | Yes| It is not allowed to add, delete, or modify elements during traversal or access. Otherwise, an exception is thrown.|
58| filter(predicate: (value: number, index: number, array: Int8Array) => any, thisArg?: any): Int8Array | filter(predicate: TypedArrayPredicateFn\<number, Int8Array>): Int8Array | Yes| 1. It is not allowed to add, delete, or modify elements during traversal or access. Otherwise, an exception is thrown.<br>2. ArkTS does not support **this**. As a result, **thisArg** is not supported.|
59| find(predicate: (value: number, index: number, obj: Int8Array) => boolean, thisArg?: any): number \| undefined | find(predicate: TypedArrayPredicateFn\<number, Int8Array>): number \| undefined | Yes| 1. It is not allowed to add, delete, or modify elements during traversal or access. Otherwise, an exception is thrown.<br>2. ArkTS does not support **this**. As a result, **thisArg** is not supported.|
60| findIndex(predicate: (value: number, index: number, obj: Int8Array) => boolean, thisArg?: any): number | findIndex(predicate: TypedArrayPredicateFn\<number, Int8Array>): number | Yes| ArkTS does not support **this**. As a result, **thisArg** is not supported.|
61| forEach(callbackfn: (value: number, index: number, array: Int8Array) => void, thisArg?: any): void | forEach(callbackFn: (value: number, index: number, array: Int8Array) => void): void | Yes| 1. It is not allowed to add, delete, or modify elements during traversal or access. Otherwise, an exception is thrown.<br>2. ArkTS does not support **this**. As a result, **thisArg** is not supported.|
62| indexOf(searchElement: number, fromIndex?: number): number | indexOf(searchElement: number, fromIndex?: number): number | No| / |
63| join(separator?: string): string | join(separator?: string): string | No| / |
64| map(callbackfn: (value: number, index: number, array: Int8Array) => number, thisArg?: any): Int8Array | map(callbackFn: TypedArrayForEachCallback\<number, Int8Array>): Int8Array | Yes| 1. It is not allowed to add, delete, or modify elements during traversal or access. Otherwise, an exception is thrown.<br>2. ArkTS does not support **this**. As a result, **thisArg** is not supported.|
65| reduce(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Int8Array) => number): number | reduce(callbackFn: TypedArrayReduceCallback\<number, number, Int8Array>): number | No| / |
66| reduce(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Int8Array) => number, initialValue: number): number | reduce(callbackFn: TypedArrayReduceCallback\<number, number, Int8Array>, initialValue: number): number | No| / |
67| reduce\<U>(callbackfn: (previousValue: U, currentValue: number, currentIndex: number, array: Int8Array) => U, initialValue: U): U | reduce\<U>(callbackFn: TypedArrayReduceCallback\<U, number, Int8Array>, initialValue: U): U | No| / |
68| reverse(): Int8Array | reverse(): Int8Array | No| / |
69| set(array: ArrayLike\<number>, offset?: number): void | set(array: ArrayLike\<number>, offset?: number): void | Yes| It is not allowed to add, delete, or modify elements during traversal or access. Otherwise, an exception is thrown.|
70| slice(start?: number, end?: number): Int8Array | slice(start?: number, end?: number): Int8Array | No| / |
71| some(predicate: (value: number, index: number, array: Int8Array) => unknown, thisArg?: any): boolean | some(predicate: TypedArrayPredicateFn\<number, Int8Array>): boolean | Yes| ArkTS does not support **this**. As a result, **thisArg** is not supported.|
72| sort(compareFn?: (a: number, b: number) => number): this | sort(compareFn?: TypedArrayCompareFn\<number>): Int8Array | Yes| 1. It is not allowed to add, delete, or modify elements during traversal or access. Otherwise, an exception is thrown.<br>2. In inheritance scenarios, the actual return value type cannot be obtained.|
73| subarray(begin?: number, end?: number): Int8Array | subarray(begin?: number, end?: number): Int8Array | Yes| It is not allowed to add, delete, or modify elements during traversal or access. Otherwise, an exception is thrown.|
74| [index: number]: number | [index: number]: number | Yes| It is not allowed to add, delete, or modify elements during traversal or access. Otherwise, an exception is thrown.|
75| entries(): IterableIterator\<[number, number]> | entries(): IterableIterator\<[number, number]> | No| / |
76| keys(): IterableIterator\<number> | keys(): IterableIterator\<number> | No| / |
77| values(): IterableIterator\<number> | values(): IterableIterator\<number> | No| / |
78| includes(searchElement: number, fromIndex?: number): boolean | includes(searchElement: number, fromIndex?: number): boolean | No| / |
79| at(index: number): number \| undefined | at(index: number): number \| undefined | No| / |
80| new(length: number): Int8Array | constructor(length: number) | No| / |
81| new(array: ArrayLike\<number> \| ArrayBufferLike): Int8Array | constructor(array: ArrayLike\<number> \| ArrayBuffer) | No| / |
82| new(buffer: ArrayBufferLike, byteOffset?: number, length?: number): Int8Array | constructor(buffer: ArrayBuffer, byteOffset?: number, length?: number) | No| / |
83| from(arrayLike: ArrayLike\<number>): Int8Array | static from(arrayLike: ArrayLike\<number>): Int8Array | No| / |
84| from\<T>(arrayLike: ArrayLike\<T>, mapfn: (v: T, k: number) => number, thisArg?: any): Int8Array | static from\<T>(arrayLike: ArrayLike\<T>, mapFn: TypedArrayFromMapFn\<T, number>): Int8Array | Yes| ArkTS does not support **this**. As a result, **thisArg** is not supported.|
85| from(arrayLike: Iterable\<number>, mapfn?: (v: number, k: number) => number, thisArg?: any): Int8Array | static from(arrayLike: Iterable\<number>, mapFn?: TypedArrayFromMapFn\<number, number>): Int8Array | Yes| ArkTS does not support **this**. As a result, **thisArg** is not supported.|
86
87## Map
88
89| Native API| ArkTS Collections API| Behavior Difference Exists| Different Behavior in ArkTS Collections|
90| --------------------------------- | ---- | ---- | ----------------- |
91| readonly size: number | readonly size: number  | Yes| It is not allowed to add, delete, or modify elements during traversal or access. Otherwise, an exception is thrown.|
92| clear(): void | clear(): void  | Yes| It is not allowed to add, delete, or modify elements during traversal or access. Otherwise, an exception is thrown.|
93| delete(key: K): boolean | delete(key: K): boolean  | Yes| It is not allowed to add, delete, or modify elements during traversal or access. Otherwise, an exception is thrown.|
94| forEach(callbackfn: (value: V, key: K, map: Map\<K, V>) => void, thisArg?: any): void | forEach(callbackFn: (value: V, key: K, map: Map\<K, V>) => void): void  | Yes| 1. It is not allowed to add, delete, or modify elements during traversal or access. Otherwise, an exception is thrown.<br>2. ArkTS does not support **this**. As a result, **thisArg** is not supported.|
95| get(key: K): V \| undefined | get(key: K): V \| undefined  | Yes| It is not allowed to add, delete, or modify elements during traversal or access. Otherwise, an exception is thrown.|
96| has(key: K): boolean | has(key: K): boolean  | Yes| It is not allowed to add, delete, or modify elements during traversal or access. Otherwise, an exception is thrown.|
97| set(key: K, value: V): this | set(key: K, value: V): Map\<K, V>  | Yes| It is not allowed to add, delete, or modify elements during traversal or access. Otherwise, an exception is thrown.|
98| entries(): IterableIterator\<[K, V]> | entries(): IterableIterator\<[K, V]>  | No| / |
99| keys(): IterableIterator\<K> | keys(): IterableIterator\<K>  | No| / |
100| values(): IterableIterator\<V> | values(): IterableIterator\<V>  | No| / |
101| new \<K, V>(entries?: readonly (readonly [K, V])[] \| null): Map\<K, V> | constructor(entries?: readonly (readonly [K, V])[] \| null)  | Yes| The passed-in values of **k** and **v** during construction cannot be [non-sendable data](arkts-sendable.md). Otherwise, an error is reported during compilation.|
102
103## Set
104
105| Native API| ArkTS Collections API| Behavior Difference Exists| Different Behavior in ArkTS Collections|
106| --------------------------------- | ---- | ---- | ----------------- |
107| readonly size: number | readonly size: number  | Yes| The **arkts-sendable-compated-prop-name** cannot be used in the Sendable class and APIs.|
108| add(value: T): this | add(value: T): Set\<T>  | Yes| It is not allowed to add, delete, or modify elements during traversal or access. Otherwise, an exception is thrown.|
109| clear(): void | clear(): void  | Yes| It is not allowed to add, delete, or modify elements during traversal or access. Otherwise, an exception is thrown.|
110| delete(value: T): boolean | delete(value: T): boolean  | Yes| It is not allowed to add, delete, or modify elements during traversal or access. Otherwise, an exception is thrown.|
111| forEach(callbackfn: (value: T, value2: T, set: Set\<T>) => void, thisArg?: any): void | forEach(callbackFn: (value: T, value2: T, set: Set\<T>) => void): void  | Yes| 1. It is not allowed to add, delete, or modify elements during traversal or access. Otherwise, an exception is thrown.<br>2. ArkTS does not support **this**. As a result, **thisArg** is not supported.|
112| has(value: T): boolean | has(value: T): boolean  | Yes| It is not allowed to add, delete, or modify elements during traversal or access. Otherwise, an exception is thrown.|
113| entries(): IterableIterator\<[T, T]> | entries(): IterableIterator\<[T, T]>  | No| / |
114| keys(): IterableIterator\<T> | keys(): IterableIterator\<T>  | No| / |
115| values(): IterableIterator\<T> | values(): IterableIterator\<T>  | Yes| The **arkts-sendable-compated-prop-name** cannot be used in the Sendable class and APIs.|
116| new \<T = any>(values?: readonly T[] \| null): Set\<T> | constructor(values?: readonly T[] \| null)  | Yes| The passed-in values during construction cannot be [non-sendable data](arkts-sendable.md). Otherwise, an error is reported during compilation.|
117