为什么这段 Java 代码在没有显式类型参数的情况下无法在 Kotlin 中编译Collectors.toList<String>()
?有没有更惯用的方法来做到这一点?
// works
List<String> folders = Files.walk(Paths.get(args[0]))
.filter(it -> it.toFile().isDirectory())
.map(it -> it.toAbsolutePath().toString())
.collect(Collectors.toList());
// does not compile - resulting type is `MutableList<in String!>..List<Any?>?` which is not compatible to `List<String>`
val folders: List<String> = Files.walk(Paths.get(args[0]))
.filter { it.toFile().isDirectory }
.map { it.toAbsolutePath().toString() }
.collect(Collectors.toList())
// compiles
val folders: List<String> = Files.walk(Paths.get(args[0]))
.filter { it.toFile().isDirectory }
.map { it.toAbsolutePath().toString() }
.collect(Collectors.toList<String>())