Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.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
如何将迭代器[int]转换为在scala中具有每格频率的映射_Scala_Scala Collections - Fatal编程技术网

如何将迭代器[int]转换为在scala中具有每格频率的映射

如何将迭代器[int]转换为在scala中具有每格频率的映射,scala,scala-collections,Scala,Scala Collections,我刚刚学习了如何在scala中将整数列表转换为每格频率的映射 然而,我正在处理一个22 GB的文件,因此我正在通过该文件进行流式处理 Source.fromFile("test.txt").getLines.filter(x => x.charAt(0) != '#').map(x => x.split("\t")(1)).map(x => x.toInt) groupby函数仅对列表有效,而对迭代器无效。我猜是因为它需要内存中的所有值。由于文件大小,我无法将迭代器转换为列

我刚刚学习了如何在scala中将整数列表转换为每格频率的映射

然而,我正在处理一个22 GB的文件,因此我正在通过该文件进行流式处理

Source.fromFile("test.txt").getLines.filter(x => x.charAt(0) != '#').map(x => x.split("\t")(1)).map(x => x.toInt)
groupby函数仅对列表有效,而对迭代器无效。我猜是因为它需要内存中的所有值。由于文件大小,我无法将迭代器转换为列表

例如

List(1,2,3,101,330,302).iterator
我怎样才能从那里到那里

res1: scala.collection.immutable.Map[Int,Int] = Map(100 -> 1, 300 -> 2, 0 -> 3)
您可以使用折叠:

val iter = List(1,2,3,101,330,302).iterator

iter.foldLeft(Map[Int, Int]()) {(accum, a) => 
                                  val key = a/100 * 100;
                                  accum + (key  -> (accum.getOrElse(key, 0) + 1))}

// scala.collection.immutable.Map[Int,Int] = Map(0 -> 3, 100 -> 1, 300 - 2)