Scala学习笔记(4)—— scala 练习

1 练习

1.1 创建一个List

scala> val lst0 = List(1,7,9,8,0,3,5,4,6,2)
lst0: List[Int] = List(1, 7, 9, 8, 0, 3, 5, 4, 6, 2)

1.2 将lst0中每个元素乘以10后生成一个新的集合

scala> val lst1 = lst0.map(_ * 10)
lst1: List[Int] = List(10, 70, 90, 80, 0, 30, 50, 40, 60, 20)

1.3 将lst0中的偶数取出来生成一个新的集合

scala> val lst2 = lst0.filter(_%2==0)
lst2: List[Int] = List(8, 0, 4, 6, 2)

scala> val lst2 = lst0.filter(x => x%2==0)
lst2: List[Int] = List(8, 0, 4, 6, 2)

1.4 将lst0排序后生成一个新的集合

scala> lst0.sorted
res0: List[Int] = List(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

1.5 反转顺序

scala> lst0.sorted.reverse
res1: List[Int] = List(9, 8, 7, 6, 5, 4, 3, 2, 1, 0)

1.6 将lst0中的元素4个一组,类型为Iterator[List[Int]]

scala> lst0.grouped(4)
res4: Iterator[List[Int]] = non-empty iterator
scala> res4.map(_.size)
res5: Iterator[Int] = non-empty iterator
scala> res5.toList
res7: List[Int] = List(4, 4, 2)

1.7 将Iterator转换成List

scala> res12.toList
res13: List[List[Int]] = List(List(1, 7, 9, 8), List(0, 3, 5, 4), List(6, 2))

1.8 将多个list压扁成一个List

scala> res13.flatten
res14: List[Int] = List(1, 7, 9, 8, 0, 3, 5, 4, 6, 2)

1.9 wordcount

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

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

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

1.10 并行计算求和

scala> lst0
res15: List[Int] = List(1, 7, 9, 8, 0, 3, 5, 4, 6, 2)

scala> lst0.par
res16: scala.collection.parallel.immutable.ParSeq[Int] = ParVector(1, 7, 9, 8, 0, 3, 5, 4, 6, 2)

scala> lst0.par.reduce(_+_)
res17: Int = 45

1.11 折叠

scala> lst0.par.fold(0)(_+_)
res18: Int = 45

scala> lst0.par.fold(10)(_+_)
res19: Int = 135

1.12 聚合

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

scala> arr.aggregate(0)(_+_.sum,_+_)
res20: Int = 20

1.13 并集

scala>   val l1 = List(5,6,4,7)
l1: List[Int] = List(5, 6, 4, 7)

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

scala> l1.union(l2)
res21: List[Int] = List(5, 6, 4, 7, 1, 2, 3, 4)

scala> l1 union l2
res22: List[Int] = List(5, 6, 4, 7, 1, 2, 3, 4)

1.14 交集

scala> val r2 = l1.intersect(l2)
r2: List[Int] = List(4)

1.15 差集

scala> val r3 = l1.diff(l2)
r3: List[Int] = List(5, 6, 7)

2 mapforeach

scala> val arr = Array(1,2,3,4)
arr: Array[Int] = Array(1, 2, 3, 4)
scala> arr.map(println)
1
2
3
4
res9: Array[Unit] = Array((), (), (), ())
scala> arr.foreach(x => println(x))
1
2
3
4
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值