简体   繁体   中英

How do I convert java.util.HashSet[java.lang.Long] to Scala Set[Long]

I'm using:

import scala.collection.JavaConverters._
import scala.collection.Set

This works:

val a: java.util.HashSet[java.lang.Long] =  javaFunction(...)
val b: Set[java.lang.Long] = a.asScala.toSet

but what I want is a Set[Long] , not a Set[java.lang.Long] . When I try this:

val a: java.util.HashSet[java.lang.Long] =  javaFunction(...)
val b: Set[Long] = a.asScala.toSet

I get: Expression of type scala.collection.immutable.Set[java.lang.Long] doesn't conform to expected type scala.collection.Set[Long] .

Why is this and how do I get a Set[Long] ?

Simply:

import scala.collection.JavaConverters._
val b: Set[Long] = a.asScala.map(Long2long).toSet

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM