运行代码HashMap(it)会发生什么? [英] What happened when the code HashMap(it) is run?
问题描述
以下示例代码来自Kotlin-for-Android-Developers,网址为
The following sample code is from Kotlin-for-Android-Developers at https://github.com/antoniolg/Kotlin-for-Android-Developers/blob/master/app/src/main/java/com/antonioleiva/weatherapp/data/db/ForecastDb.kt
我无法完全理解代码DayForecast(HashMap(it))
. 它"是什么意思?
I can't understand completely the code DayForecast(HashMap(it))
. what does "it" mean?
还有,执行parseList { DayForecast(HashMap(it)) }
时会发生什么?
And more, what happend when the parseList { DayForecast(HashMap(it)) }
is executed ?
override fun requestForecastByZipCode(zipCode: Long, date: Long) = forecastDbHelper.use {
val dailyRequest = "${DayForecastTable.CITY_ID} = ? AND ${DayForecastTable.DATE} >= ?"
val dailyForecast = select(DayForecastTable.NAME)
.whereSimple(dailyRequest, zipCode.toString(), date.toString())
.parseList { DayForecast(HashMap(it)) }
}
class DayForecast(var map: MutableMap<String, Any?>) {
var _id: Long by map
var date: Long by map
var description: String by map
var high: Int by map
var low: Int by map
var iconUrl: String by map
var cityId: Long by map
constructor(date: Long, description: String, high: Int, low: Int, iconUrl: String, cityId: Long)
: this(HashMap()) {
this.date = date
this.description = description
this.high = high
this.low = low
this.iconUrl = iconUrl
this.cityId = cityId
}
}
已添加
在下面的示例代码中,我可以理解代码val doubled = ints.map {it * 2 }
中的"it","it"是var ints的元素,例如10、20、30!
In the following Sample Code, I can understand "it" in the code val doubled = ints.map {it * 2 }
, "it" is the element of var ints, such as 10, 20, 30 !
但是在代码val dailyForecast = select(DayForecastTable.NAME).whereSimple(dailyRequest, zipCode.toString(), date.toString()).parseList { DayForecast(HashMap(it)) }
中,它"是什么意思?
But in the code val dailyForecast = select(DayForecastTable.NAME).whereSimple(dailyRequest, zipCode.toString(), date.toString()).parseList { DayForecast(HashMap(it)) }
, what does "it" mean ?
示例代码
var ints= listOf(10,20,30);
val doubled = ints.map {it * 2 }
fun <T, R> List<T>.map(transform: (T) -> R): List<R> {
val result = arrayListOf<R>()
for (item in this)
result.add(transform(item))
return result
}
推荐答案
正如Lym Zoy所说,it
是闭包的单个参数的隐式名称.
As Lym Zoy says it
is the implicit name of the single argument to a closure.
如果您不熟悉Kotlin中的闭包和高阶函数,则可以阅读这里.具有闭包/函数/lambda的函数基本上是在寻求帮助以完成其工作的函数.
If you are not familiar with closures and higher order functions in Kotlin, you can read about them here . A function that take a closure/function/lambda, is one that is basically asking for some help to do it's job.
我喜欢使用 sortedBy 作为一个很好的例子. sortedBy
是Kotlin集合库中的一个函数,它将对集合进行排序,但是要使其正常工作,它需要为每个项目设置一个可比较的属性.解决此问题的方法是,它要求您(sortedBy函数的用户)提供一个接受集合成员并返回可比较属性的函数.例如,如果集合是Person对象的列表,那么如果要按firstName,lastName或age进行排序,则可以提供sortedBy
一个不同的闭包.
I like using sortedBy as a good example. sortedBy
is a function in the Kotlin collections library that will sort a collection, but in order for it to work it needs a comparable attribute for each item. The way it solves this is it asks you, the user of the sortedBy function, to provide a function that takes a member of the collection and returns a comparable attribute. For example if the collection was a list of Person objects, you could provide sortedBy
a different closure if you wanted to sort by firstName, lastName or age.
这是一个简单的示例,您还可以在此处上找到展示了sortedBy
如何接受带有成员的闭包参数,并返回一个类似的属性,sortedBy
可以使用该属性对集合的各个成员进行排名.在第一种情况下,闭包/函数返回成员的年龄,在第二种情况下,闭包/函数返回lastName(使用隐式形式),二者均为Comparable,Int和String.
Here is a quick example which you can also find here , which shows how sortedBy
can take a closure argument which takes a member and returns a comparable attribute which sortedBy
can use to rank the various members of the collection. In the first case the closure/function returns the age of the member and in the second case the closure returns the lastName (using the implicit form), both of which are Comparable, an Int and a String.
data class Person(val firstName: String, val lastName: String, val age: Int)
fun main(args: Array<String>) {
val people = listOf( Person("Jane", "Jones", 27), Person("Johm", "Smith", 22), Person("John", "Jones", 29))
val byAge = people.sortedBy { person -> person.age } // explicit argument: person is a memeber of the List of Persons
val byLastName = people.sortedBy { it.lastName } // implict argument: "it" is also a member of the List of Persons
println(people)
println(byAge)
println(byLastName)
}
回到您的特定问题的详细信息.
Getting back to the detail of your specific question.
In your question the parseList function found here is defined as such:
fun <T : Any> SelectQueryBuilder.parseList(parser: (Map<String, Any?>) -> T): List<T> =
parseList(object : MapRowParser<T> {
override fun parseRow(columns: Map<String, Any?>): T = parser(columns)
})
此函数带有一个需要一个Map<String, Any?>
This is a function that takes a closure that expects one argument of type Map<String, Any?>
因此,在您的问题所示的通话中:
So in the call shown in your question:
.parseList { DayForecast(HashMap(it)) }
其中{ DayForecast(HashMap(it)) }
是预期传递给parseList的闭包,
也可以使用较长的形式{ arg -> DayForecast(HashMap(arg) }
,但是较短的形式{ DayForecast(HashMap(it)) }
是更惯用的形式,其中使用it
作为参数允许您跳过arg ->
部分.
where { DayForecast(HashMap(it)) }
is the closure expected to be passed to parseList,
the longer form { arg -> DayForecast(HashMap(arg) }
could have been used as well but the short form { DayForecast(HashMap(it)) }
is the more idiomatic form where using it
as the argument allows you to skip the arg ->
part.
因此,在这种情况下,it
是parseList
函数提供的Map对象.然后,将it
引用的对象作为唯一参数传递给
So in this case it
is a Map object provided by the parseList
function. The object referred to by it
is then passed as the lone argument to a HashMap constructor (which not surprisingly expects a Map), the result of that construction, is then being passed to the constructor of DayForecast
这篇关于运行代码HashMap(it)会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!