llguanli
scala> val m = List(List("a","b"),List("c","d"))
m: List[List[String]] = List(List(a, b), List(c, d))


scala> m.flatten
res8: List[String] = List(a, b, c, d)

scala> val n = List(List(1,2),List(3,4))
n: List[List[Int]] = List(List(1, 2), List(3, 4))

scala> n.flatMap(x=>x.map(_*2))
res9: List[Int] = List(2, 4, 6, 8)

由以上代码能够看出,flatMap是map和flatten操作的结合,先进行map操作,然后再进行flatten操作。flatMap还能够去除掉空元素NONE。








分类:

技术点:

相关文章:

  • 2021-11-19
  • 2021-11-19
  • 2021-11-19
  • 2021-07-17
  • 2021-11-19
  • 2021-11-23
  • 2022-12-23
  • 2021-11-19
猜你喜欢
  • 2022-01-09
  • 2021-09-18
  • 2021-11-19
  • 2021-12-25
  • 2021-11-19
  • 2021-05-26
相关资源
相似解决方案