Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Collections kotlin将此转换的结果分配给变量_Collections_Kotlin_Iterator - Fatal编程技术网

Collections kotlin将此转换的结果分配给变量

Collections kotlin将此转换的结果分配给变量,collections,kotlin,iterator,Collections,Kotlin,Iterator,嗨,我正在使用下一个代码: private fun getAttributesMap(navMenu: NavItem?): AttributesMap { var attributesString = navMenu?.attributes val attributesMap = mutableMapOf<String, String>() attributesString?.lines()?.map { va

嗨,我正在使用下一个代码:

private fun getAttributesMap(navMenu: NavItem?): AttributesMap {
        var attributesString = navMenu?.attributes
        val attributesMap = mutableMapOf<String, String>()
        attributesString?.lines()?.map {
            val pair = it.split("=")
            if (pair?.size == 2) {
                attributesMap.put(pair[0], pair[1])
            }
        }
        return AttributesMap(attributesMap)
    }
private fun GetAttributeMap(导航菜单:导航项?):属性映射{
var AttributeString=navMenu?.attributes
val attributemap=mutableMapOf()
属性字符串?.lines()?.map{
val pair=it.split(“=”)
如果(对?.size==2){
AttributeMap.put(对[0],对[1])
}
}
返回属性映射(attributemap)
}
但根据文件:

我应该能够将此转换的结果分配给AttributeMap。有关于kotlin.map方法的线索吗


谢谢

map
filter
组合使用将有效:

val attributesMap = attributesString.lines()
    .map { it.split("=") }
    .filter { it.size == 2 } //filter all with more or less elements
    .map { it[0] to it[1] } //convert to Pair
    .toMap() //convert to Map

的确-不鼓励使用
.map
执行副作用。FWIW我认为稍微不太详细的选项是用
.associate{it[0]to it[1]}
@OliverCharlesworth谢谢你的提示。直到今天我才知道这个功能。