Scala 将 Iterable 或 collection.Seq 转换为 collection.immutable.Seq

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16939611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 05:22:10  来源:igfitidea点击:

Scala convert Iterable or collection.Seq to collection.immutable.Seq

scalascala-collections

提问by Richard Todd

It appears the toSeqmethod in Scala collections returns a scala.collection.Seq, I could also return a Traversableor Iterablebut need to convert this to a scala.collection.immutable.Seq.

看来toSeqScala 集合中的方法返回 a scala.collection.Seq,我也可以返回 a TraversableorIterable但需要将其转换为 a scala.collection.immutable.Seq

Is there an easy way to do this?

是否有捷径可寻?

Thanks Richard

谢谢理查德

回答by axel22

Use the tomethod to convert between arbitrary collection types in Scala 2.10:

to在 Scala 2.10 中使用该方法在任意集合类型之间进行转换:

scala> Array(1, 2, 3).toSeq
res0: Seq[Int] = WrappedArray(1, 2, 3)

scala> Array(1, 2, 3).to[collection.immutable.Seq]
res1: scala.collection.immutable.Seq[Int] = Vector(1, 2, 3)