开发者

Converting immutable to mutable collections

开发者 https://www.devze.com 2023-01-05 13:21 出处:网络
What is the best way to convert collection.immutab开发者_如何学JAVAle.Set to collection.mutable.Set?scala> var a=collection.mutable.Set[Int](1,2,3)

What is the best way to convert collection.immutab开发者_如何学JAVAle.Set to collection.mutable.Set?


scala> var a=collection.mutable.Set[Int](1,2,3)                              
a: scala.collection.mutable.Set[Int] = Set(1, 2, 3)

scala> var b=collection.immutable.Set[Int](1,2,3)
b: scala.collection.immutable.Set[Int] = Set(1, 2, 3)

scala> collection.mutable.Set(b.toArray:_*)      
res0: scala.collection.mutable.Set[Int] = Set(1, 2, 3)

scala> collection.mutable.Set(b.toSeq:_*)  
res1: scala.collection.mutable.Set[Int] = Set(1, 2, 3)

scala> collection.mutable.Set(b.toList:_*)
res2: scala.collection.mutable.Set[Int] = Set(1, 2, 3)


Starting Scala 2.13, via factory builders applied with .to(factory):

Set(1, 2, 3).to(collection.mutable.Set)
// collection.mutable.Set[Int] = HashSet(1, 2, 3)

Prior to Scala 2.13 and starting Scala 2.10:

Set(1, 2, 3).to[collection.mutable.Set]
// collection.mutable.Set[Int] = Set(1, 2, 3)
0

精彩评论

暂无评论...
验证码 换一张
取 消