SCALA练习

scala> val lines = List("hello tom hello jerry","hello tom hello hello hello")
lines: List[String] = List(hello tom hello jerry, hello tom hello hello hello)

scala> lines.map(_.split(" "))
res0: List[Array[String]] = List(Array(hello, tom, hello, jerry), Array(hello, tom, hello, hello, hello))

scala> lines.map(_.split(" ")).flatten
res1: List[String] = List(hello, tom, hello, jerry, hello, tom, hello, hello, hello)

scala> lines.flatMap(_.split(" "))
res3: List[String] = List(hello, tom, hello, jerry, hello, tom, hello, hello, hello)

scala> lines.flatMap(_.split(" ")).map((_,1))
res4: List[(String, Int)] = List((hello,1), (tom,1), (hello,1), (jerry,1), (hello,1), (tom,1), (hello,1), (hello,1), (hello,1))

scala> lines.flatMap(_.split(" ")).map((_,1)).groupBy(_._1)
res5: scala.collection.immutable.Map[String,List[(String, Int)]] = Map(tom -> List((tom,1), (tom,1)), jerry -> List((jerry,1)), hello -> List((hello,1), (hello,1), (hello,1), (hello,1), (hello,1), (hello,1)))

scala> lines.flatMap(_.split(" ")).map((_,1)).groupBy(_._1).map(_._1)
res6: scala.collection.immutable.Iterable[String] = List(tom, jerry, hello)

scala> lines.flatMap(_.split(" ")).map((_,1)).groupBy(_._1).map(t=>(t._1,t._2.size))
res8: scala.collection.immutable.Map[String,Int] = Map(tom -> 2, jerry -> 1, hello -> 6)

scala> lines.flatMap(_.split(" ")).map((_,1)).groupBy(_._1).map(t=>(t._1,t._2.size)).toList
res15: List[(String, Int)] = List((tom,2), (jerry,1), (hello,6))

scala> lines.flatMap(_.split(" ")).map((_,1)).groupBy(_._1).map(t=>(t._1,t._2.size)).toList.sortBy(_._2)
res16: List[(String, Int)] = List((jerry,1), (tom,2), (hello,6))

scala> lines.flatMap(_.split(" ")).map((_,1)).groupBy(_._1).map(t=>(t._1,t._2.size)).toList.sortBy(_._2).reverse
res17: List[(String, Int)] = List((hello,6), (tom,2), (jerry,1))

scala> lines.flatMap(_.split(" ")).map((_,1)).groupBy(_._1).mapValues(_.size)
res18: scala.collection.immutable.Map[String,Int] = Map(tom -> 2, jerry -> 1, hello -> 6)

scala> lines.flatMap(_.split(" ")).map((_,1)).groupBy(_._1).mapValues(_.size).toList
res19: List[(String, Int)] = List((tom,2), (jerry,1), (hello,6))

scala> lines.flatMap(_.split(" ")).map((_,1)).groupBy(_._1).mapValues(_.size).toList.reverse
res20: List[(String, Int)] = List((hello,6), (jerry,1), (tom,2))

scala> val a= Array(1,2,3,4,5,6,7,8)
a: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)

scala> a.sum
res21: Int = 36

scala> a.reduce(_+_)
res23: Int = 36

scala> a.reduce(_-_)
res24: Int = -34

scala> a.par.reduce(_+_)
res25: Int = 36

scala> a.fold(10)(_+_)
res26: Int = 46

scala> a.par.fold(10)(_+_)
res27: Int = 76

scala> a.par.fold(0)(_+_)
res30: Int = 36

scala> a.par.foldLeft(0)(_+_)
res31: Int = 36

scala> a.par.foldRight(0)(_+_)
res32: Int = 36

scala> a.foldRight(0)(_+_)
res33: Int = 36

scala> a.foldLeft(0)(_+_)
res34: Int = 36

scala> lines.flatMap(_.split(" ")).map((_,1)).groupBy(_._1)
res36: scala.collection.immutable.Map[String,List[(String, Int)]] = Map(tom -> List((tom,1), (tom,1)), jerry -> List((jerry,1)), hello -> List((hello,1), (hello,1), (hello,1), (hello,1), (hello,1), (hello,1)))

scala> lines.flatMap(_.split(" ")).map((_,1)).groupBy(_._1).mapValues(_.foldLeft(0)(_+_._2))
res38: scala.collection.immutable.Map[String,Int] = Map(tom -> 2, jerry -> 1, hello -> 6)

scala> lines.flatMap(_.split(" ")).map((_,1)).groupBy(_._1).mapValues(_.foldLeft(0)(_+_._2)).toList
res39: List[(String, Int)] = List((tom,2), (jerry,1), (hello,6))

scala> lines.flatMap(_.split(" ")).map((_,1)).groupBy(_._1).mapValues(_.foldLeft(0)(_+_._2)).toList.reverse
res40: List[(String, Int)] = List((hello,6), (jerry,1), (tom,2))

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值