fun contentValuesOf(vararg pairs: Pair<String, Any?>) = ContentValues().apply { for (pair in pairs) { val key = pair.first val value = pair.second when (value) { is Int -> put(key, value) is Long -> put(key, value) is Short -> put(key, value) is Float -> put(key, valu...
fun cvOf(vararg pairs: Pair<String, Any?>): ContentValues { val cv = ContentValues() for (pair in pairs) { val key = pair.first when (val value = pair.second) { is Int -> cv.put(key, value) is Long -> cv.put(key, value) is Float -> cv.put(key, value) is Double -> ...
使用“to” 来声明 map 的 key 与 value 之间的对应关系,这种形式的函数调用被称为中缀调用。 kotlin 标准库中对 to 函数的声明如下所示,其作为扩展函数存在,且是一个泛型函数,返回值 Pair 最终再通过解构声明分别将 key 和 value 传给 Map publicinfixfun<A,B>A.to(that:B):Pair<A,B>=Pair(this,that...
* Represents a generic pair of two values. * * There is no meaning attached to values in this class, it can be used for any purpose. * Pair exhibits value semantics, i.e. two pairs are equal if both components are equal. * * An example of decomposing it into values: * @sample sa...
return sb.toString() } 顶层函数是包内成员,包内直接访问。若包外访问,需要import(IDEA等开发工具会为你自动import) 顶层函数是都是静态函数,默认函数所在的文件名加KT作为容器类,比如上述joinToString方法是在Example3_2文件名下的顶层函数,在Java里调用是时 ...
// code 4classCar(varbrand:String,varprice:Double){// 普通类 解构operatorfuncomponent1():String{returnthis.brand}operatorfuncomponent2():Double{returnthis.price}} operator 关键字可以用来重载操作符或者实现一个约定。这里就是实现了一个约定。这样写之后就可以像 data class 一样进行解构了。Kotlin 的...
Example for Gradle: implementation("io.reactivex.rxjava3:rxkotlin:3.x.y") RxKotlin 2.x Example for Maven: <dependency> <groupId>io.reactivex.rxjava2</groupId> <artifactId>rxkotlin</artifactId> <version>2.x.y</version> </dependency> ...
funMIME(varargmimeToData:Pair<String,String>):MimeTypedResult For example: MIME("text/html"to"Some HTML","text/plain"to"No HTML for text clients") Another approach is to use theHTMLhelper function, which provides a simpler way to display HTML content directly: funHTML(text:String):Mime...
predefined class in Kotlin that is used to store and return two variables at a time. Those two variables can be of the same type or of a different type. So, whenever you want to return more than one variable from a function then you can usePairin your function. But how to usePair?
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" map.forEach { (key, value) -> println("$key = $value") } } use Iterator function The Map.keys...