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)
Sign up to request clarification or add additional context in comments.

1 Comment

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.

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.