1e41f4b71Sopenharmony_ci# @ohos.util.TreeSet (非线性容器TreeSet)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciTreeSet基于[TreeMap](js-apis-treemap.md)实现,在TreeSet中,只对value对象进行处理。TreeSet可用于存储一系列值的集合,元素中value唯一且有序。
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ciTreeSet和[HashSet](js-apis-hashset.md)相比,HashSet中的数据无序存放,而TreeSet是有序存放。它们集合中的元素都不允许重复,但HashSet允许放入null值,TreeSet不建议插入空值,可能会影响排序结果。
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci**推荐使用场景:** 一般需要存储有序集合的场景,可以使用TreeSet。
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci文档中存在泛型的使用,涉及以下泛型标记符:
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci- T:Type,类
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci> **说明:**
14e41f4b71Sopenharmony_ci>
15e41f4b71Sopenharmony_ci> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_ci## 导入模块
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ci```ts
21e41f4b71Sopenharmony_ciimport { TreeSet } from '@kit.ArkTS';
22e41f4b71Sopenharmony_ci```
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ci## TreeSet
25e41f4b71Sopenharmony_ci
26e41f4b71Sopenharmony_ci### 属性
27e41f4b71Sopenharmony_ci
28e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang
31e41f4b71Sopenharmony_ci
32e41f4b71Sopenharmony_ci| 名称 | 类型 | 可读 | 可写 | 说明 |
33e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | -------- |
34e41f4b71Sopenharmony_ci| length | number | 是 | 否 | TreeSet的元素个数。 |
35e41f4b71Sopenharmony_ci
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci### constructor
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ciconstructor(comparator?: (firstValue: T, secondValue: T) => boolean)
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ciTreeSet的构造函数,支持通过比较函数对元素进行升序或降序排序。
42e41f4b71Sopenharmony_ci
43e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
44e41f4b71Sopenharmony_ci
45e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang
46e41f4b71Sopenharmony_ci
47e41f4b71Sopenharmony_ci**参数:**
48e41f4b71Sopenharmony_ci
49e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 |
50e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
51e41f4b71Sopenharmony_ci| comparator | function | 否 | 用户自定义的比较函数,可通过比较关系对元素进行排序。默认值为hole(一个空白占位符),表示不提供比较函数。|
52e41f4b71Sopenharmony_ci
53e41f4b71Sopenharmony_ci**错误码:**
54e41f4b71Sopenharmony_ci
55e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
56e41f4b71Sopenharmony_ci
57e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
58e41f4b71Sopenharmony_ci| -------- | -------- |
59e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. |
60e41f4b71Sopenharmony_ci| 10200012 | The TreeSet's constructor cannot be directly invoked. |
61e41f4b71Sopenharmony_ci
62e41f4b71Sopenharmony_ci**示例:**
63e41f4b71Sopenharmony_ci
64e41f4b71Sopenharmony_ci```ts
65e41f4b71Sopenharmony_ci//默认构造
66e41f4b71Sopenharmony_cilet treeSet : TreeSet<string | number | boolean | Object> = new TreeSet();
67e41f4b71Sopenharmony_ci```
68e41f4b71Sopenharmony_ci
69e41f4b71Sopenharmony_ci```ts
70e41f4b71Sopenharmony_ci//使用comparator firstValue < secondValue,表示期望结果为升序排序。反之firstValue > secondValue,表示为降序排序。
71e41f4b71Sopenharmony_cilet treeSet : TreeSet<string> = new TreeSet<string>((firstValue: string, secondValue: string) : boolean => {return firstValue < secondValue});
72e41f4b71Sopenharmony_citreeSet.add("a");
73e41f4b71Sopenharmony_citreeSet.add("c");
74e41f4b71Sopenharmony_citreeSet.add("d");
75e41f4b71Sopenharmony_citreeSet.add("b");
76e41f4b71Sopenharmony_cilet numbers = Array.from(treeSet.values())
77e41f4b71Sopenharmony_cifor (let item of numbers) {
78e41f4b71Sopenharmony_ci  console.log("TreeSet:" + item);
79e41f4b71Sopenharmony_ci}
80e41f4b71Sopenharmony_ci```
81e41f4b71Sopenharmony_ci
82e41f4b71Sopenharmony_ci```ts
83e41f4b71Sopenharmony_ci//当插入自定义类型时,则必须要提供比较函数。
84e41f4b71Sopenharmony_ciclass TestEntry{
85e41f4b71Sopenharmony_ci  public id: number = 0;
86e41f4b71Sopenharmony_ci}
87e41f4b71Sopenharmony_cilet ts1: TreeSet<TestEntry> = new TreeSet<TestEntry>((t1: TestEntry, t2: TestEntry): boolean => {return t1.id > t2.id;});
88e41f4b71Sopenharmony_cilet entry1: TestEntry = {
89e41f4b71Sopenharmony_ci  id: 0
90e41f4b71Sopenharmony_ci};
91e41f4b71Sopenharmony_cilet entry2: TestEntry = {
92e41f4b71Sopenharmony_ci  id: 1
93e41f4b71Sopenharmony_ci}
94e41f4b71Sopenharmony_cits1.add(entry1);
95e41f4b71Sopenharmony_cits1.add(entry2);
96e41f4b71Sopenharmony_ciconsole.log("treeSet: ", ts1.length);
97e41f4b71Sopenharmony_ci```
98e41f4b71Sopenharmony_ci
99e41f4b71Sopenharmony_ci
100e41f4b71Sopenharmony_ci### isEmpty
101e41f4b71Sopenharmony_ci
102e41f4b71Sopenharmony_ciisEmpty(): boolean
103e41f4b71Sopenharmony_ci
104e41f4b71Sopenharmony_ci判断该容器是否为空。
105e41f4b71Sopenharmony_ci
106e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
107e41f4b71Sopenharmony_ci
108e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang
109e41f4b71Sopenharmony_ci
110e41f4b71Sopenharmony_ci**返回值:**
111e41f4b71Sopenharmony_ci
112e41f4b71Sopenharmony_ci| 类型 | 说明 |
113e41f4b71Sopenharmony_ci| -------- | -------- |
114e41f4b71Sopenharmony_ci| boolean | 为空返回true,不为空返回false。 |
115e41f4b71Sopenharmony_ci
116e41f4b71Sopenharmony_ci**错误码:**
117e41f4b71Sopenharmony_ci
118e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
119e41f4b71Sopenharmony_ci
120e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
121e41f4b71Sopenharmony_ci| -------- | -------- |
122e41f4b71Sopenharmony_ci| 10200011 | The isEmpty method cannot be bound. |
123e41f4b71Sopenharmony_ci
124e41f4b71Sopenharmony_ci**示例:**
125e41f4b71Sopenharmony_ci
126e41f4b71Sopenharmony_ci```ts
127e41f4b71Sopenharmony_ciconst treeSet : TreeSet<string | number | boolean | Object>  = new TreeSet();
128e41f4b71Sopenharmony_cilet result = treeSet.isEmpty();
129e41f4b71Sopenharmony_ci```
130e41f4b71Sopenharmony_ci
131e41f4b71Sopenharmony_ci
132e41f4b71Sopenharmony_ci### has
133e41f4b71Sopenharmony_ci
134e41f4b71Sopenharmony_cihas(value: T): boolean
135e41f4b71Sopenharmony_ci
136e41f4b71Sopenharmony_ci判断此容器中是否含有该指定元素。
137e41f4b71Sopenharmony_ci
138e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
139e41f4b71Sopenharmony_ci
140e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang
141e41f4b71Sopenharmony_ci
142e41f4b71Sopenharmony_ci**参数:**
143e41f4b71Sopenharmony_ci
144e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 |
145e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
146e41f4b71Sopenharmony_ci| value | T | 是 | 指定元素。 |
147e41f4b71Sopenharmony_ci
148e41f4b71Sopenharmony_ci**返回值:**
149e41f4b71Sopenharmony_ci
150e41f4b71Sopenharmony_ci| 类型 | 说明 |
151e41f4b71Sopenharmony_ci| -------- | -------- |
152e41f4b71Sopenharmony_ci| boolean | 包含指定元素返回true,否则返回false。 |
153e41f4b71Sopenharmony_ci
154e41f4b71Sopenharmony_ci**错误码:**
155e41f4b71Sopenharmony_ci
156e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
157e41f4b71Sopenharmony_ci
158e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
159e41f4b71Sopenharmony_ci| -------- | -------- |
160e41f4b71Sopenharmony_ci| 10200011 | The has method cannot be bound. |
161e41f4b71Sopenharmony_ci
162e41f4b71Sopenharmony_ci**示例:**
163e41f4b71Sopenharmony_ci
164e41f4b71Sopenharmony_ci```ts
165e41f4b71Sopenharmony_cilet treeSet : TreeSet<number> = new TreeSet();
166e41f4b71Sopenharmony_citreeSet.add(123);
167e41f4b71Sopenharmony_cilet result = treeSet.has(123);
168e41f4b71Sopenharmony_ci```
169e41f4b71Sopenharmony_ci
170e41f4b71Sopenharmony_ci### getFirstValue
171e41f4b71Sopenharmony_ci
172e41f4b71Sopenharmony_cigetFirstValue(): T
173e41f4b71Sopenharmony_ci
174e41f4b71Sopenharmony_ci获取容器中排序第一的数据,为空时返回undefined。
175e41f4b71Sopenharmony_ci
176e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
177e41f4b71Sopenharmony_ci
178e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang
179e41f4b71Sopenharmony_ci
180e41f4b71Sopenharmony_ci**返回值:**
181e41f4b71Sopenharmony_ci
182e41f4b71Sopenharmony_ci| 类型 | 说明 |
183e41f4b71Sopenharmony_ci| -------- | -------- |
184e41f4b71Sopenharmony_ci| T | 返回排序第一的数据,为空时返回undefined。 |
185e41f4b71Sopenharmony_ci
186e41f4b71Sopenharmony_ci**错误码:**
187e41f4b71Sopenharmony_ci
188e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
189e41f4b71Sopenharmony_ci
190e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
191e41f4b71Sopenharmony_ci| -------- | -------- |
192e41f4b71Sopenharmony_ci| 10200011 | The getFirstValue method cannot be bound. |
193e41f4b71Sopenharmony_ci
194e41f4b71Sopenharmony_ci**示例:**
195e41f4b71Sopenharmony_ci
196e41f4b71Sopenharmony_ci```ts
197e41f4b71Sopenharmony_cilet treeSet : TreeSet<string> = new TreeSet();
198e41f4b71Sopenharmony_citreeSet.add("squirrel");
199e41f4b71Sopenharmony_citreeSet.add("sparrow");
200e41f4b71Sopenharmony_cilet result = treeSet.getFirstValue();
201e41f4b71Sopenharmony_ci```
202e41f4b71Sopenharmony_ci
203e41f4b71Sopenharmony_ci
204e41f4b71Sopenharmony_ci### getLastValue
205e41f4b71Sopenharmony_ci
206e41f4b71Sopenharmony_cigetLastValue(): T
207e41f4b71Sopenharmony_ci
208e41f4b71Sopenharmony_ci获取容器中排序最后的数据,为空时返回undefined。
209e41f4b71Sopenharmony_ci
210e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
211e41f4b71Sopenharmony_ci
212e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang
213e41f4b71Sopenharmony_ci
214e41f4b71Sopenharmony_ci**返回值:**
215e41f4b71Sopenharmony_ci
216e41f4b71Sopenharmony_ci| 类型 | 说明 |
217e41f4b71Sopenharmony_ci| -------- | -------- |
218e41f4b71Sopenharmony_ci| T | 返回排序最后的数据,为空时返回undefined。 |
219e41f4b71Sopenharmony_ci
220e41f4b71Sopenharmony_ci**错误码:**
221e41f4b71Sopenharmony_ci
222e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
223e41f4b71Sopenharmony_ci
224e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
225e41f4b71Sopenharmony_ci| -------- | -------- |
226e41f4b71Sopenharmony_ci| 10200011 | The getLastValue method cannot be bound. |
227e41f4b71Sopenharmony_ci
228e41f4b71Sopenharmony_ci**示例:**
229e41f4b71Sopenharmony_ci
230e41f4b71Sopenharmony_ci```ts
231e41f4b71Sopenharmony_cilet treeSet : TreeSet<string> = new TreeSet();
232e41f4b71Sopenharmony_citreeSet.add("squirrel");
233e41f4b71Sopenharmony_citreeSet.add("sparrow");
234e41f4b71Sopenharmony_cilet result = treeSet.getLastValue();
235e41f4b71Sopenharmony_ci```
236e41f4b71Sopenharmony_ci
237e41f4b71Sopenharmony_ci
238e41f4b71Sopenharmony_ci### add
239e41f4b71Sopenharmony_ci
240e41f4b71Sopenharmony_ciadd(value: T): boolean
241e41f4b71Sopenharmony_ci
242e41f4b71Sopenharmony_ci向容器中添加一组数据。
243e41f4b71Sopenharmony_ci
244e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
245e41f4b71Sopenharmony_ci
246e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang
247e41f4b71Sopenharmony_ci
248e41f4b71Sopenharmony_ci**参数:**
249e41f4b71Sopenharmony_ci
250e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 |
251e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
252e41f4b71Sopenharmony_ci| value | T | 是 | 添加的成员数据。 |
253e41f4b71Sopenharmony_ci
254e41f4b71Sopenharmony_ci**返回值:**
255e41f4b71Sopenharmony_ci
256e41f4b71Sopenharmony_ci| 类型 | 说明 |
257e41f4b71Sopenharmony_ci| -------- | -------- |
258e41f4b71Sopenharmony_ci| boolean | 成功添加新数据至容器返回true,否则返回false。 |
259e41f4b71Sopenharmony_ci
260e41f4b71Sopenharmony_ci**错误码:**
261e41f4b71Sopenharmony_ci
262e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
263e41f4b71Sopenharmony_ci
264e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
265e41f4b71Sopenharmony_ci| -------- | -------- |
266e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
267e41f4b71Sopenharmony_ci| 10200011 | The add method cannot be bound. |
268e41f4b71Sopenharmony_ci
269e41f4b71Sopenharmony_ci**示例:**
270e41f4b71Sopenharmony_ci
271e41f4b71Sopenharmony_ci```ts
272e41f4b71Sopenharmony_cilet treeSet : TreeSet<string> = new TreeSet();
273e41f4b71Sopenharmony_cilet result = treeSet.add("squirrel");
274e41f4b71Sopenharmony_ci```
275e41f4b71Sopenharmony_ci
276e41f4b71Sopenharmony_ci
277e41f4b71Sopenharmony_ci### remove
278e41f4b71Sopenharmony_ci
279e41f4b71Sopenharmony_ciremove(value: T): boolean
280e41f4b71Sopenharmony_ci
281e41f4b71Sopenharmony_ci删除指定的元素。
282e41f4b71Sopenharmony_ci
283e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
284e41f4b71Sopenharmony_ci
285e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang
286e41f4b71Sopenharmony_ci
287e41f4b71Sopenharmony_ci**参数:**
288e41f4b71Sopenharmony_ci
289e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 |
290e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
291e41f4b71Sopenharmony_ci| value | T | 是 | 指定的元素。 |
292e41f4b71Sopenharmony_ci
293e41f4b71Sopenharmony_ci**返回值:**
294e41f4b71Sopenharmony_ci
295e41f4b71Sopenharmony_ci| 类型 | 说明 |
296e41f4b71Sopenharmony_ci| -------- | -------- |
297e41f4b71Sopenharmony_ci| boolean | 成功删除元素返回true,否则返回false。 |
298e41f4b71Sopenharmony_ci
299e41f4b71Sopenharmony_ci**错误码:**
300e41f4b71Sopenharmony_ci
301e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
302e41f4b71Sopenharmony_ci
303e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
304e41f4b71Sopenharmony_ci| -------- | -------- |
305e41f4b71Sopenharmony_ci| 10200011 | The remove method cannot be bound. |
306e41f4b71Sopenharmony_ci
307e41f4b71Sopenharmony_ci**示例:**
308e41f4b71Sopenharmony_ci
309e41f4b71Sopenharmony_ci```ts
310e41f4b71Sopenharmony_cilet treeSet : TreeSet<string> = new TreeSet();
311e41f4b71Sopenharmony_citreeSet.add("squirrel");
312e41f4b71Sopenharmony_citreeSet.add("sparrow");
313e41f4b71Sopenharmony_cilet result = treeSet.remove("sparrow");
314e41f4b71Sopenharmony_ci```
315e41f4b71Sopenharmony_ci
316e41f4b71Sopenharmony_ci
317e41f4b71Sopenharmony_ci### getLowerValue
318e41f4b71Sopenharmony_ci
319e41f4b71Sopenharmony_cigetLowerValue(key: T): T
320e41f4b71Sopenharmony_ci
321e41f4b71Sopenharmony_ci获取容器中比传入元素排序靠前一位的元素,为空时返回undefined。
322e41f4b71Sopenharmony_ci
323e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
324e41f4b71Sopenharmony_ci
325e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang
326e41f4b71Sopenharmony_ci
327e41f4b71Sopenharmony_ci**参数:**
328e41f4b71Sopenharmony_ci
329e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 |
330e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
331e41f4b71Sopenharmony_ci| key | T | 是 | 对比的元素值。 |
332e41f4b71Sopenharmony_ci
333e41f4b71Sopenharmony_ci**返回值:**
334e41f4b71Sopenharmony_ci
335e41f4b71Sopenharmony_ci| 类型 | 说明 |
336e41f4b71Sopenharmony_ci| -------- | -------- |
337e41f4b71Sopenharmony_ci| T | 返回排序中对比元素前一位的数据,为空时返回undefined。 |
338e41f4b71Sopenharmony_ci
339e41f4b71Sopenharmony_ci**错误码:**
340e41f4b71Sopenharmony_ci
341e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
342e41f4b71Sopenharmony_ci
343e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
344e41f4b71Sopenharmony_ci| -------- | -------- |
345e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
346e41f4b71Sopenharmony_ci| 10200011 | The getLowerValue method cannot be bound. |
347e41f4b71Sopenharmony_ci
348e41f4b71Sopenharmony_ci**示例:**
349e41f4b71Sopenharmony_ci
350e41f4b71Sopenharmony_ci```ts
351e41f4b71Sopenharmony_cilet treeSet : TreeSet<string> = new TreeSet();
352e41f4b71Sopenharmony_citreeSet.add("squirrel");
353e41f4b71Sopenharmony_citreeSet.add("sparrow");
354e41f4b71Sopenharmony_citreeSet.add("gander");
355e41f4b71Sopenharmony_cilet result = treeSet.getLowerValue("sparrow");
356e41f4b71Sopenharmony_ci```
357e41f4b71Sopenharmony_ci
358e41f4b71Sopenharmony_ci
359e41f4b71Sopenharmony_ci### getHigherValue
360e41f4b71Sopenharmony_ci
361e41f4b71Sopenharmony_cigetHigherValue(key: T): T
362e41f4b71Sopenharmony_ci
363e41f4b71Sopenharmony_ci获取容器中比传入元素排序靠后一位的元素,为空时返回undefined。
364e41f4b71Sopenharmony_ci
365e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
366e41f4b71Sopenharmony_ci
367e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang
368e41f4b71Sopenharmony_ci
369e41f4b71Sopenharmony_ci**参数:**
370e41f4b71Sopenharmony_ci
371e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 |
372e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
373e41f4b71Sopenharmony_ci| key | T | 是 | 对比的元素。 |
374e41f4b71Sopenharmony_ci
375e41f4b71Sopenharmony_ci**返回值:**
376e41f4b71Sopenharmony_ci
377e41f4b71Sopenharmony_ci| 类型 | 说明 |
378e41f4b71Sopenharmony_ci| -------- | -------- |
379e41f4b71Sopenharmony_ci| T | 返回排序中传入元素后一位的数据。为空时返回undefined。 |
380e41f4b71Sopenharmony_ci
381e41f4b71Sopenharmony_ci**错误码:**
382e41f4b71Sopenharmony_ci
383e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
384e41f4b71Sopenharmony_ci
385e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
386e41f4b71Sopenharmony_ci| -------- | -------- |
387e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
388e41f4b71Sopenharmony_ci| 10200011 | The getHigherValue method cannot be bound. |
389e41f4b71Sopenharmony_ci
390e41f4b71Sopenharmony_ci**示例:**
391e41f4b71Sopenharmony_ci
392e41f4b71Sopenharmony_ci```ts
393e41f4b71Sopenharmony_cilet treeSet : TreeSet<string> = new TreeSet();
394e41f4b71Sopenharmony_citreeSet.add("squirrel");
395e41f4b71Sopenharmony_citreeSet.add("sparrow");
396e41f4b71Sopenharmony_citreeSet.add("gander");
397e41f4b71Sopenharmony_cilet result = treeSet.getHigherValue("sparrow");
398e41f4b71Sopenharmony_ci```
399e41f4b71Sopenharmony_ci
400e41f4b71Sopenharmony_ci
401e41f4b71Sopenharmony_ci### popFirst
402e41f4b71Sopenharmony_ci
403e41f4b71Sopenharmony_cipopFirst(): T
404e41f4b71Sopenharmony_ci
405e41f4b71Sopenharmony_ci删除容器中排序最前的数据,为空时返回undefined。
406e41f4b71Sopenharmony_ci
407e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
408e41f4b71Sopenharmony_ci
409e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang
410e41f4b71Sopenharmony_ci
411e41f4b71Sopenharmony_ci**返回值:**
412e41f4b71Sopenharmony_ci
413e41f4b71Sopenharmony_ci| 类型 | 说明 |
414e41f4b71Sopenharmony_ci| -------- | -------- |
415e41f4b71Sopenharmony_ci| T | 返回删除的数据,为空时返回undefined。 |
416e41f4b71Sopenharmony_ci
417e41f4b71Sopenharmony_ci**错误码:**
418e41f4b71Sopenharmony_ci
419e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
420e41f4b71Sopenharmony_ci
421e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
422e41f4b71Sopenharmony_ci| -------- | -------- |
423e41f4b71Sopenharmony_ci| 10200011 | The popFirst method cannot be bound. |
424e41f4b71Sopenharmony_ci
425e41f4b71Sopenharmony_ci**示例:**
426e41f4b71Sopenharmony_ci
427e41f4b71Sopenharmony_ci```ts
428e41f4b71Sopenharmony_cilet treeSet : TreeSet<string> = new TreeSet();
429e41f4b71Sopenharmony_citreeSet.add("squirrel");
430e41f4b71Sopenharmony_citreeSet.add("sparrow");
431e41f4b71Sopenharmony_cilet result = treeSet.popFirst();
432e41f4b71Sopenharmony_ci```
433e41f4b71Sopenharmony_ci
434e41f4b71Sopenharmony_ci
435e41f4b71Sopenharmony_ci### popLast
436e41f4b71Sopenharmony_ci
437e41f4b71Sopenharmony_cipopLast(): T
438e41f4b71Sopenharmony_ci
439e41f4b71Sopenharmony_ci删除容器中排序最后的数据,为空时返回undefined。
440e41f4b71Sopenharmony_ci
441e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
442e41f4b71Sopenharmony_ci
443e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang
444e41f4b71Sopenharmony_ci
445e41f4b71Sopenharmony_ci**返回值:**
446e41f4b71Sopenharmony_ci
447e41f4b71Sopenharmony_ci| 类型 | 说明 |
448e41f4b71Sopenharmony_ci| -------- | -------- |
449e41f4b71Sopenharmony_ci| T | 返回删除的数据,为空时返回undefined。 |
450e41f4b71Sopenharmony_ci
451e41f4b71Sopenharmony_ci**错误码:**
452e41f4b71Sopenharmony_ci
453e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
454e41f4b71Sopenharmony_ci
455e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
456e41f4b71Sopenharmony_ci| -------- | -------- |
457e41f4b71Sopenharmony_ci| 10200011 | The popLast method cannot be bound. |
458e41f4b71Sopenharmony_ci
459e41f4b71Sopenharmony_ci**示例:**
460e41f4b71Sopenharmony_ci
461e41f4b71Sopenharmony_ci```ts
462e41f4b71Sopenharmony_cilet treeSet : TreeSet<string> = new TreeSet();
463e41f4b71Sopenharmony_citreeSet.add("squirrel");
464e41f4b71Sopenharmony_citreeSet.add("sparrow");
465e41f4b71Sopenharmony_cilet result = treeSet.popLast();
466e41f4b71Sopenharmony_ci```
467e41f4b71Sopenharmony_ci
468e41f4b71Sopenharmony_ci
469e41f4b71Sopenharmony_ci### clear
470e41f4b71Sopenharmony_ci
471e41f4b71Sopenharmony_ciclear(): void
472e41f4b71Sopenharmony_ci
473e41f4b71Sopenharmony_ci清除容器中的所有元素,并把length置为0。
474e41f4b71Sopenharmony_ci
475e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
476e41f4b71Sopenharmony_ci
477e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang
478e41f4b71Sopenharmony_ci
479e41f4b71Sopenharmony_ci**错误码:**
480e41f4b71Sopenharmony_ci
481e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
482e41f4b71Sopenharmony_ci
483e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
484e41f4b71Sopenharmony_ci| -------- | -------- |
485e41f4b71Sopenharmony_ci| 10200011 | The clear method cannot be bound. |
486e41f4b71Sopenharmony_ci
487e41f4b71Sopenharmony_ci**示例:**
488e41f4b71Sopenharmony_ci
489e41f4b71Sopenharmony_ci```ts
490e41f4b71Sopenharmony_cilet treeSet : TreeSet<string> = new TreeSet();
491e41f4b71Sopenharmony_citreeSet.add("squirrel");
492e41f4b71Sopenharmony_citreeSet.add("sparrow");
493e41f4b71Sopenharmony_citreeSet.clear();
494e41f4b71Sopenharmony_ci```
495e41f4b71Sopenharmony_ci
496e41f4b71Sopenharmony_ci
497e41f4b71Sopenharmony_ci### values
498e41f4b71Sopenharmony_ci
499e41f4b71Sopenharmony_civalues(): IterableIterator&lt;T&gt;
500e41f4b71Sopenharmony_ci
501e41f4b71Sopenharmony_ci返回包含此映射中键值的新迭代器对象。
502e41f4b71Sopenharmony_ci
503e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
504e41f4b71Sopenharmony_ci
505e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang
506e41f4b71Sopenharmony_ci
507e41f4b71Sopenharmony_ci**返回值:**
508e41f4b71Sopenharmony_ci
509e41f4b71Sopenharmony_ci| 类型 | 说明 |
510e41f4b71Sopenharmony_ci| -------- | -------- |
511e41f4b71Sopenharmony_ci| IterableIterator&lt;T&gt; | 返回一个迭代器。 |
512e41f4b71Sopenharmony_ci
513e41f4b71Sopenharmony_ci**错误码:**
514e41f4b71Sopenharmony_ci
515e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
516e41f4b71Sopenharmony_ci
517e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
518e41f4b71Sopenharmony_ci| -------- | -------- |
519e41f4b71Sopenharmony_ci| 10200011 | The values method cannot be bound. |
520e41f4b71Sopenharmony_ci
521e41f4b71Sopenharmony_ci**示例:**
522e41f4b71Sopenharmony_ci
523e41f4b71Sopenharmony_ci```ts
524e41f4b71Sopenharmony_cilet treeSet : TreeSet<string> = new TreeSet();
525e41f4b71Sopenharmony_citreeSet.add("squirrel");
526e41f4b71Sopenharmony_citreeSet.add("sparrow");
527e41f4b71Sopenharmony_cilet it = treeSet.values();
528e41f4b71Sopenharmony_cilet t: IteratorResult<string> = it.next();
529e41f4b71Sopenharmony_ciwhile(!t.done) {
530e41f4b71Sopenharmony_ci  console.log("TreeSet: " + t.value);
531e41f4b71Sopenharmony_ci  t = it.next()
532e41f4b71Sopenharmony_ci}
533e41f4b71Sopenharmony_ci```
534e41f4b71Sopenharmony_ci
535e41f4b71Sopenharmony_ci
536e41f4b71Sopenharmony_ci### forEach
537e41f4b71Sopenharmony_ci
538e41f4b71Sopenharmony_ciforEach(callbackFn: (value?: T, key?: T, set?: TreeSet&lt;T&gt;) => void, thisArg?: Object): void
539e41f4b71Sopenharmony_ci
540e41f4b71Sopenharmony_ci通过回调函数来遍历实例对象上的元素以及元素对应的下标。
541e41f4b71Sopenharmony_ci
542e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
543e41f4b71Sopenharmony_ci
544e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang
545e41f4b71Sopenharmony_ci
546e41f4b71Sopenharmony_ci**参数:**
547e41f4b71Sopenharmony_ci
548e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 |
549e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
550e41f4b71Sopenharmony_ci| callbackFn | function | 是 | 回调函数。 |
551e41f4b71Sopenharmony_ci| thisArg | Object | 否 | callbackFn被调用时用作this值,默认值为当前实例对象。 |
552e41f4b71Sopenharmony_ci
553e41f4b71Sopenharmony_cicallbackFn的参数说明:
554e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 |
555e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
556e41f4b71Sopenharmony_ci| value | T | 否 | 当前遍历到的value元素,默认值为首个键值对的值。 |
557e41f4b71Sopenharmony_ci| key | T | 否 | 当前遍历到的key元素,默认值为首个键值对的键。 |
558e41f4b71Sopenharmony_ci| set | TreeSet&lt;T&gt; | 否 | 当前调用forEach方法的实例对象,默认值为当前实例对象。 |
559e41f4b71Sopenharmony_ci
560e41f4b71Sopenharmony_ci**错误码:**
561e41f4b71Sopenharmony_ci
562e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
563e41f4b71Sopenharmony_ci
564e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
565e41f4b71Sopenharmony_ci| -------- | -------- |
566e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
567e41f4b71Sopenharmony_ci| 10200011 | The forEach method cannot be bound. |
568e41f4b71Sopenharmony_ci
569e41f4b71Sopenharmony_ci**示例:**
570e41f4b71Sopenharmony_ci
571e41f4b71Sopenharmony_ci```ts
572e41f4b71Sopenharmony_cilet treeSet : TreeSet<string> = new TreeSet();
573e41f4b71Sopenharmony_citreeSet.add("sparrow");
574e41f4b71Sopenharmony_citreeSet.add("gull");
575e41f4b71Sopenharmony_citreeSet.forEach((value ?: string, key ?: string) :void => {
576e41f4b71Sopenharmony_ci  console.log("value:" + value, "key:" + key);
577e41f4b71Sopenharmony_ci});
578e41f4b71Sopenharmony_ci```
579e41f4b71Sopenharmony_ci```ts
580e41f4b71Sopenharmony_ci// 不建议在forEach中使用set、remove方法,会导致死循环等不可预知的风险,可使用for循环来进行插入和删除。
581e41f4b71Sopenharmony_cilet treeSet : TreeSet<string> = new TreeSet();
582e41f4b71Sopenharmony_cifor(let i = 0; i < 10; i++) {
583e41f4b71Sopenharmony_ci  treeSet.add("sparrow" + i);
584e41f4b71Sopenharmony_ci}
585e41f4b71Sopenharmony_cifor(let i = 0; i < 10; i++) {
586e41f4b71Sopenharmony_ci  treeSet.remove("sparrow" + i);
587e41f4b71Sopenharmony_ci}
588e41f4b71Sopenharmony_ci```
589e41f4b71Sopenharmony_ci
590e41f4b71Sopenharmony_ci### entries
591e41f4b71Sopenharmony_ci
592e41f4b71Sopenharmony_cientries(): IterableIterator<[T, T]>
593e41f4b71Sopenharmony_ci
594e41f4b71Sopenharmony_ci返回包含此映射中键值对的新迭代器对象。
595e41f4b71Sopenharmony_ci
596e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
597e41f4b71Sopenharmony_ci
598e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang
599e41f4b71Sopenharmony_ci
600e41f4b71Sopenharmony_ci**返回值:**
601e41f4b71Sopenharmony_ci
602e41f4b71Sopenharmony_ci| 类型 | 说明 |
603e41f4b71Sopenharmony_ci| -------- | -------- |
604e41f4b71Sopenharmony_ci| IterableIterator<[T, T]> | 返回一个迭代器。 |
605e41f4b71Sopenharmony_ci
606e41f4b71Sopenharmony_ci**错误码:**
607e41f4b71Sopenharmony_ci
608e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
609e41f4b71Sopenharmony_ci
610e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
611e41f4b71Sopenharmony_ci| -------- | -------- |
612e41f4b71Sopenharmony_ci| 10200011 | The entries method cannot be bound. |
613e41f4b71Sopenharmony_ci
614e41f4b71Sopenharmony_ci**示例:**
615e41f4b71Sopenharmony_ci
616e41f4b71Sopenharmony_ci```ts
617e41f4b71Sopenharmony_cilet treeSet : TreeSet<string> = new TreeSet();
618e41f4b71Sopenharmony_citreeSet.add("squirrel");
619e41f4b71Sopenharmony_citreeSet.add("sparrow");
620e41f4b71Sopenharmony_cilet it = treeSet.entries();
621e41f4b71Sopenharmony_cilet t: IteratorResult<Object[]> = it.next();
622e41f4b71Sopenharmony_ciwhile(!t.done) {
623e41f4b71Sopenharmony_ci  console.log("TreeSet: " + t.value);
624e41f4b71Sopenharmony_ci  t = it.next()
625e41f4b71Sopenharmony_ci}
626e41f4b71Sopenharmony_ci```
627e41f4b71Sopenharmony_ci```ts
628e41f4b71Sopenharmony_ci// 不建议在entries中使用set、remove方法,会导致死循环等不可预知的风险,可使用for循环来进行插入和删除。
629e41f4b71Sopenharmony_cilet treeSet : TreeSet<string> = new TreeSet();
630e41f4b71Sopenharmony_cifor(let i = 0; i < 10; i++) {
631e41f4b71Sopenharmony_ci  treeSet.add("sparrow" + i);
632e41f4b71Sopenharmony_ci}
633e41f4b71Sopenharmony_cifor(let i = 0; i < 10; i++) {
634e41f4b71Sopenharmony_ci  treeSet.remove("sparrow" + i);
635e41f4b71Sopenharmony_ci}
636e41f4b71Sopenharmony_ci```
637e41f4b71Sopenharmony_ci
638e41f4b71Sopenharmony_ci### [Symbol.iterator]
639e41f4b71Sopenharmony_ci
640e41f4b71Sopenharmony_ci[Symbol.iterator]\(): IterableIterator&lt;T&gt;
641e41f4b71Sopenharmony_ci
642e41f4b71Sopenharmony_ci返回一个迭代器,迭代器的每一项都是一个JavaScript对象,并返回该对象。
643e41f4b71Sopenharmony_ci
644e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
645e41f4b71Sopenharmony_ci
646e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang
647e41f4b71Sopenharmony_ci
648e41f4b71Sopenharmony_ci**返回值:**
649e41f4b71Sopenharmony_ci
650e41f4b71Sopenharmony_ci| 类型 | 说明 |
651e41f4b71Sopenharmony_ci| -------- | -------- |
652e41f4b71Sopenharmony_ci| IterableIterator&lt;T&gt; | 返回一个迭代器。 |
653e41f4b71Sopenharmony_ci
654e41f4b71Sopenharmony_ci**错误码:**
655e41f4b71Sopenharmony_ci
656e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
657e41f4b71Sopenharmony_ci
658e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
659e41f4b71Sopenharmony_ci| -------- | -------- |
660e41f4b71Sopenharmony_ci| 10200011 | The Symbol.iterator method cannot be bound. |
661e41f4b71Sopenharmony_ci
662e41f4b71Sopenharmony_ci**示例:**
663e41f4b71Sopenharmony_ci
664e41f4b71Sopenharmony_ci```ts
665e41f4b71Sopenharmony_cilet treeSet : TreeSet<string> = new TreeSet();
666e41f4b71Sopenharmony_citreeSet.add("squirrel");
667e41f4b71Sopenharmony_citreeSet.add("sparrow");
668e41f4b71Sopenharmony_cilet numbers = Array.from(treeSet.values())
669e41f4b71Sopenharmony_ci// 使用方法一:
670e41f4b71Sopenharmony_cifor (let item of numbers) {
671e41f4b71Sopenharmony_ci  console.log("value:" + item);
672e41f4b71Sopenharmony_ci}
673e41f4b71Sopenharmony_ci// 使用方法二:
674e41f4b71Sopenharmony_cilet iter = treeSet[Symbol.iterator]();
675e41f4b71Sopenharmony_cilet temp: IteratorResult<string> = iter.next().value;
676e41f4b71Sopenharmony_ciwhile(temp != undefined) {
677e41f4b71Sopenharmony_ci  console.log("value:" + temp);
678e41f4b71Sopenharmony_ci  temp = iter.next().value;
679e41f4b71Sopenharmony_ci}
680e41f4b71Sopenharmony_ci```
681e41f4b71Sopenharmony_ci```ts
682e41f4b71Sopenharmony_ci// 不建议在Symbol.iterator中使用set、remove方法,会导致死循环等不可预知的风险,可使用for循环来进行插入和删除。
683e41f4b71Sopenharmony_cilet treeSet : TreeSet<string> = new TreeSet();
684e41f4b71Sopenharmony_cifor(let i = 0; i < 10; i++) {
685e41f4b71Sopenharmony_ci  treeSet.add("sparrow" + i);
686e41f4b71Sopenharmony_ci}
687e41f4b71Sopenharmony_cifor(let i = 0; i < 10; i++) {
688e41f4b71Sopenharmony_ci  treeSet.remove("sparrow" + i);
689e41f4b71Sopenharmony_ci}
690e41f4b71Sopenharmony_ci```