user3071643
user3071643

Reputation: 1543

Convert a scala Set of Strings to a Set of Long

how can I convert a Scala set of strings

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

to a set of long?

Set(1, 2, 3)

Thanks!

Upvotes: 0

Views: 495

Answers (2)

Leo C
Leo C

Reputation: 22449

Adding long value check:

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)

Upvotes: 2

Jeffrey Chung
Jeffrey Chung

Reputation: 19527

val s = Set("1","2","3")
val longs = s.map(_.toLong)

Upvotes: 4

Related Questions