java - Kotlin lambda 主体中的 `it` 是什么?

标签 java file jvm kotlin

例如在下面这两个代码中:

File("./file1.txt").forEachLine { println(it) }

File("somefile.txt").bufferedWriter().use { out ->
    history.forEach {
        out.write("${it.key}, ${it.value}\n")
    }
}

在这段代码中,it 是什么意思?

最佳答案

it variable 是 lambda 中的隐式参数。

One other helpful convention is that if a function literal has only one parameter, its declaration may be omitted (along with the ->), and its name will be it:

关于java - Kotlin lambda 主体中的 `it` 是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44773351/

相关文章:

java - 从其他项目链接java源代码?

java - 反序列化 : get the data from the custom class

java - 回收器 View 无法正常工作,没有任何错误

java - 多线程环境中的对象深度锁定

c++ - boost asio 套接字 : fastest way to read file from hard drive?

c# - 检测文件扩展名 c#

python - (result, consumed) = self._buffer_decode(data, self.errors, final) 错误

java - Stackdriver GCE JVM 监控设置问题

java - 为什么强制垃圾收集会增加分配给 java 进程的内存?

java - 从命令行运行时 JVM 变量转义等于