将 String.split() 结果转换为 Scala 列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20083389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 05:52:15 来源:igfitidea点击:
Convert String.split() result to Scala list
提问by javadba
What is the means to convert the following java String[] to a Scala List?
将以下 java String[] 转换为 Scala List 的方法是什么?
val trimmedList : List[String] = str.split("\n")).map (_.trim) // Missing some code here, does not compile
回答by dhg
For simplicity, use toList:
为简单起见,请使用toList:
val trimmedList: List[String] = str.split("\n").map(_.trim).toList
For complexity, use breakOut(which avoids creating an intermediate collection from map):
对于复杂性,请使用breakOut(避免从 中创建中间集合map):
import collection.breakOut
val trimmedList: List[String] = str.split("\n").map(_.trim)(breakOut)

