1# @arkts.utils (ArkTS Utils) 2 3The Utils module provides a variety of ArkTS utility functions. 4 5> **NOTE** 6> 7> The initial APIs of this module are supported since API version 12. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8> 9> This module can be imported only to ArkTS files (with the file name extension .ets). 10 11## Modules to Import 12 13```ts 14import { ArkTSUtils } from '@kit.ArkTS' 15``` 16 17## ArkTSUtils.locks 18 19To avoid data races between concurrent instances, the ArkTS common library introduces **AsyncLock**. Passing **AsyncLock** objects by reference across concurrent instances is supported. 20 21ArkTS supports asynchronous operations, and blocking locks are prone to deadlocks. Therefore, only asynchronous locks (non-blocking locks) are used in ArkTS. 22 23The method that uses an asynchronous lock must be marked as **async**, and the caller must use **await** in the call to ensure the correct call sequence. As a result, all outer functions are marked as **async**. 24 25### AsyncLockCallback 26 27type AsyncLockCallback\<T> = () => T | Promise\<T> 28 29A supplementary type alias that represents the callback in all the overloads of [lockAsync](#lockasync). 30 31**Atomic service API**: This API can be used in atomic services since API version 12. 32 33**System capability**: SystemCapability.Utils.Lang 34 35### AsyncLock 36 37A class that implements an asynchronous lock and allows asynchronous operations to be performed under a lock. This class is decorated by [@Sendable](../../arkts-utils/arkts-sendable.md). 38 39#### Attributes 40 41**Atomic service API**: This API can be used in atomic services since API version 12. 42 43**System capability**: SystemCapability.Utils.Lang 44 45| Name| Type | Readable| Writable| Description | 46| ---- | ------ | ---- | ---- | ---------- | 47| name | string | Yes | No | Name of the lock.| 48 49**Example** 50 51```ts 52// Example 1 53@Sendable 54class A { 55 count_: number = 0; 56 async getCount(): Promise<number> { 57 let lock: ArkTSUtils.locks.AsyncLock = ArkTSUtils.locks.AsyncLock.request("lock_1"); 58 return lock.lockAsync(() => { 59 return this.count_; 60 }) 61 } 62 async setCount(count: number) { 63 let lock: ArkTSUtils.locks.AsyncLock = ArkTSUtils.locks.AsyncLock.request("lock_1"); 64 await lock.lockAsync(() => { 65 this.count_ = count; 66 }) 67 } 68} 69 70// Example 2 71@Sendable 72class A { 73 count_: number = 0; 74 lock_: ArkTSUtils.locks.AsyncLock = new ArkTSUtils.locks.AsyncLock(); 75 async getCount(): Promise<number> { 76 return this.lock_.lockAsync(() => { 77 return this.count_; 78 }) 79 } 80 async setCount(count: number) { 81 await this.lock_.lockAsync(() => { 82 this.count_ = count; 83 }) 84 } 85} 86 87@Concurrent 88async function foo(a: A) { 89 await a.setCount(10) 90} 91``` 92 93#### constructor 94 95constructor() 96 97Default constructor used to create an asynchronous lock. 98 99**Atomic service API**: This API can be used in atomic services since API version 12. 100 101**System capability**: SystemCapability.Utils.Lang 102 103**Return value** 104 105| Type | Description | 106| ----------------------- | ------------------ | 107| [AsyncLock](#asynclock) | **AsyncLock** instance created.| 108 109**Example** 110 111```ts 112let lock = new ArkTSUtils.locks.AsyncLock(); 113``` 114 115#### request 116 117static request(name: string): AsyncLock 118 119Finds or creates (if not found) an **AsyncLock** instance with the specified name. 120 121**Atomic service API**: This API can be used in atomic services since API version 12. 122 123**System capability**: SystemCapability.Utils.Lang 124 125**Parameters** 126 127| Name| Type | Mandatory| Description | 128| ---- | ------ | ---- | -------------------------------- | 129| name | string | Yes | Name of the lock.| 130 131**Return value** 132 133| Type | Description | 134| ----------------------- | -------------------------------- | 135| [AsyncLock](#asynclock) | **AsyncLock** instance found or created.| 136 137**Example** 138 139```ts 140let lockName = 'isAvailableLock'; 141let lock = ArkTSUtils.locks.AsyncLock.request(lockName); 142``` 143 144#### query 145 146static query(name: string): AsyncLockState 147 148Queries information about an asynchronous lock. 149 150**Atomic service API**: This API can be used in atomic services since API version 12. 151 152**System capability**: SystemCapability.Utils.Lang 153 154**Parameters** 155 156| Name| Type | Mandatory| Description | 157| ---- | ------ | ---- | ---------- | 158| name | string | Yes | Name of the lock.| 159 160**Return value** 161 162| Type | Description | 163| --------------------------------- | ---------------------------------- | 164| [AsyncLockState](#asynclockstate) | **AsyncLockState** instance that contains the lock state information.| 165 166**Error codes** 167 168For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 169 170| ID| Error Message | 171| -------- | ------------- | 172| 401 | The input parameters are invalid. | 173| 10200030 | The lock does not exist. | 174 175**Example** 176 177```ts 178// You have created a lock somewhere else. 179// let lock = ArkTSUtils.locks.AsyncLock.request("queryTestLock"); 180let state = ArkTSUtils.locks.AsyncLock.query('queryTestLock'); 181if (!state) { 182 throw new Error('Test failed: A valid state is expected, but the obtained state is '+ state); 183} 184let pending: ArkTSUtils.locks.AsyncLockInfo[] = state.pending; 185let held: ArkTSUtils.locks.AsyncLockInfo[] = state.held; 186``` 187 188#### queryAll 189 190static queryAll(): AsyncLockState[] 191 192Queries information about all existing asynchronous locks. 193 194**Atomic service API**: This API can be used in atomic services since API version 12. 195 196**System capability**: SystemCapability.Utils.Lang 197 198**Return value** 199 200| Type | Description | 201| ----------------------------------- | -------------------------------- | 202| [AsyncLockState](#asynclockstate)[] | **AsyncLockState** array that contains the lock state information.| 203 204**Example** 205 206```ts 207let states: ArkTSUtils.locks.AsyncLockState[] = ArkTSUtils.locks.AsyncLock.queryAll(); 208if (states.length == 0) { 209 throw new Error('Test failed: At least one state is expected, but what got is ' + states.length); 210} 211``` 212 213#### lockAsync 214 215lockAsync\<T>(callback: AsyncLockCallback\<T>): Promise\<T> 216 217Performs an operation exclusively under a lock. This API acquires the lock, executes the callback, and releases the lock. The callback is executed asynchronously in the same thread where [lockAsync](#lockasync) was called. 218 219**Atomic service API**: This API can be used in atomic services since API version 12. 220 221**System capability**: SystemCapability.Utils.Lang 222 223**Parameters** 224 225| Name | Type | Mandatory| Description | 226| -------- | --------------------------------------- | ---- | ---------------------- | 227| callback | [AsyncLockCallback](#asynclockcallback) | Yes | Callback to be executed after a lock is acquired.| 228 229**Return value** 230 231| Type | Description | 232| ----------- | --------------------------- | 233| Promise\<T> | Promise that will be resolved after the callback is executed.| 234 235**Error codes** 236 237For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 238 239| ID| Error Message | 240| -------- | ------------- | 241| 401 | The input parameters are invalid. | 242| 10200030 | The lock does not exist. | 243 244**Example** 245 246```ts 247let lock = new ArkTSUtils.locks.AsyncLock(); 248let p1 = lock.lockAsync<void>(() => { 249 // Perform an operation. 250}); 251``` 252 253#### lockAsync 254 255lockAsync\<T>(callback: AsyncLockCallback\<T>, mode: AsyncLockMode): Promise\<T> 256 257Performs an operation under a lock. This API acquires the lock, executes the callback, and releases the lock. The callback is executed asynchronously in the same thread where [lockAsync](#lockasync) was called. 258 259**Atomic service API**: This API can be used in atomic services since API version 12. 260 261**System capability**: SystemCapability.Utils.Lang 262 263**Parameters** 264 265| Name | Type | Mandatory| Description | 266| -------- | --------------------------------------- | ---- | ---------------------- | 267| callback | [AsyncLockCallback](#asynclockcallback) | Yes | Callback to be executed after a lock is acquired.| 268| mode | [AsyncLockMode](#asynclockmode) | Yes | Mode of the lock. | 269 270**Return value** 271 272| Type | Description | 273| ----------- | --------------------------- | 274| Promise\<T> | Promise that will be resolved after the callback is executed.| 275 276**Error codes** 277 278For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 279 280| ID| Error Message | 281| -------- | ------------- | 282| 401 | The input parameters are invalid. | 283| 10200030 | The lock does not exist. | 284 285**Example** 286 287```ts 288let lock = new ArkTSUtils.locks.AsyncLock(); 289let p1 = lock.lockAsync<void>(() => { 290 // Perform an operation. 291}, ArkTSUtils.locks.AsyncLockMode.EXCLUSIVE); 292``` 293 294#### lockAsync 295 296lockAsync\<T, U>(callback: AsyncLockCallback\<T>, mode: AsyncLockMode, options: AsyncLockOptions\<U>): Promise\<T | U> 297 298Performs an operation under a lock. This API acquires the lock, executes the callback, and releases the lock. The callback is executed asynchronously in the same thread where [lockAsync](#lockasync) was called. An optional timeout value can be provided in [AsyncLockOptions](#asynclockoptions). If a lock is not acquired before timeout, **lockAsync** returns a projected Promise with a **BusinessError** instance. In this instance, the error message contains information about the locks being held and in the waiting state, as well as possible deadlock warnings. 299 300**Atomic service API**: This API can be used in atomic services since API version 12. 301 302**System capability**: SystemCapability.Utils.Lang 303 304**Parameters** 305 306| Name | Type | Mandatory| Description | 307| -------- | ----------------------------------------- | ---- | ---------------------- | 308| callback | [AsyncLockCallback](#asynclockcallback) | Yes | Callback to be executed after a lock is acquired.| 309| mode | [AsyncLockMode](#asynclockmode) | Yes | Mode of the lock. | 310| options | [AsyncLockOptions\<U>](#asynclockoptions) | Yes | Options of the lock. | 311 312**Return value** 313 314| Type | Description | 315| ---------------- | -------------------------------------------------- | 316| Promise\<T \| U> | Promise that will be resolved after the callback is executed, or rejected in the case of timeout.| 317 318**Error codes** 319 320For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 321 322| ID| Error Message | 323| -------- | ----------------- | 324| 401 | The input parameters are invalid. | 325| 10200030 | The lock does not exist. | 326| 10200031 | Timeout exceeded. | 327 328**Example** 329 330```ts 331let lock = new ArkTSUtils.locks.AsyncLock(); 332let options = new ArkTSUtils.locks.AsyncLockOptions<void>(); 333options.timeout = 1000; 334let p: Promise<void> = lock.lockAsync<void, void>( 335 () => { 336 // Perform an operation. 337 }, 338 ArkTSUtils.locks.AsyncLockMode.EXCLUSIVE, 339 options 340); 341``` 342 343### AsyncLockMode 344 345Enumerates the modes of an asynchronous lock. 346 347**Atomic service API**: This API can be used in atomic services since API version 12. 348 349**System capability**: SystemCapability.Utils.Lang 350 351| Name | Value | Description | 352| --------- | --- | -------------------------------------------------------- | 353| SHARED | 1 | Shared lock mode, in which multiple threads can run at the same time. | 354| EXCLUSIVE | 2 | Exclusive lock mode, in which a thread can run only when it exclusively acquires the lock.| 355 356**Example** 357 358```ts 359let lock = new ArkTSUtils.locks.AsyncLock(); 360// shared0 can acquire the lock and start execution. 361lock.lockAsync(async () => { 362 console.info('shared0'); 363 await new Promise<void>((resolve) => setTimeout(resolve, 1000)); 364}, ArkTSUtils.locks.AsyncLockMode.SHARED); 365// shared1 can acquire the lock and start execution without waiting for shared0. 366lock.lockAsync(async () => { 367 console.info('shared1'); 368 await new Promise<void>((resolve) => setTimeout(resolve, 1000)); 369}, ArkTSUtils.locks.AsyncLockMode.SHARED); 370// exclusive0 can acquire the lock and start execution after shared0 and shared1 are executed. 371lock.lockAsync(async () => { 372 console.info('exclusive0'); 373 await new Promise<void>((resolve) => setTimeout(resolve, 1000)); 374}, ArkTSUtils.locks.AsyncLockMode.EXCLUSIVE); 375// shared2 can acquire the lock and start execution after exclusive0 is executed. 376lock.lockAsync(async () => { 377 console.info('shared2'); 378 await new Promise<void>((resolve) => setTimeout(resolve, 1000)); 379}, ArkTSUtils.locks.AsyncLockMode.SHARED); 380// shared3 can acquire the lock and start execution after exclusive0 is executed, but not after shared2 is executed. 381lock.lockAsync(async () => { 382 console.info('shared3'); 383 await new Promise<void>((resolve) => setTimeout(resolve, 1000)); 384}, ArkTSUtils.locks.AsyncLockMode.SHARED); 385``` 386 387### AsyncLockOptions 388 389class AsyncLockOptions\<T> 390 391Class that implements the asynchronous lock options. 392 393**Atomic service API**: This API can be used in atomic services since API version 12. 394 395**System capability**: SystemCapability.Utils.Lang 396 397#### constructor 398 399constructor() 400 401Default constructor used to create an **AsyncLockOptions** instance with the default values for all attributes. 402 403**Atomic service API**: This API can be used in atomic services since API version 12. 404 405**System capability**: SystemCapability.Utils.Lang 406 407**Return value** 408 409| Type | Description | 410| ------------------------------------- | ---------------------- | 411| [AsyncLockOptions](#asynclockoptions) | **AsyncLockOptions** instance created.| 412 413**Example** 414 415```ts 416let s: ArkTSUtils.locks.AbortSignal<string> = { aborted: false, reason: 'Aborted' }; 417let options = new ArkTSUtils.locks.AsyncLockOptions<string>(); 418options.isAvailable = false; 419options.signal = s; 420``` 421 422#### Attributes 423 424| Name | Type | Readable| Writable| Description | 425| ----------- | ------------------------------------- | ---- | ---- | ------------------------------------------------------------------------------------------------------------------------- | 426| isAvailable | boolean | Yes | Yes | Whether the lock is available. If the value is **true**, a lock is granted only when it is not held. If the value is **false**, a lock is granted once it is released. The default value is **false**.| 427| signal | [AbortSignal\<T>](#abortsignal)\|null | Yes | Yes | Signal used to abort an asynchronous operation. If **signal.aborted** is **true**, the lock request is discarded. If **signal.aborted** is **false**, the request keeps waiting. If **signal.aborted** is **null**, the request is queued normally. The default value is **null**. | 428| timeout | number | Yes | Yes | Timeout interval of the lock request, in milliseconds. If the value is greater than zero and a lock is not acquired before time, [lockAsync](#lockasync) returns a rejected Promise. The default value is **0**. | 429 430### AsyncLockState 431 432A class used to store information about all lock operations currently performed on an **AsyncLock** instance. 433 434**Atomic service API**: This API can be used in atomic services since API version 12. 435 436**System capability**: SystemCapability.Utils.Lang 437 438#### Attributes 439 440| Name | Type | Readable| Writable| Description | 441| ------- | --------------------------------- | ---- | ---- | ---------------- | 442| held | [AsyncLockInfo[]](#asynclockinfo) | Yes | Yes | Information about the lock being held. | 443| pending | [AsyncLockInfo[]](#asynclockinfo) | Yes | Yes | Information about the lock in the waiting state.| 444 445### AsyncLockInfo 446 447Describes the information about a lock. 448 449**Atomic service API**: This API can be used in atomic services since API version 12. 450 451**System capability**: SystemCapability.Utils.Lang 452 453#### Attributes 454 455| Name | Type | Readable| Writable| Description | 456| --------- | ------------------------------- | ---- | ---- | --------------------------------------------------------- | 457| name | string | Yes | Yes | Name of the lock. | 458| mode | [AsyncLockMode](#asynclockmode) | Yes | Yes | Mode of the lock. | 459| contextId | number | Yes | Yes | Context identifier of the caller of [AsyncLockMode](#asynclockmode).| 460 461### AbortSignal 462 463A class that implements a signal used to abort an asynchronous operation. An instance of this class must be accessed in the same thread it creates. Otherwise, undefined behavior occurs. 464 465**Atomic service API**: This API can be used in atomic services since API version 12. 466 467**System capability**: SystemCapability.Utils.Lang 468 469#### Attributes 470 471| Name | Type | Readable| Writable| Description | 472| ------- | ------- | ---- | ---- | ---------------------------------------------------------------- | 473| aborted | boolean | Yes | Yes | Whether to abort the asynchronous operation. The value **true** means to abort the asynchronous operation, and **false** means the opposite. | 474| reason | \<T> | Yes | Yes | Reason for abort. This value will be used in the rejected Promise returned by [lockAsync](#lockasync).| 475 476### ConditionVariable<sup>18+</sup> 477 478A class that implements asynchronous waiting, enabling asynchronous wait and notify operations. 479 480**Atomic service API**: This API can be used in atomic services since API version 18. 481 482**System capability**: SystemCapability.Utils.Lang 483 484#### constructor<sup>18+</sup> 485 486constructor() 487 488Default constructor used to create an object for asynchronous wait and notify operations. 489 490**Atomic service API**: This API can be used in atomic services since API version 18. 491 492**System capability**: SystemCapability.Utils.Lang 493 494**Example** 495 496```ts 497let conditionVariable = new ArkTSUtils.locks.ConditionVariable(); 498``` 499 500#### request<sup>18+</sup> 501 502static request(name: string): ConditionVariable 503 504Looks up or creates (if not found) an object for asynchronous wait and notify operations based on the specified name. 505 506**Atomic service API**: This API can be used in atomic services since API version 18. 507 508**System capability**: SystemCapability.Utils.Lang 509 510**Parameters** 511 512| Name| Type | Mandatory| Description | 513| ---- | ------ | ---- | -------------------------------- | 514| name | string | Yes | Name used to identify the object for asynchronous wait and notify operations.| 515 516**Return value** 517 518| Type | Description | 519| ----------------------- | -------------------------------- | 520| [ConditionVariable](#conditionvariable18) | Object for asynchronous wait and notify operations.| 521 522**Error codes** 523 524For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 525 526| ID| Error Message | 527| -------- | ----------------- | 528| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 529 530**Example** 531 532```ts 533let conditionVariable = ArkTSUtils.locks.ConditionVariable.request("conditionName"); 534``` 535 536#### wait<sup>18+</sup> 537 538wait(): Promise\<void> 539 540Asynchronously waits until notified. This API uses a promise to return the result. 541 542**Atomic service API**: This API can be used in atomic services since API version 18. 543 544**System capability**: SystemCapability.Utils.Lang 545 546**Return value** 547 548| Type | Description | 549| ----------- | --------------------------- | 550| Promise\<void> | Promise that returns no value.| 551 552**Example** 553 554```ts 555let conditionVariable = ArkTSUtils.locks.AsyncLock.ConditionVariable(); 556conditionVariable.wait().then(() => { 557 console.info(`Thread being awakened, then continue...`); // Output logs upon awakening. 558}); 559``` 560 561#### waitFor<sup>18+</sup> 562 563waitFor(timeout : number) : Promise\<void> 564 565Asynchronously waits for a specified duration or until notified. This API uses a promise to return the result. 566 567**Atomic service API**: This API can be used in atomic services since API version 18. 568 569**System capability**: SystemCapability.Utils.Lang 570 571**Parameters** 572 573| Name| Type | Mandatory| Description | 574| -------- | -------- | ---- | ---------- | 575| timeout | number | Yes | Duration to wait, in ms. The value is a positive integer.| 576 577**Return value** 578 579| Type | Description | 580| ----------- | --------------------------- | 581| Promise\<void> | Promise that returns no value.| 582 583**Error codes** 584 585For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 586 587| ID| Error Message | 588| -------- | ----------------- | 589| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 590 591**Example** 592 593```ts 594let conditionVariable = ArkTSUtils.locks.AsyncLock.ConditionVariable(); 595conditionVariable.waitFor(3000).then(() => { 596 console.info(`Thread being awakened, then continue...`); // Output logs upon awakening. 597}); 598``` 599 600#### notifyAll<sup>18+</sup> 601 602notifyAll() : void 603 604Notifies all waiting threads. 605 606**Atomic service API**: This API can be used in atomic services since API version 18. 607 608**System capability**: SystemCapability.Utils.Lang 609 610**Example** 611 612```ts 613let conditionVariable = ArkTSUtils.locks.AsyncLock.ConditionVariable(); 614conditionVariable.waitFor(3000).then(() => { 615 console.info(`Thread being awakened, then continue...`); // Output logs upon awakening. 616}); 617conditionVariable.notifyAll(); 618``` 619 620#### notifyOne<sup>18+</sup> 621 622notifyOne() : void 623 624Notifies the first waiting thread. 625 626**Atomic service API**: This API can be used in atomic services since API version 18. 627 628**System capability**: SystemCapability.Utils.Lang 629 630**Example** 631 632```ts 633let conditionVariable = ArkTSUtils.locks.AsyncLock.ConditionVariable(); 634conditionVariable.waitFor(3000).then(() => { 635 console.info(`Thread a being awakened, then continue...`); // Output logs upon awakening. 636}); 637conditionVariable.waitFor().then(() => { 638 console.info(`Thread twob being awakened, then continue...`); // Output logs upon awakening. 639}); 640conditionVariable.notifyOne(); 641``` 642 643## ArkTSUtils.ASON 644 645A utility class used to parse JSON strings into [sendable data](../../arkts-utils/arkts-sendable.md#sendable-data-types). ASON allows you to parse JSON strings and generate data that can be passed across concurrency domains. It also supports conversion from sendable data into JSON strings. 646 647### ISendable 648 649type ISendable = lang.ISendable 650 651**ISendable** is the parent type of all sendable types except null and undefined. It does not have any necessary methods or properties. 652 653**Atomic service API**: This API can be used in atomic services since API version 12. 654 655**System capability**: SystemCapability.Utils.Lang 656 657| Type| Description | 658| ------ | ------ | 659| [lang.ISendable](js-apis-arkts-lang.md#langisendable) | Parent type of all sendable types.| 660 661### Transformer 662 663type Transformer = (this: ISendable, key: string, value: ISendable | undefined | null) => ISendable | undefined | null 664 665Defines the type of the conversion result function. 666 667**Atomic service API**: This API can be used in atomic services since API version 12. 668 669**System capability**: SystemCapability.Utils.Lang 670 671**Parameters** 672 673| Name| Type | Mandatory| Description | 674| ------ | ------ | ---- | --------------- | 675| this | [ISendable](#isendable) | Yes| Object to which the key-value pair to parse belongs.| 676| key | string | Yes| Key to parse.| 677| value | [ISendable](#isendable) | Yes| Value of the key.| 678 679**Return value** 680 681| Type| Description| 682| -------- | -------- | 683| [ISendable](#isendable) \| undefined \| null | **ISendable** object, undefined, or null.| 684 685### BigIntMode 686 687Enumerates the modes for processing BigInt. 688 689**Atomic service API**: This API can be used in atomic services since API version 12. 690 691**System capability**: SystemCapability.Utils.Lang 692 693| Name| Value| Description | 694| ------ | ------ | --------------- | 695| DEFAULT | 0 |BigInt is not supported.| 696| PARSE_AS_BIGINT | 1 |Parses an integer that is less than -(2^53-1) or greater than (2^53-1) as BigInt.| 697| ALWAYS_PARSE_AS_BIGINT | 2 |Parses all integers as BigInt.| 698 699### ParseReturnType 700 701Enumerates the return types of the parsing result. 702 703**System capability**: SystemCapability.Utils.Lang 704 705| Name| Value| Description | 706| ------ | ------ | --------------- | 707| OBJECT | 0 |Returns a **SendableObject** object.<br>**Atomic service API**: This API can be used in atomic services since API version 12.| 708| MAP<sup>13+</sup> | 1 |Returns a **SendableMap** object.<br>**Atomic service API**: This API can be used in atomic services since API version 13.| 709 710### ParseOptions 711 712Describes the parsing options, which defines the BigInt processing mode and the return type of the parsing result. 713 714**Atomic service API**: This API can be used in atomic services since API version 12. 715 716**System capability**: SystemCapability.Utils.Lang 717 718| Name| Type| Mandatory| Description | 719| ------ | ------ | ---- | --------------- | 720| bigIntMode | [BigIntMode](#bigintmode) | Yes|Mode for processing BigInt.| 721| parseReturnType | [ParseReturnType](#parsereturntype) | Yes|Return type of the parsing result.| 722 723### parse 724 725parse(text: string, reviver?: Transformer, options?: ParseOptions): ISendable | null 726 727Parses a JSON string to generate ISendable data or null. 728 729**Atomic service API**: This API can be used in atomic services since API version 12. 730 731**System capability**: SystemCapability.Utils.Lang 732 733**Parameters** 734 735| Name| Type | Mandatory| Description | 736| ------ | ------ | ---- | --------------- | 737| text | string | Yes| Valid JSON string.| 738| reviver | [Transformer](#transformer) | No| Conversion function. This parameter can be used to modify the value generated after parsing. The default value is undefined. Currently, only undefined can be passed in.| 739| options | [ParseOptions](#parseoptions) | No| Parsing options. This parameter is used to control the type of the parsing result. The default value is undefined.| 740 741**Return value** 742 743| Type| Description| 744| -------- | -------- | 745| [ISendable](#isendable) \| null | ISendable data or **null** (if **null** is passed in).| 746 747**Example** 748 749```ts 750import { lang } from '@kit.ArkTS'; 751import { collections } from '@kit.ArkTS'; 752 753type ISendable = lang.ISendable; 754let jsonText = '{"name": "John", "age": 30, "city": "ChongQing"}'; 755let obj = ArkTSUtils.ASON.parse(jsonText) as ISendable; 756console.info((obj as object)?.["name"]); 757// Expected output: 'John' 758console.info((obj as object)?.["age"]); 759// Expected output: 30 760console.info((obj as object)?.["city"]); 761// Expected output: 'ChongQing' 762 763let options: ArkTSUtils.ASON.ParseOptions = { 764 bigIntMode: ArkTSUtils.ASON.BigIntMode.PARSE_AS_BIGINT, 765 parseReturnType: ArkTSUtils.ASON.ParseReturnType.OBJECT, 766} 767let numberText = '{"largeNumber":112233445566778899}'; 768let numberObj = ArkTSUtils.ASON.parse(numberText,undefined,options) as ISendable; 769 770console.info((numberObj as object)?.["largeNumber"]); 771// Expected output: 112233445566778899 772 773let options2: ArkTSUtils.ASON.ParseOptions = { 774 bigIntMode: ArkTSUtils.ASON.BigIntMode.PARSE_AS_BIGINT, 775 parseReturnType: ArkTSUtils.ASON.ParseReturnType.MAP, 776 } 777let mapText = '{"largeNumber":112233445566778899}'; 778let map = ArkTSUtils.ASON.parse(mapText,undefined,options2); 779console.info("map is " + map); 780// Expected output: map is [object SendableMap] 781console.info("largeNumber is " + (map as collections.Map<string,bigint>).get("largeNumber")); 782// Expected output: largeNumber is 112233445566778899 783``` 784 785### stringify 786 787stringify(value: ISendable | null | undefined): string 788 789Converts ISendable data into a JSON string. 790 791**Atomic service API**: This API can be used in atomic services since API version 12. 792 793**System capability**: SystemCapability.Utils.Lang 794 795**Parameters** 796 797| Name| Type| Mandatory| Description| 798| -------- | -------- | -------- | -------- | 799| value | [ISendable](#isendable) \| null \| undefined | Yes| ISendable data.| 800 801**Return value** 802 803| Type| Description| 804| -------- | -------- | 805| string | JSON string.| 806 807**Example** 808 809```ts 810import { collections } from '@kit.ArkTS'; 811 812let arr = new collections.Array(1, 2, 3); 813let str = ArkTSUtils.ASON.stringify(arr); 814console.info(str); 815// Expected output: '[1,2,3]' 816``` 817 818### stringify<sup>18+</sup> 819 820stringify(value: Object | null | undefined): string 821 822Converts ArkTS object data into a JSON string, with additional support for Map and Set types. 823 824**Atomic service API**: This API can be used in atomic services since API version 18. 825 826**System capability**: SystemCapability.Utils.Lang 827 828**Parameters** 829 830| Name| Type| Mandatory| Description| 831| -------- | -------- | -------- | -------- | 832| value | Object \| null \| undefined | Yes| ArkTS object data.| 833 834**Return value** 835 836| Type| Description| 837| -------- | -------- | 838| string | JSON string.| 839 840**Example** 841 842```ts 843import { ArkTSUtils, collections, HashMap, HashSet } from '@kit.ArkTS'; 844 845let hashMap = new HashMap<string,string>(); 846hashMap.set("ha","a"); 847hashMap.set("sh","b"); 848hashMap.set("map","c"); 849let str1 = ArkTSUtils.ASON.stringify(hashMap); 850console.info(str1); 851// Expected output: '{"sh":"b","ha":"a","map":"c"}' 852let hashSet = new HashSet<string>(); 853hashSet.add("ha"); 854hashSet.add("sh"); 855hashSet.add("set"); 856let str2 = ArkTSUtils.ASON.stringify(hashSet); 857console.info(str2); 858// Expected output: '["set","sh","ha"]' 859let map = new Map<string,string>(); 860map.set("m","a"); 861map.set("a","b"); 862map.set("p","c"); 863let str3 = ArkTSUtils.ASON.stringify(map); 864console.info(str3); 865// Expected output: '{"m":"a","a":"b","p":"c"}' 866let set = new Set<string>(); 867set.add("s"); 868set.add("e"); 869set.add("t"); 870let str4 = ArkTSUtils.ASON.stringify(set); 871console.info(str4); 872// Expected output: '["s","e","t"]' 873let sendableMap = new collections.Map<string,string>(); 874sendableMap.set("send","a"); 875sendableMap.set("able","b"); 876sendableMap.set("map","c"); 877let str5 = ArkTSUtils.ASON.stringify(sendableMap); 878console.info(str5); 879// Expected output: '{"send":"a","able":"b","map":"c"}' 880let sendableSet = new collections.Set<string>(); 881sendableSet.add("send"); 882sendableSet.add("able"); 883sendableSet.add("set"); 884let str6 = ArkTSUtils.ASON.stringify(sendableSet); 885console.info(str6); 886// Expected output: '["send","able","set"]' 887``` 888 889### isSendable 890 891isSendable(value: Object | null | undefined): boolean 892 893Checks whether the passed-in value is of the sendable data type. 894 895**Atomic service API**: This API can be used in atomic services since API version 12. 896 897**System capability**: SystemCapability.Utils.Lang 898 899**Parameters** 900 901| Name| Type| Mandatory| Description| 902| -------- | -------- | -------- | -------- | 903| value | Object \| null \| undefined | Yes| Object to check.| 904 905**Return value** 906 907| Type| Description| 908| -------- | -------- | 909| boolean | Check result. The value **true** means that the passed-in value is of the sendable data type, and **false** means the opposite.| 910 911**Example** 912 913```ts 914import { ArkTSUtils } from '@kit.ArkTS' 915 916@Sendable 917function sendableFunc() { 918 console.info("sendableFunc") 919} 920 921if (ArkTSUtils.isSendable(sendableFunc)) { 922 console.info("sendableFunc is Sendable"); 923} else { 924 console.info("sendableFunc is not Sendable"); 925} 926// Expected output: 'SendableFunc is Sendable' 927``` 928 929## SendableLruCache<K, V><sup>18+</sup> 930 931Provides APIs to discard the least recently used data to make rooms for new elements when the cache is full. This class uses the Least Recently Used (LRU) algorithm, which believes that the recently used data may be accessed again in the near future and the least accessed data is the least valuable data and should be removed from the cache. **SendableLruCache** supports the Sendable feature, allowing it to store Sendable objects, which can be accessed safely across threads. 932 933### Attributes 934 935**Atomic service API**: This API can be used in atomic services since API version 18. 936 937**System capability**: SystemCapability.Utils.Lang 938 939| Name | Type | Readable| Writable| Description | 940| ------ | ------ | ---- | ---- | ---------------------- | 941| length | number | Yes | No | Total number of values in this cache.| 942 943**Example** 944 945```ts 946let pro = new ArkTSUtils.SendableLruCache<number, number>(); 947pro.put(2, 10); 948pro.put(1, 8); 949let result = pro.length; 950console.info('result = ' + result); 951// Expected output: result = 2 952``` 953 954### constructor<sup>18+</sup> 955 956constructor(capacity?: number) 957 958A constructor used to create a **SendableLruCache** instance. The default capacity of the cache is 64. 959 960**Atomic service API**: This API can be used in atomic services since API version 18. 961 962**System capability**: SystemCapability.Utils.Lang 963 964**Parameters** 965 966| Name | Type | Mandatory| Description | 967| -------- | ------ | ---- | ---------------------------- | 968| capacity | number | No | Capacity of the cache to create. The default value is **64**, and the maximum value is **2147483647**.| 969 970**Example** 971 972```ts 973let pro = new ArkTSUtils.SendableLruCache<number, number>(); 974``` 975 976### updateCapacity<sup>18+</sup> 977 978updateCapacity(newCapacity: number): void 979 980Changes the cache capacity. If the total number of values in the cache is greater than the specified capacity, the least used key-value pairs are deleted. 981 982**Atomic service API**: This API can be used in atomic services since API version 18. 983 984**System capability**: SystemCapability.Utils.Lang 985 986**Parameters** 987 988| Name | Type | Mandatory| Description | 989| ----------- | ------ | ---- | ---------------------------- | 990| newCapacity | number | Yes | New capacity of the cache. The maximum value is 2147483647. If the value is less than or equal to 0, an exception is thrown.| 991 992**Error codes** 993 994For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 995 996| ID | Error Message | 997| -------- | ------------------------------------------| 998| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified;2.Incorrect parameter types. | 999 1000**Example** 1001 1002```ts 1003let pro = new ArkTSUtils.SendableLruCache<number, number>(); 1004pro.updateCapacity(100); 1005``` 1006 1007### toString<sup>18+</sup> 1008 1009toString(): string 1010 1011Obtains the string representation of this cache. 1012 1013**Atomic service API**: This API can be used in atomic services since API version 18. 1014 1015**System capability**: SystemCapability.Utils.Lang 1016 1017**Return value** 1018 1019| Type | Description | 1020| ------ | -------------------------- | 1021| string | String representation of the cache, in the format of SendableLruCache[ maxSize = (maxSize), hits = (hitCount), misses = (missCount), hitRate = (hitRate) ], where **(maxSize)** indicates the maximum size of the cache, **(hitCount)** indicates the number of matched queries, **(missCount)** indicates the number of times that the number of mismatched queries, and **(hitRate)** indicates the matching rate.| 1022 1023**Example** 1024 1025```ts 1026let pro = new ArkTSUtils.SendableLruCache<number, number>(); 1027pro.put(2, 10); 1028pro.get(2); 1029pro.get(3); 1030console.info(pro.toString()); 1031// Expected output: SendableLruCache[ maxSize = 64, hits = 1, misses = 1, hitRate = 50% ] 1032// maxSize: maximum size of the cache. hits: number of matched queries. misses: number of mismatched queries. hitRate: matching rate. 1033``` 1034 1035### getCapacity<sup>18+</sup> 1036 1037getCapacity(): number 1038 1039Obtains the capacity of this cache. 1040 1041**Atomic service API**: This API can be used in atomic services since API version 18. 1042 1043**System capability**: SystemCapability.Utils.Lang 1044 1045**Return value** 1046 1047| Type | Description | 1048| ------ | ---------------------- | 1049| number | Capacity of the cache.| 1050 1051**Example** 1052 1053```ts 1054let pro = new ArkTSUtils.SendableLruCache<number, number>(); 1055let result = pro.getCapacity(); 1056console.info('result = ' + result); 1057// Expected output: result = 64 1058``` 1059 1060### clear<sup>18+</sup> 1061 1062clear(): void 1063 1064Clears all key-value pairs from this cache. 1065 1066**Atomic service API**: This API can be used in atomic services since API version 18. 1067 1068**System capability**: SystemCapability.Utils.Lang 1069 1070**Example** 1071 1072```ts 1073let pro = new ArkTSUtils.SendableLruCache<number, number>(); 1074pro.put(2, 10); 1075let result = pro.length; 1076pro.clear(); 1077let res = pro.length; 1078console.info('result = ' + result); 1079console.info('res = ' + res); 1080// Expected output: result = 1 1081// Expected output: res = 0 1082``` 1083 1084### getCreateCount<sup>18+</sup> 1085 1086getCreateCount(): number 1087 1088Obtains the number of times that the **createDefault** API is called to create objects. 1089 1090**Atomic service API**: This API can be used in atomic services since API version 18. 1091 1092**System capability**: SystemCapability.Utils.Lang 1093 1094**Return value** 1095 1096| Type | Description | 1097| ------ | -------------------| 1098| number | Number of times that the **createDefault** API is called.| 1099 1100**Example** 1101 1102```ts 1103@Sendable 1104class ChildLRUCache extends ArkTSUtils.SendableLruCache<number, number> { 1105 constructor() { 1106 super(); 1107 } 1108 createDefault(key: number): number { 1109 return key; 1110 } 1111} 1112 1113let lru = new ChildLRUCache(); 1114lru.put(2, 10); 1115lru.get(3); 1116lru.get(5); 1117let res = lru.getCreateCount(); 1118console.info('res = ' + res); 1119// Expected output: res = 2 1120// When the get operation is performed, if the key does not exist, call the createDefault API to check whether the return value is undefined. 1121// If the return value is not undefined, add the key and return value to the cache as a new entry and increase the creation count by one. 1122``` 1123 1124### getMissCount<sup>18+</sup> 1125 1126getMissCount(): number 1127 1128Obtains the number of times that the queried values are mismatched. 1129 1130**Atomic service API**: This API can be used in atomic services since API version 18. 1131 1132**System capability**: SystemCapability.Utils.Lang 1133 1134**Return value** 1135 1136| Type | Description | 1137| ------ | ------------------------ | 1138| number | Number of times that the queried values are mismatched.| 1139 1140**Example** 1141 1142```ts 1143let pro = new ArkTSUtils.SendableLruCache<number, number>(); 1144pro.put(2, 10); 1145pro.get(2); 1146let result = pro.getMissCount(); 1147console.info('result = ' + result); 1148// Expected output: result = 0 1149``` 1150 1151### getRemoveCount<sup>18+</sup> 1152 1153getRemoveCount(): number 1154 1155Obtains the number of times that key-value pairs in the cache are recycled. When the size of the cache exceeds the capacity limit, the least used key-value pairs will be recycled. 1156 1157**Atomic service API**: This API can be used in atomic services since API version 18. 1158 1159**System capability**: SystemCapability.Utils.Lang 1160 1161**Return value** 1162 1163| Type | Description | 1164| ------ | -------------------------- | 1165| number | Number of times that key-value pairs in the cache are recycled.| 1166 1167**Example** 1168 1169```ts 1170let pro = new ArkTSUtils.SendableLruCache<number, number>(); 1171pro.put(2, 10); 1172pro.updateCapacity(2); 1173pro.put(50, 22); 1174let result = pro.getRemoveCount(); 1175console.info('result = ' + result); 1176// Expected output: result = 0 1177``` 1178 1179### getMatchCount<sup>18+</sup> 1180 1181getMatchCount(): number 1182 1183Obtains the number of times that the queried values are matched. 1184 1185**Atomic service API**: This API can be used in atomic services since API version 18. 1186 1187**System capability**: SystemCapability.Utils.Lang 1188 1189**Return value** 1190 1191| Type | Description | 1192| ------ | -------------------------- | 1193| number | Number of times that the queried values are matched.| 1194 1195**Example** 1196 1197```ts 1198let pro = new ArkTSUtils.SendableLruCache<number, number>(); 1199pro.put(2, 10); 1200pro.get(2); 1201let result = pro.getMatchCount(); 1202console.info('result = ' + result); 1203// Expected output: result = 1 1204``` 1205 1206### getPutCount<sup>18+</sup> 1207 1208getPutCount(): number 1209 1210Obtains the number of additions to this cache. 1211 1212**Atomic service API**: This API can be used in atomic services since API version 18. 1213 1214**System capability**: SystemCapability.Utils.Lang 1215 1216**Return value** 1217 1218| Type | Description | 1219| ------ | ---------------------------- | 1220| number | Number of additions to the cache.| 1221 1222**Example** 1223 1224```ts 1225let pro = new ArkTSUtils.SendableLruCache<number, number>(); 1226pro.put(2, 10); 1227let result = pro.getPutCount(); 1228console.info('result = ' + result); 1229// Expected output: result = 1 1230``` 1231 1232### isEmpty<sup>18+</sup> 1233 1234isEmpty(): boolean 1235 1236Checks whether this cache is empty. 1237 1238**Atomic service API**: This API can be used in atomic services since API version 18. 1239 1240**System capability**: SystemCapability.Utils.Lang 1241 1242**Return value** 1243 1244| Type | Description | 1245| ------- | ---------------------------------------- | 1246| boolean | Check result. The value **true** means that the cache is empty and does not contain any key-value pairs, and **false** means the opposite.| 1247 1248**Example** 1249 1250```ts 1251let pro = new ArkTSUtils.SendableLruCache<number, number>(); 1252pro.put(2, 10); 1253let result = pro.isEmpty(); 1254console.info('result = ' + result); 1255// Expected output: result = false 1256``` 1257 1258### get<sup>18+</sup> 1259 1260get(key: K): V | undefined 1261 1262Obtains the value of a key. 1263 1264**Atomic service API**: This API can be used in atomic services since API version 18. 1265 1266**System capability**: SystemCapability.Utils.Lang 1267 1268**Parameters** 1269 1270| Name| Type| Mandatory| Description | 1271| ------ | ---- | ---- | ------------ | 1272| key | K | Yes | Key based on which the value is queried.| 1273 1274**Return value** 1275 1276| Type | Description | 1277| ------------------------ | ------------------------------------------------------------ | 1278| V \| undefined | If the specified key exists in the cache, the return value is the value associated with that key. If not, the **createDefault** API is called. If **createDefault** returns undefined, the return value is undefined; otherwise, the return value is whatever **createDefault** comes up with.| 1279 1280**Error codes** 1281 1282For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1283 1284| ID| Error Message| 1285| -------- | -------- | 1286| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1287 1288**Example** 1289 1290```ts 1291let pro = new ArkTSUtils.SendableLruCache<number, number>(); 1292pro.put(2, 10); 1293let result = pro.get(2); 1294console.info('result = ' + result); 1295// Expected output: result = 10 1296``` 1297 1298### put<sup>18+</sup> 1299 1300put(key: K,value: V): V 1301 1302Adds a key-value pair to this cache and returns the value associated with the key. If the total number of values in the cache is greater than the specified capacity, the deletion operation is performed. 1303 1304**Atomic service API**: This API can be used in atomic services since API version 18. 1305 1306**System capability**: SystemCapability.Utils.Lang 1307 1308**Parameters** 1309 1310| Name| Type| Mandatory| Description | 1311| ------ | ---- | ---- | -------------------------- | 1312| key | K | Yes | Key of the key-value pair to add. | 1313| value | V | Yes | Value of the key-value pair to add.| 1314 1315**Return value** 1316 1317| Type| Description | 1318| ---- | ------------------------------------------------------------ | 1319| V | Value of the key-value pair added. If the key or value is empty, error code 401 is thrown.| 1320 1321**Error codes** 1322 1323For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1324 1325| ID| Error Message| 1326| -------- | -------- | 1327| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1328 1329**Example** 1330 1331```ts 1332let pro = new ArkTSUtils.SendableLruCache<number, number>(); 1333let result = pro.put(2, 10); 1334console.info('result = ' + result); 1335// Expected output: result = 10 1336``` 1337 1338### values<sup>18+</sup> 1339 1340values(): V[] 1341 1342Obtains all values in this cache, listed from the most to the least recently accessed, where the most recently accessed indicates the key-value pair with the latest operation. 1343 1344**Atomic service API**: This API can be used in atomic services since API version 18. 1345 1346**System capability**: SystemCapability.Utils.Lang 1347 1348**Return value** 1349 1350| Type | Description | 1351| --------- | ------------------------------------------------------------ | 1352| V [] | All values in the cache, listed from the most to the least recently accessed.| 1353 1354**Example** 1355 1356```ts 1357let pro = new ArkTSUtils.SendableLruCache<number|string,number|string>(); 1358pro.put(2, 10); 1359pro.put(2, "anhu"); 1360pro.put("afaf", "grfb"); 1361let result = pro.values(); 1362console.info('result = ' + result); 1363// Expected output: result = anhu,grfb 1364``` 1365 1366### keys<sup>18+</sup> 1367 1368keys(): K[] 1369 1370Obtains all keys in this cache, listed from the most to the least recently accessed. 1371 1372**Atomic service API**: This API can be used in atomic services since API version 18. 1373 1374**System capability**: SystemCapability.Utils.Lang 1375 1376**Return value** 1377 1378| Type | Description | 1379| --------- | ------------------------------------------------------------ | 1380| K [] | All keys in the cache, listed from the most to the least recently accessed.| 1381 1382**Example** 1383 1384```ts 1385let pro = new ArkTSUtils.SendableLruCache<number, number>(); 1386pro.put(2, 10); 1387pro.put(3, 1); 1388let result = pro.keys(); 1389console.info('result = ' + result); 1390// Expected output: result = 2,3 1391``` 1392 1393### remove<sup>18+</sup> 1394 1395remove(key: K): V | undefined 1396 1397Removes a key and its associated value from this cache and returns the value associated with the key. If the key does not exist, undefined is returned. 1398 1399**Atomic service API**: This API can be used in atomic services since API version 18. 1400 1401**System capability**: SystemCapability.Utils.Lang 1402 1403**Parameters** 1404 1405| Name| Type| Mandatory| Description | 1406| ------ | ---- | ---- | -------------- | 1407| key | K | Yes | Key to remove.| 1408 1409**Return value** 1410 1411| Type | Description | 1412| ------------------------ | ------------------------------------------------------------ | 1413| V \| undefined | Returns an **Optional** object containing the removed key-value pair if the key exists in the cache; returns undefined if the key does not exist; throws an error if **null** is passed in for **key**.| 1414 1415**Error codes** 1416 1417For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1418 1419| ID| Error Message| 1420| -------- | -------- | 1421| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1422 1423**Example** 1424 1425```ts 1426let pro = new ArkTSUtils.SendableLruCache<number, number>(); 1427pro.put(2, 10); 1428let result = pro.remove(20); 1429console.info('result = ' + result); 1430// Expected output: result = undefined 1431``` 1432 1433### contains<sup>18+</sup> 1434 1435contains(key: K): boolean 1436 1437Checks whether the cache contains the specified key. 1438 1439**Atomic service API**: This API can be used in atomic services since API version 18. 1440 1441**System capability**: SystemCapability.Utils.Lang 1442 1443**Parameters** 1444 1445| Name| Type | Mandatory| Description | 1446| ------ | ------ | ---- | ---------------- | 1447| key | K | Yes | Key to check.| 1448 1449**Return value** 1450 1451| Type | Description | 1452| ------- | ------------------------------------------ | 1453| boolean | Check result. The value **true** means that the cache contains the specified key, and **false** means the opposite.| 1454 1455**Error codes** 1456 1457For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1458 1459| ID| Error Message| 1460| -------- | -------- | 1461| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1462 1463**Example** 1464 1465```ts 1466let pro = new ArkTSUtils.SendableLruCache<number, number>(); 1467pro.put(2, 10); 1468let result = pro.contains(2); 1469console.info('result = ' + result); 1470// Expected output: result = true 1471``` 1472 1473### entries<sup>18+</sup> 1474 1475entries(): IterableIterator<[K,V]> 1476 1477Obtains a new iterator object that contains all key-value pairs in this object. 1478 1479**Atomic service API**: This API can be used in atomic services since API version 18. 1480 1481**System capability**: SystemCapability.Utils.Lang 1482 1483**Return value** 1484 1485| Type | Description | 1486| ----------- | -------------------- | 1487| IterableIterator<[K, V]> | Iterable array.| 1488 1489**Example** 1490 1491```ts 1492let pro = new ArkTSUtils.SendableLruCache<number, number>(); 1493pro.put(2, 10); 1494pro.put(3, 15); 1495let pair:Iterable<Object[]> = pro.entries(); 1496let arrayValue = Array.from(pair); 1497for (let value of arrayValue) { 1498 console.info(value[0]+ ', '+ value[1]); 1499 // Expected output: 1500 // 2, 10 1501 // 3, 15 1502} 1503``` 1504