fun getUpperFruits() { val list = listOf("Apple", "Banana", "Orange", "Pear", "Grape", "Watermelon") val newlist = list.map { it.toUpperCase() } for (fruit in newlist) { println(fruit) } } 1. 2. 3. 4. 5. 6. 7. filter: Example: fun filterFruits() {...
//www.deadcoderising.com/kotlin-improve-readability-by-using-the-infix-notation/ * */ fun main() { val msg = json { obj( "timerId" to "123", "bool" to true, "num" to 1, "test1" to "test1" ) } println(msg) // --- val map1 = mapOf( 1 to "one", 2.to("two"), P...
B> = Pair(this, that) //我们仿照它写一个 public infix fun <A,B> A.with(that: B): Pair<A,B> = Pair(this,that) fun main() { val map = mapOf("Apple" with 1,"Banana" with 2,"Orange" with 3,"Pear" with 4,"Grape" with 5) } ...
The example creates a map using Java'sHashMapand prints the values and pairs to the console. Kotlin map size The size of a map (the number of pairs) can be determined with thesizeproperty and thecountmethod. MapSize.kt package com.zetcode fun main() { val items = mapOf("coins" to...
Here is an example fun main() { val map = HashMap<String, String>() map["1"] = "one" map["2"] = "two" map["3"] = "three" map["4"] = "four" map["5"] = "five" val iterator = map.keys.iterator() while (iterator.hasNext()) { val key = iterator.next() val value...
Example@433c675d, 这里委托了 p 属性 标准委托 Kotlin 的标准库中已经内置了很多工厂方法来实现属性的委托。 延迟属性 Lazy lazy() 是一个函数, 接受一个 Lambda 表达式作为参数, 返回一个 Lazy <T> 实例的函数,返回的实例可以作为实现延迟属性的委托: 第一次调用 get() 会执行已传递给 lazy() 的 lamda ...
//委托者类Example,这里将会将其属性计算委托给Delegate实现classExample{//属性委托的语法,同上一个章节阐述的一样,使用by关键字修饰即可。vartest:StringbyDelegate()}//测试类classMain{companionobject{@JvmStaticfunmain(args:Array<String>){valexample=Example()//生成一个委托者对象println(example.test)//这里...
// Inferred type: Map<String, Int> Map<String, Int>的泛型类型是从传递给Pair构造函数的参数的类型推断出来的。我们可能会想知道,如果用于创建map的推断类型的对不同会发生什么?第一对是Pair<String, Int>,第二对是Pair<String, String>: var map = mapOf("Mount Everest" to 8848, "K2" to "4017"...
Example@433c675d 的 p 属性赋值为 RunoobExample@433c675d, 这里委托了 p 属性 可观察属性 Observable @[Kotlin 的标准库中已经内置了很多工厂方法来实现属性的委托。] import kotlin.properties.Delegates class User { var name: String by Delegates.observable("初始值") { prop, old, new -> println("...
Kotlin MutableMap Example - 1 traversing MutableMap Let's create an example to create a MutableMap using mutablemapOf() function and traverse it. In this example we create three different types (MutableMap<Int, String>, MutableMap<String, String> and MutableMap<Any, Any>) of MutableMap wit...