scala基础8-map/tuple/zip进阶

本文深入探讨了Scala中各种集合的使用方法,包括不可变与可变Map的操作、Tuple的使用技巧、字符串分区处理以及Array的Zip操作等。通过具体实例展示了如何进行遍历、增删元素、排序及组合等多种实用操作。
object InAction {
	//不可变数组
	val map = Map("book" -> 10, "gun" -> 18, "ipad" -> 100)
                                                  //> map  : scala.collection.immutable.Map[String,Int] = Map(book -> 10, gun -> 18
                                                  //| , ipad -> 100)
  //遍历,生产新的map,注意value的类型变化
	for((k,v) <- map) yield (k, v * 0.9)      //> res0: scala.collection.immutable.Map[String,Double] = Map(book -> 9.0, gun -
                                                  //| > 16.2, ipad -> 90.0)
  //创建可变map
  val scores = scala.collection.mutable.Map("scala" -> 1, "hadoop" -> 2, "spark" -> 3)
                                                  //> scores  : scala.collection.mutable.Map[String,Int] = Map(spark -> 3, hadoop 
                                                  //| -> 2, scala -> 1)
  //getOrElse:获取不到则取第二个参数的值
	val hadoopScore = scores.getOrElse("hadoop1", 0)
                                                  //> hadoopScore  : Int = 0
  //map的增删
  scores += ("java" -> 4)                         //> res1: InAction.scores.type = Map(spark -> 3, hadoop -> 2, scala -> 1, java -
                                                  //| > 4)
  scores -= "hadoop"                              //> res2: InAction.scores.type = Map(spark -> 3, scala -> 1, java -> 4)
  
  //排序
  val sortedScore = scala.collection.immutable.SortedMap("scala" -> 1, "hadoop" -> 2, "spark" -> 3)
                                                  //> sortedScore  : scala.collection.immutable.SortedMap[String,Int] = Map(hadoop
                                                  //|  -> 2, scala -> 1, spark -> 3)
  
  //tuple:类型可以不同,下标从1开始
  val tuple = (1,2,3,"hello", "world")            //> tuple  : (Int, Int, Int, String, String) = (1,2,3,hello,world)
  val third = tuple._3                            //> third  : Int = 3
  
  //tuple的接收,可使用占位符
  val (first, second, thirda, fourth, fifth) = tuple
                                                  //> first  : Int = 1
                                                  //| second  : Int = 2
                                                  //| thirda  : Int = 3
                                                  //| fourth  : String = hello
                                                  //| fifth  : String = world
  val (f, s, t, _, _) = tuple                     //> f  : Int = 1
                                                  //| s  : Int = 2
                                                  //| t  : Int = 3
  //按大写小分区,partition参数是个布尔表达式
  "Hello World".partition(_.isUpper)              //> res3: (String, String) = (HW,ello orld)
  
  //zip拉链操作,返回一个map
  val symbols = Array("[", "-", "]")              //> symbols  : Array[String] = Array([, -, ])
  val counts = Array(1, 2, 3)                     //> counts  : Array[Int] = Array(1, 2, 3)
  val pairs = symbols.zip(counts)                 //> pairs  : Array[(String, Int)] = Array(([,1), (-,2), (],3))
  for ((x, y) <- pairs) Console.print(x * y)
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值