[英]How to convert a SortedSet in Java to a Seq in Scala
The Jedis call I'm using returns a Set, although at runtime it is actually a LinkedHashSet.我使用的 Jedis 调用返回一个 Set,尽管在运行时它实际上是一个 LinkedHashSet。 I want to pull it into Scala, deserialize the elements, and return a Seq.
我想将它拉入 Scala,反序列化元素,并返回一个 Seq。
Easy!简单的!
import collection.JavaConverters._
val theJavaSet = methodReturningLinkedHashSet()
theJavaSet.asScala.toSeq
I'd also tend to avoid JavaConversions
(unless restricted by an older version of Scala).我也倾向于避免
JavaConversions
(除非受旧版本的 Scala 限制)。 JavaConverters
offers more control, and is immune from a couple of problems that can occur in more complicated scenarios. JavaConverters
提供了更多的控制,并且不受更复杂场景中可能出现的一些问题的影响。
Like Kevin says but without the typo, on 2.8.1 or later:就像凯文说的,但没有错字,在 2.8.1 或更高版本:
val javaSet: java.util.Set[String] = new java.util.LinkedHashSet[String]()
javaSet.add("a")
javaSet.add("b")
import collection.JavaConverters._
javaSet.asScala.toSeq
// res2: Seq[String] = ArrayBuffer(a, b)
or (also works on 2.8.0):或(也适用于 2.8.0):
import collection.JavaConversions._
javaSet.toSeq
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.