Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.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
如何将这个排序映射方法从java转换为Kotlin_Java_Kotlin - Fatal编程技术网

如何将这个排序映射方法从java转换为Kotlin

如何将这个排序映射方法从java转换为Kotlin,java,kotlin,Java,Kotlin,如何将以下Java转换为Kotlin private Map<String, Map<String, String>> example(List<Dto> dtos) { if (dtos.isEmpty()) { return ImmutableMap.of(); } final Map<String, Map<String, String>> outerMapByField1 = new

如何将以下Java转换为Kotlin

private Map<String, Map<String, String>> example(List<Dto> dtos) {

    if (dtos.isEmpty()) {
        return ImmutableMap.of();
    }

    final Map<String, Map<String, String>> outerMapByField1 = new TreeMap<>();

    dtos.forEach(dto -> {

        final String field1 = dto.getField1();

        final Map<String, String> innerMapByField2 = outerMapByField1.computeIfAbsent(field1, (key) -> new TreeMap<>());
        innerMapByField2.put(dto.getField2(), dto.getField3());

    });

    return outerMapByField1;

}
私有映射示例(列出DTO){
if(dtos.isEmpty()){
返回ImmutableMap.of();
}
最终映射outerMapByField1=新树映射();
dtos.forEach(dto->{
最后一个字符串field1=dto.getField1();
最终映射innerMapByField2=outerMapByField1.ComputeFabSent(字段1,(键)->new TreeMap());
innerMapByField2.put(dto.getField2(),dto.getField3());
});
返回outerMapByField1;
}
IntelliJ转换为我提供了一些可以使用的东西,而我重构它的尝试最终导致了以下无法编译的结果

private fun example(dtos: List<Dto>): Map<String, Map<String, String>> {

    if (dtos.isEmpty()) {
        return ImmutableMap.of()
    }

    val outerMapByField1 = sortedMapOf<String, Map<String, String>>()

    dtos.forEach { dto ->

        val field1 = dto.field1

        val innerMapByField2 = outerMapByField1.getOrPut(field1) { key -> TreeMap() }
        innerMapByField2.put(dto.field2, dto.field3)

    }

    return outerMapByField1

}
私人娱乐示例(dtos:List):地图{
if(dtos.isEmpty()){
返回ImmutableMap.of()
}
val outerMapByField1=sortedMapOf()
dtos.forEach{dto->
val field1=dto.field1
val innerMapByField2=outerMapByField1.getOrPut(field1){key->TreeMap()}
innerMapByField2.put(dto.field2,dto.field3)
}
返回outerMapByField1
}

映射在Kotlin中默认情况下是不可变的,因此添加一些可变性将使其可编译:

 private fun example(dtos: List<Dto>): Map<String, Map<String, String>> {

        if (dtos.isEmpty()) {
            return emptyMap()
        }

        val outerMapByField1 = sortedMapOf<String, MutableMap<String, String>>()

        dtos.forEach { dto ->

            val field1 = dto.field1

            val innerMapByField2 = outerMapByField1.getOrPut(field1) { mutableMapOf() }
            innerMapByField2.put(dto.field2, dto.field3)

        }

        return outerMapByField1

    }
私人娱乐示例(dtos:List):地图{
if(dtos.isEmpty()){
返回空映射()
}
val outerMapByField1=sortedMapOf()
dtos.forEach{dto->
val field1=dto.field1
val innerMapByField2=outerMapByField1.getOrPut(field1){mutableMapOf()}
innerMapByField2.put(dto.field2,dto.field3)
}
返回outerMapByField1
}
也许是这样的:

private fun example(dtos: List<Dto>) = dtos.groupBy { it.field1 }.map { entry ->
    entry.key to entry.value.associateBy { it.field2 }.mapValues { it.value.field3 }.toSortedMap()
}.toMap().toSortedMap()
private-fun-example(dtos:List)=dtos.groupBy{it.field1}.map{entry->
entry.key到entry.value.associateBy{it.field2}.mapValues{it.value.field3}.toSortedMap()
}.toMap().toSortedMap()

映射在Kotlin中默认情况下是不可变的,因此添加一些可变性将使其可编译:

 private fun example(dtos: List<Dto>): Map<String, Map<String, String>> {

        if (dtos.isEmpty()) {
            return emptyMap()
        }

        val outerMapByField1 = sortedMapOf<String, MutableMap<String, String>>()

        dtos.forEach { dto ->

            val field1 = dto.field1

            val innerMapByField2 = outerMapByField1.getOrPut(field1) { mutableMapOf() }
            innerMapByField2.put(dto.field2, dto.field3)

        }

        return outerMapByField1

    }
私人娱乐示例(dtos:List):地图{
if(dtos.isEmpty()){
返回空映射()
}
val outerMapByField1=sortedMapOf()
dtos.forEach{dto->
val field1=dto.field1
val innerMapByField2=outerMapByField1.getOrPut(field1){mutableMapOf()}
innerMapByField2.put(dto.field2,dto.field3)
}
返回outerMapByField1
}
也许是这样的:

private fun example(dtos: List<Dto>) = dtos.groupBy { it.field1 }.map { entry ->
    entry.key to entry.value.associateBy { it.field2 }.mapValues { it.value.field3 }.toSortedMap()
}.toMap().toSortedMap()
private-fun-example(dtos:List)=dtos.groupBy{it.field1}.map{entry->
entry.key到entry.value.associateBy{it.field2}.mapValues{it.value.field3}.toSortedMap()
}.toMap().toSortedMap()
  • 首先,您的
    outerMapByField1
    中的值是实例,这是一个只读接口。要修改这些地图,您需要将它们存储为s(根据@Heinrisch的回答)
  • KOTLIN提供了一个您可以考虑使用的函数,而不是<代码> imTababLima.Of()
  • 传递给的lamda不接受参数,因此必须删除
    key->
以下是所有这些应用程序:

private fun example(dtos: List<Dto>): Map<String, Map<String, String>> {

    if (dtos.isEmpty()) {
        return emptyMap()
    }

    val outerMapByField1 = sortedMapOf<String, MutableMap<String, String>>()

    dtos.forEach { dto ->

        val field1 = dto.field1

        val innerMapByField2 = outerMapByField1.getOrPut(field1) { TreeMap() }
        innerMapByField2.put(dto.field2, dto.field3)

    }

    return outerMapByField1

}
私人娱乐示例(dtos:List):地图{
if(dtos.isEmpty()){
返回空映射()
}
val outerMapByField1=sortedMapOf()
dtos.forEach{dto->
val field1=dto.field1
val innerMapByField2=outerMapByField1.getOrPut(field1){TreeMap()}
innerMapByField2.put(dto.field2,dto.field3)
}
返回outerMapByField1
}
  • 首先,您的
    outerMapByField1
    中的值是实例,这是一个只读接口。要修改这些地图,您需要将它们存储为s(根据@Heinrisch的回答)
  • KOTLIN提供了一个您可以考虑使用的函数,而不是<代码> imTababLima.Of()
  • 传递给的lamda不接受参数,因此必须删除
    key->
以下是所有这些应用程序:

private fun example(dtos: List<Dto>): Map<String, Map<String, String>> {

    if (dtos.isEmpty()) {
        return emptyMap()
    }

    val outerMapByField1 = sortedMapOf<String, MutableMap<String, String>>()

    dtos.forEach { dto ->

        val field1 = dto.field1

        val innerMapByField2 = outerMapByField1.getOrPut(field1) { TreeMap() }
        innerMapByField2.put(dto.field2, dto.field3)

    }

    return outerMapByField1

}
私人娱乐示例(dtos:List):地图{
if(dtos.isEmpty()){
返回空映射()
}
val outerMapByField1=sortedMapOf()
dtos.forEach{dto->
val field1=dto.field1
val innerMapByField2=outerMapByField1.getOrPut(field1){TreeMap()}
innerMapByField2.put(dto.field2,dto.field3)
}
返回outerMapByField1
}

请发布编译器错误请发布编译器错误非常感谢。这是正确的。只有一个问题。内部地图也必须进行排序。因此,使用TreeMap而不是mutableMapOf()。@KevinStembridge我不知道为什么您需要排序映射,但添加了一个更实用的解决方案。非常感谢。这是正确的。只有一个问题。内部地图也必须进行排序。因此,使用TreeMap而不是mutableMapOf()。@KevinStembridge我不知道为什么您需要排序映射,但添加了一个更实用的解决方案。