• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# ArkTS Changelog
2
3## cl.arkts.1 Forward Fix for TreeSet and TreeMap Comparator Loss on Capacity Expansion
4
5**Access Level**
6
7Public API
8
9**Reason for Change**
10
11When the **add** interface of the TreeSet or TreeMap module triggers a capacity expansion, the custom comparator for TreeSet or TreeMap is lost after the capacity expansion, causing the system to resort to default sorting.
12
13**Impact of the Change**
14
15This change requires application adaptation.
16
17**Before the Change**
18
19For the following code, the expected results do not match the actual execution results, leading to errors.
20
21The reason is that the comparator is lost after capacity expansion, causing **remove(a1)** to fail and subsequent behavior to become abnormal.
22
23```ts
24import TreeSet from '@kit.ArkTS';
25class A {
26time: number;
27constructor(time: number) {
28  this.time = time;
29}
30static readonly compared = ((first: A, second: A): number => {
31    return second.time - first.time;
32  });
33}
34const a1 = new A(1);
35const a2 = new A(2);
36const a3 = new A(3);
37const a4 = new A(4);
38const a5 = new A(5);
39const a6 = new A(6);
40const set = new TreeSet<A>(A.compared); // Comparator A.compared is lost after add triggers a capacity expansion.
41set.add(a1);
42set.add(a2);
43set.add(a3); // Capacity expansion is triggered, and A.compared is lost.
44set.add(a4);
45set.add(a5);
46set.add(a6);
47for (let i = 0; i < 5; ++i) {
48  set.remove(a1); // Two different comparison rules used before and after the capacity expansion, and the data structure integrity is compromised.
49  console.log(set.has(a1));
50  // Expected result: false, false, false, false, false
51  // Actual result: false, false, true, true, true
52  set.add(a1);
53}
54for (let item of set) {
55  console.log(item.time);
56  // Expected result: 6, 5, 4, 3, 2, and 1
57  // Actual result: 6, 1, 1
58}
59```
60
61**After modification:**
62
63The **TaggedTree** comparator remains consistent before and after the capacity expansion. All **add** and **remove** operations on **TaggedTree** use the same comparison rule, and the output results match expectations.
64
65**Start API Level**
66
678
68
69**Change Since**
70
71OpenHarmony SDK 6.0
72
73**Key API/Component Changes**
74
75TreeSet, TreeMap
76
77**Adaptation Guide**
78
79This is a behavioral change. In most cases, no adaptation is required.
80
81If you use custom comparators and have relied on the incorrect results as correct, you should be aware of the changes in TreeSet and TreeMap results and adapt your code according to the fixed results.
82