![]() This example shows how even complex operations can be performed easily with Kotlin’s powerful APIs. We’re making use of the with scope function here to keep our code tidy. If you create a class with the object keyword instead of class, the Kotlin compiler makes the constructor private, creates a. Sales.forEach ĪssertEquals(17, inventory) // 24 - 7 + 0ĪssertEquals(13, inventory) // 14 - 4 + 3ĪssertEquals(11, inventory) // 9 - 5 + 7ĪssertEquals(5, inventory) // 0 - 0 + 5 In Kotlin, object is a special class that only has one instance. The best thing of the kotlin functions is that we can able to call it any number of times and utilised with the object instance. The Kotlin List.any() function can be used to check if the list has at least one element, or if the list has at least one element that matches the given. Val shipments = mapOf("Chocolate" to 3, "Strawberry" to 7, "Rocky Road" to 5) We’ll subtract all the entries in the sales map and then add all the entries from the shipments map to update each flavor’s Quantity: val sales = mapOf("Vanilla" to 7, "Chocolate" to 4, "Strawberry" to 5) After a day of receiving shipments and selling ice cream, we need to update our store’s inventory map. ![]() The forEach method performs an action on each entry in a given map. As a final example, we’ll use what we’ve learned already and introduce the forEach method. ![]()
0 Comments
Leave a Reply. |