flatMap=map + flatten
例1:
scala> val test=List("hello java","hello python","hello hadoop")
test: List[String] = List(hello java, hello python, hello hadoop)
map输出结果:
scala> test.map(line=>line).foreach(x=>println(x))
hello java
hello python
hello hadoop
flatMap输出结果1:
scala> test.flatMap(line=>line.split(" ")).foreach(x=>println(x))
hello
java
hello
python
hello
hadoop
flatMap输出结果2:
scala> text.flatMap(line=>line).foreach(x=>println(x))
h
e
l
l
o
j
a
v
a
h
……
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。