4

While in a project that is a mix of Scala and Java, I need to convert a Java Set into a Java List while in the Scala portion of the code.

What are some efficient ways of doing this? I could potentially use JavaConverters to convert Java Set -> Scala Set -> Scala List -> Java List. Are there other options that would be more efficient?

Thanks

1

2 Answers 2

4

The Java collection classes provide a constructor that takes a Collection, so why not just use that?

def js2jl[A](s: java.util.Set[A]): java.util.List[A] = new java.util.ArrayList(s)

Nothing Scala-specific beyond the syntax, but that's not a bad thing in this case.

Sign up to request clarification or add additional context in comments.

Comments

2

Why don't you do it just as you would in pure Java ? For example :

val mySet : java.util.Set[Integer] = new java.util.HashSet()
mySet.add(5)
val myList : java.util.List[Integer] = new java.util.ArrayList(mySet)
println(myList)

Is that what you want to do ?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.