如何转换Scala字符串集
val s = Set("1","2","3")
到一组长?
Set(1, 2, 3)
谢谢!
答案 0 :(得分:4)
val s = Set("1","2","3")
val longs = s.map(_.toLong)
答案 1 :(得分:2)
添加long
值检查:
import scala.util.{Try, Success, Failure}
val s = Set("1", "2", "3", "x")
s.map( x => Try(x.toLong) match {
case Success(e) => e
case Failure(_) => -999L
} )
res1: scala.collection.immutable.Set[Long] = Set(1, 2, 3, -999)