scala – Source.fromFile的效率
发布时间:2020-12-16 18:12:09 所属栏目:安全 来源:网络整理
导读:我想知道以下片段的效率是多少: val lst = Source.fromFile(f).getLines.toList 发出lst.contains(x)时, 这是否意味着f正在重新扫描,或者搜索是否依赖于新创建的列表中f的内存内容? 提前致谢. 解决方法 搜索依赖于内存中的内容.它只被加载一次toList被调用
我想知道以下片段的效率是多少:
val lst = Source.fromFile(f).getLines.toList 发出lst.contains(x)时, 这是否意味着f正在重新扫描,或者搜索是否依赖于新创建的列表中f的内存内容? 提前致谢. 解决方法
搜索依赖于内存中的内容.它只被加载一次toList被调用.
如何直接从source查看.Source.fromFile返回一个scala.io.BufferedSource. getLines返回BufferedLineIterator. 它在BufferedLineIterator中,读取文件的内容. override def hasNext = { if (nextLine == null) nextLine = lineReader.readLine nextLine != null } override def next(): String = { val result = { if (nextLine == null) lineReader.readLine else try nextLine finally nextLine = null } if (result == null) Iterator.empty.next else result } } 调用toList使用上面的next和hasNext来派生列表.所以我已经包含了File的所有元素. 执行lst.contains(x)会像其他任何列表一样遍历列表. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |