将scala字符串集转换为一组长字符串

时间:2017-06-08 16:57:24

标签: scala

如何转换Scala字符串集

val s = Set("1","2","3")

到一组长?

Set(1, 2, 3)

谢谢!

2 个答案:

答案 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)