8

What is the means to convert the following java String[] to a Scala List?

val trimmedList : List[String] = str.split("\\n")).map (_.trim)    // Missing some code here, does not compile

1 Answer 1

22

For simplicity, use toList:

val trimmedList: List[String] = str.split("\\n").map(_.trim).toList

For complexity, use breakOut (which avoids creating an intermediate collection from map):

import collection.breakOut
val trimmedList: List[String] = str.split("\\n").map(_.trim)(breakOut)
1
  • thx. The actual problem is that I had two right parentheses after the split (take a look..) . I have not become accustomed to IJ's parser error reporting and tend to misinterpret. The "toList" was trivial at that point. Commented Nov 19, 2013 at 23:25

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.