有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将计数器引入scala中的循环

我正在写一个小程序,将一个非常大的文件转换成多个较小的文件,每个文件将包含100行

我正在迭代一行迭代:

  while (lines.hasNext) {
      val line = lines.next()
  }

我想引入一个计数器,当它达到某个值时,重置计数器并继续。在java中,我将执行以下操作:

int counter = 0;
      while (lines.hasNext) {
          val line = lines.next()
if(counter == 100){
 counter = 0;
}
++counter
      }

scala中是否有类似的方法或替代方法


共 (4) 个答案

  1. # 1 楼答案

    如果重置计数器表示原始列表中存在重复的数据组,则可能需要使用grouped方法:

    scala> val l = List("one", "two", "three", "four")
    l: List[java.lang.String] = List(one, two, three, four)
    
    scala> l.grouped(2).toList
    res0: List[List[java.lang.String]] = List(List(one, two), List(three, four))
    

    更新:既然你是从一个文件中读取,你应该能够非常有效地迭代该文件:

    val bigFile = io.Source.fromFile("/tmp/verybigfile")
    val groupedLines = bigFile.getLines.grouped(2).zipWithIndex
    groupedLines.foreach(group => {
      val (lines, index) = group
      val p = new java.io.PrintWriter("/tmp/" + index)
      lines.foreach(p.println)
      p.close()
    })
    

    当然,这也可以作为一种理解的方式来写

    在将每组行写入自己的文件之前,您甚至可以通过将groupedLines转换为带有.par的并行集合来获得更好的性能

  2. # 2 楼答案

    可以使用zipWithIndex和一些转换

    scala> List(10, 20, 30, 40, 50).zipWithIndex.map(p => (p._1, p._2 % 3))
    res0: List[(Int, Int)] = List((10,0), (20,1), (30,2), (40,0), (50,1))
    
  3. # 3 楼答案

    在scala中,通常使用.zipWithIndex

    scala> List("foo","bar")
    res0: List[java.lang.String] = List(foo, bar)
    
    scala> for((x,i) <- res0.zipWithIndex) println(i + " : " +x)
    0 : foo
    1 : bar
    

    (这也适用于您的行,只要它们在迭代器中,例如hashasNextnext()方法,或其他一些scala集合)

    但如果您需要一个复杂的逻辑,比如重置计数器,您可以用与java相同的方式编写它:

    var counter = 0
    while (lines.hasNext) {
      val line = lines.next()
      if(counter % 100 == 0) {
        // now write to another file
      }
    }
    

    也许你可以告诉我们为什么要重置计数器,所以我们可以说如何做得更好

    编辑 根据您的更新,最好使用分组方法,如@pr1001所建议的:

    lines.grouped(100).foreach(l => l.foreach(/* write line to file*/))
    
  4. # 4 楼答案

    这将奏效:

    lines grouped 100 flatMap (_.zipWithIndex) foreach {
      case (line, count) => //whatever
    }