我有一些代码:
directoryChooser.title = "Select the directory"
val file = directoryChooser.showDialog(null)
if (file != null) {
    var files = Files.list(file.toPath())
            .filter { f ->
                f.fileName.endsWith("zip") && f.fileName.endsWith("ZIP")
                        && (f.fileName.startsWith("1207") || f.fileName.startsWith("4407") || f.fileName.startsWith("1507") || f.fileName.startsWith("9007") || f.fileName.startsWith("1807"))
            }
    for (f in files) {
        textArea.appendText(f.toString() + "\n")
    }
}
如果我collect(Collectors.toList())在过滤器结束时调用,我得到:
Error:(22, 13) Kotlin: [Internal Error] org.jetbrains.kotlin.codegen.CompilationException: Back-end (JVM) Internal error: no descriptor for type constructor of ('Captured(in ('Path'..'Path?'))'..'CapturedTypeConstructor(in ('Path'..'Path?'))?')
Cause: no descriptor for type constructor of ('Captured(in ('Path'..'Path?'))'..'CapturedTypeConstructor(in ('Path'..'Path?'))?')
File …在Python中,有列表推导和类似的地图和集合结构.在Kotlin中,任何具有类似名称的文档都没有任何内容.
这些理解的等价物是什么?例如,Python 3 Patterns,Recipes and Idioms中的那些.其中包括对以下方面的理解:
public class Main {
    static class Account {
        private Long id;
        private String name;
        private Book book;
        public Account(Long id, String name, Book book) {
            this.id = id;
            this.name = name;
            this.book = book;
        }
        public String getName() {
            return name;
        }
    }
    public static void main(String[] args) {
        List<Account> data1 = new ArrayList<>();
        data1.add(new Account(1L,"name",null));
        List<String> collect = data1.stream().map(account -> account.getName()).collect(Collectors.toList());
        System.out.println(collect);
    }
}
在上面的代码中,我试图转换以下行
List<String> collect = data1.stream().map(account -> account.getName()).collect(Collectors.toList());
到kotlin代码.Kotlin在线编辑器给了我以下代码
 val collect = data1.stream().map({ account-> account.getName() }).collect(Collectors.toList()) …我正在用kotlin开发一些日志分析工具.我有大量的传入日志,因此无法将它们全部加载到内存中,我需要以"管道"方式处理它们.我发现有两件事令我失望:
filter,map等)不懒惰.例如,我有1 GB的日志,并希望获得与给定正则表达式匹配的前十行的长度.如果按原样编写,过滤和转换将应用于内存中的整个千兆字节的字符串.l.stream(),我定义为val l = ArrayList<String>().编译器说:"未解决的参考:流".所以问题是:你是否会使收集功能变得懒惰?为什么我不能访问该stream()方法?
所以这可能更多是关于函数式编程而不是Kotlin,我在那个阶段有点知识是危险的,我在Kotlin写了应用程序所以似乎公平地问Kotlin问题是我感兴趣的Kotlins结构.
我有一系列项目,它们分三批,所以流可能看起来像
1,a,+,2,b,*,3,c,&.......
我想要做的是把它分成三个列表,目前我这样做是通过分成两个列表,一个包含数字,一个包含其他所有,然后取结果的后半部分,字母和符号和再次划分为字母和符号,因此我最终得到三个列表.
这让我觉得有些低效,也许功能方法不是最好的方法.
有没有一种有效的方法可以做到这一点,是我的选择,这个还是for循环?
谢谢
我试图将一个对象数组映射到具有不同类型对象的另一个数组,我曾经在Java 8中使用流来做到这一点,这很简单,实例化一个对象设置其值并返回该对象。我刚切换到Kotlin,实际上有时候做这种操作更令人困惑。我发现的所有示例都非常简单,找不到我想要的东西。
我有这个BalanceMap类:
data class BalanceMap @JsonCreator constructor(
        var balType: String,
        var value: Any
)
我正在从Web服务获取数据。
val balances: List<AcctBal> = res.getAcctBals();
AcctBal类如下所示
public class AcctBal {
  @SerializedName("CurAmt")
  @Expose
  private CurAmt curAmt;
  @SerializedName("Desc")
  @Expose
  private String desc;
  @SerializedName("ExpDt")
  @Expose
  private LocalDateTime expDt;
}
并尝试将该响应映射到 var balanceList: List<BalanceMap>
balances.map {}
-> var balanceList: List<BalanceMap> = balances.map { t -> fun AcctBal.toBalanceMap() = BalanceMap(
                balType = "",
                value = ""
        )}
我想做这样的事情:
List<ProductDetail> details = acctBal.stream().filter(f -> f.getBalType() != null).map(e -> {
                String …