feat:ChatGPT第 8 课:flatMap 和嵌套集合。
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
package practise8.demo
|
||||
|
||||
/*
|
||||
* split + flatMap 拆分字符串*/
|
||||
object Demo5SplitFlatMap {
|
||||
def main(args: Array[String]): Unit = {
|
||||
val sentences = List(
|
||||
"hello scala",
|
||||
"hello spark"
|
||||
)
|
||||
|
||||
// 使用map
|
||||
val result = sentences.map(x => x.split(" ").toList)
|
||||
println(result)
|
||||
|
||||
// 使用flatMap
|
||||
val result2 = sentences.flatMap(x => x.split(" "))
|
||||
println(result2)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user