我正在寻找一个将列表划分为固定大小的子列表的功能,这正是Google Collections库中的Lists.partition所做的.我在Scala Collections API中找不到这样的方法.我错过了什么吗?
Dav*_*ith 15
您正在寻找的方法是"分组".与分区函数的细微差别在于它返回列表的迭代器而不是列表列表.这可能没问题,或者您可能需要使用Iterator.toList函数进行转换
val list = List(1, 2, 3, 4, 5)
println(list.grouped(2).toList) //prints List(List(1, 2), List(3, 4), List(5))
Run Code Online (Sandbox Code Playgroud)