DEV Community

SameX
SameX

Posted on

The World of HarmonyOS Programming: Behavioral Differences between ArkTS Containers and Native Containers

This article aims to deeply explore the technical details of the Huawei HarmonyOS Next system (up to API 12 as of now), and is summarized based on actual development practices. It mainly serves as a vehicle for technical sharing and communication. Mistakes and omissions are inevitable. Colleagues are welcome to put forward valuable opinions and questions so that we can make进步 together. This article is original content, and any form of reprint must indicate the source and the original author.
ArkTS provides a set of container types, including Array, Map, and Set, etc., which are used to store and manage data. There are some differences in behavior between ArkTS containers and JavaScript native containers, which developers need to pay attention to.

ArkTS Container Types such as Array, Map, and Set

  • Array: The Array type in ArkTS is similar to the Array type in JavaScript, but there are some differences. For example, it is not allowed to add, delete, or modify elements during traversal or access.
  • Map: The Map type in ArkTS is similar to the Map type in JavaScript, but there are some differences. For example, the constructor must be provided with a constructor of an initial value, and the use of computed property names is not supported.
  • Set: The Set type in ArkTS is similar to the Set type in JavaScript, but there are some differences. For example, it is not allowed to add, delete, or modify elements during traversal or access, and the use of computed property names is not allowed in Sendable classes and interfaces. ### Differences between Native APIs and ArkTS APIs | Native API | ArkTS API | |---|---| | Array.from | collections.Array.from | | Array.slice | collections.Array.slice | | Map.entries | collections.Map.entries | | Map.keys | collections.Map.keys | | Map.values | collections.Map.values | | Set.add | collections.Set.add | | Set.delete | collections.Set.delete | | Set.has | collections.Set.has | ### Application of ArkTS Containers in Concurrency ArkTS containers can be safely passed between concurrent instances, avoiding data race problems. However, ArkTS containers are not thread-safe and use a fail-fast mechanism internally. Therefore, when using ArkTS containers in a concurrent environment, an asynchronous lock mechanism needs to be used to ensure the safe access of the containers. ### Examples of Creating and Operating ArkTS Containers The following is a simple example demonstrating how to create and operate ArkTS containers:
import { collections } from '@kit.ArkTS';
@Entry
@Component
struct Index {
    @State message: string = 'Hello World';
    build() {
        Column() {
            Text(this.message)
             .fontSize(50)
             .fontWeight(FontWeight.Bold)
             .onClick(async () => {
                    // Create an Array
                    const arr = new collections.Array<number>();
                    arr.push(1);
                    arr.push(2);
                    arr.push(3);
                    console.log(arr); // Output: [1, 2, 3]
                    // Create a Map
                    const map = new collections.Map<number, string>();
                    map.set(1, 'one');
                    map.set(2, 'two');
                    map.set(3, 'three');
                    console.log(map); // Output: {1: "one", 2: "two", 3: "three"}
                    // Create a Set
                    const set = new collections.Set<string>();
                    set.add('one');
                    set.add('oneself');
                    set.add('two');
                    set.add('three');
                    console.log(set); // Output: Set { "one", "two", "three" }
                })
             .width('100%');
        }
     .height('100%');
    }
}
Enter fullscreen mode Exit fullscreen mode

This code defines a component named Index and displays a text message "Hello World" in the component. Clicking the button will create Array, Map, and Set containers and output the contents of the containers.

Comparison Table between ArkTS Containers and Native APIs

Native API ArkTS API Difference Points
Array.length collections.Array.length Not allowed to set length
Array.pop collections.Array.pop Not allowed to operate on elements during traversal or access
Array.push collections.Array.push Not allowed to operate on elements during traversal or access
Array.concat collections.Array.concat Not allowed to operate on elements during traversal or access
Map.new collections.Map.create Must provide an initial value
Map.entries collections.Map.entries Does not support thisArg
Map.keys collections.Map.keys Does not support thisArg
Map.values collections.Map.values Does not support thisArg
Set.add collections.Set.add Not allowed to operate on elements during traversal or access
Set.delete collections.Set.delete Not allowed to operate on elements during traversal or access
Set.has collections.Set.has Does not support thisArg

Summary

Through the above introduction, you can understand the differences between ArkTS containers and native containers in the HarmonyOS system, as well as the application of ArkTS containers in concurrency. Hope this article can help you master the concurrent programming techniques in the HarmonyOS system and develop better HarmonyOS applications.

Top comments (0)