为了账号安全,请及时绑定邮箱和手机立即绑定

scala的多种集合的使用(3)之遍历集合的方法

标签:
大数据

遍历集合的方法

1.用foreach循环遍历一个集合

foreach接收一个函数作为参数。定义的函数应该接收一个元素作为输入参数,然后不要返回任何的东西。输入的参数的类型应该匹配集合中的类型。随着foreach的执行,它每次都会把一个元素传给你的函数,直到集合中最后一个元素。

foreach常用的就是输出信息:

?


scala> val x = Vector(1,2,3)x: scala.collection.immutable.Vector[Int] = Vector(1, 2, 3) scala> x.foreach((i:Int) => println(i))123

对于表达式,还有其他写法:

?


x.foreach(i => println(i)) x.foreach(println(_)) x.foreach(println) 

对于最后一种表达式,情况是一个只有一条语句组成并且接受一个参数的函数可以简写成这种形式。

2.用for循环遍历一个集合

 可以用for循环遍历任意一种Traversable的类型。

?


scala> val fruits = Traversable("apple","orange","banana")fruits: Traversable[String] = List(apple, orange, banana) scala> for(i <- fruits) println(i)appleorangebanana

如果算法很长,可以在for循环的代码块里执行。

下面展示了for循环里使用计数器的几种方式:

?


scala> val fruits = Array("apple","banana","orange")fruits: Array[String] = Array(apple, banana, orange) scala> for(i <- 0 until fruits.size) println(s"$i is ${fruits(i)}")0 is apple1 is banana2 is orange

?


scala> val fruits = Array("apple","banana","orange")fruits: Array[String] = Array(apple, banana, orange) scala> for((elem,count) <- fruits.zipWithIndex){     | println(s"$count is $elem")     | }0 is apple1 is banana2 is orange

?


scala> val fruits = Array("apple","banana","orange")fruits: Array[String] = Array(apple, banana, orange) scala> for((elem,count) <- fruits.zip(Stream from 1)){     | println(s"$count is $elem")     | }1 is apple2 is banana3 is orange

3.用zipWithIndex创建循环计数器

 (1)以foreach的形式遍历集合

?


scala> val days = Array("Sunday","Monday","Tusday","Wednsday","Thursday","Friday","Saturday")days: Array[String] = Array(Sunday, Monday, Tusday, Wednsday, Thursday, Friday,Saturday) scala> days.zipWithIndex.foreach{     | case(day,count) => println(s"${count+1} is $day")     | }1 is Sunday2 is Monday3 is Tusday4 is Wednsday5 is Thursday6 is Friday7 is Saturday

 另外的一种表示方法:

?


scala> val days = Array("Sunday","Monday","Tusday","Wednsday","Thursday","Friday","Saturday")days: Array[String] = Array(Sunday, Monday, Tusday, Wednsday, Thursday, Friday,Saturday) scala> days.zipWithIndex.foreach{ d=>     | println(s"${d._2+1} is ${d._1}")     | }1 is Sunday2 is Monday3 is Tusday4 is Wednsday5 is Thursday6 is Friday7 is Saturday

(2) 以for的形式循环遍历

?


scala> val days = Array("Sunday","Monday","Tusday","Wednsday","Thursday","Friday","Saturday")days: Array[String] = Array(Sunday, Monday, Tusday, Wednsday, Thursday, Friday,Saturday) scala> for ((day,count) <- days.zipWithIndex){     | println(s"${count+1} is $day")     | }1 is Sunday2 is Monday3 is Tusday4 is Wednsday5 is Thursday6 is Friday7 is Saturday

4.用zip创建循环计数器

 在zip中使用Stream是一种生存计数器的方法。

?


scala> val days = Array("Sunday","Monday","Tusday","Wednsday","Thursday","Friday","Saturday")days: Array[String] = Array(Sunday, Monday, Tusday, Wednsday, Thursday, Friday,Saturday) scala> for ((day,count) <- days.zip(Stream from 1)){     | println(s"$count is $day")     | }1 is Sunday2 is Monday3 is Tusday4 is Wednsday5 is Thursday6 is Friday7 is Saturday

5.range循环计数器

 如果只是需要重复做某事多次,可以用range。

?


scala> val fruits = Array("apple","banana","orange")fruits: Array[String] = Array(apple, banana, orange) scala> for(i <- 0 until fruits.size) {     | println(s"$i is ${fruits(i)}")}0 is apple1 is banana2 is orange

6.用reduce方法遍历集合

使用reduceLeft和reduceRight方法来遍历序列中的元素,把相邻的元素传给你的函数成成一个新的结果,之后和序列的下一个元素比较在生成新的结果。

reduceLeft方法是从左到右遍历一个序列,在算法中首先会对前两个元素进行比较,然后返回一个结果。该结果会与第三个元素进行比较,比较之后在产生一个新结果,接着在于第四个元素比较,以此类推。

?


scala> val a = Array(12,3,4,5,67)a: Array[Int] = Array(12, 3, 4, 5, 67) scala> a.reduceLeft(_ + _)res4: Int = 91 scala> a.reduceLeft(_ min _)res5: Int = 3 scala> a.reduceLeft(_ max _)res6: Int = 67

这里面的两个下划线,它们代表传给函数的两个参数。

reduceRight与reduceLeft同理的。

7.用fold方法遍历集合

 foldLeft方法如同reduceLeft,它会设置一个种子值用于第一个元素。foldLeft接收两个参数列表。第一个列表有一个字段,种子值。第二个列表是要运行的代码块。

?


scala> val a = Array(12,3,4,5,67)a: Array[Int] = Array(12, 3, 4, 5, 67) scala> a.reduceLeft(_ + _)res8: Int = 91 scala> a.foldLeft(20)(_ + _)res9: Int = 111

原文出处:https://www.cnblogs.com/zhaohadoopone/p/9525317.html

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消