Scala Source.fromFile的效率

Scala Source.fromFile的效率,scala,Scala,我想知道以下代码片段的效率是什么: val lst = Source.fromFile(f).getLines.toList 当发出lst.contains(x)时 这是指正在重新扫描f,还是搜索依赖于新创建列表中f的内存内容 提前感谢。搜索依赖内存中的内容。并且只在调用toList时加载一次 如何更好地直接从屏幕上看到Source.fromFile返回一个scala.io.BufferedSource获取行返回一个 它位于BufferedLineIterator中,读取文件的内容 overr

我想知道以下代码片段的效率是什么:

val lst = Source.fromFile(f).getLines.toList
当发出
lst.contains(x)

这是指正在重新扫描
f
,还是搜索依赖于新创建列表中
f
的内存内容


提前感谢。

搜索依赖内存中的内容。并且只在调用
toList
时加载一次

如何更好地直接从屏幕上看到
Source.fromFile
返回一个
scala.io.BufferedSource
<代码>获取行返回一个

它位于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
导出列表。因此,
lst
已经包含文件的所有元素


Doing
lst.contains(x)
会像任何其他列表一样迭代该列表。

一旦使用toList,它会将不可变列表返回给您进行操作。您的文件将不会因您在列表中执行的操作而重新扫描