视频下载地址:http://yunpan.cn/cmxUWhNukuXaM 访问密码 6790
大数据微信公众账号:DT_Spark
Map操作,
//> 标记是因为在scala worksheet上即时显示执行结果的标识符,类似shell命令行
//定义一个可变的map
val map = scala.collection.mutable.Map("book"->10,"Run"->12,"IPad"->111)
//循环map,把其值赋给k,v ,然后再把v*0.9
for((k,v)<-map) yield (k,v * 0.9)
//结果
res24: scala.collection.mutable.Map[String,Double] = Map(book -> 9.0, Run -> 10.8, IPad -> 99.9)
//getOrElse
val hadoopScore = map.getOrElse("book", "is null")
//> hadoopScore : Any = 10
val hadoopScore = map.getOrElse("books", "is null")
//> hadoopScore : Any = is null
//给map增加一个k,v
map += ("R"->90)
//> res25: com.wy.scala.ScalaInAction.map.type = Map(book -> 10, R -> 90, Run -> 12, IPad -> 111)
//给map去除一个k,v
map -= "Run"
//> res26: com.wy.scala.ScalaInAction.map.type = Map(book -> 10, R -> 90, IPad -> 111)
//map 排序
val sortedScore = scala.collection.immutable.SortedMap("Spark"->11,"Hadoop"->12,"Scala"->14)
//结果,可以看到他们是按map的key的字典排序的
//> sortedScore : scala.collection.immutable.SortedMap[String,Int] = Map(Hadoop -> 12, Scala -> 14, Spark -> 11)
Tuple,
//定义一个tuple
val tuple =(1,2,3.14,"Rocky","Spark")
//> tuple : (Int, Int, Double, String, String) = (1,2,3.14,Rocky,Spark)
//取出tuple的第三个数据
val tv = tuple._3
//> tv : Double = 3.14
//把tuple赋值给--,他们会自动对应,并类型推导
val (first,second,third,four,five) = tuple
//> first : Int = 1
second : Int = 2
third : Double = 3.14
four : String = Rocky
five : String = Spark
//占位符,但是只取了前俩个
val (f,ss,_,_,_) = tuple
//> f : Int = 1
ss : Int = 2
//错误
val (ff,sss) = tuple
//partition分组,大写的归为一组
"Rock Spark".partition(_.isUpper)
//> res27: (String, String) = (RS,ock park)
Zip,
//zip操作
val zip1 = Array("[","-","]")
//> zip1 : Array[String] = Array([, -, ])
val zip2 = Array(2,5,2)
//> zip2 : Array[Int] = Array(2, 5, 2)
val zip3 = zip1.zip(zip2)
//> zip3 : Array[(String, Int)] = Array(([,2), (-,5), (],2))
//打印2次【,打印5次-,打印2次】
for ((x,y) <- zip3) Console.print(x*y)
//?在worksheet里,这里并没有打印出结果[[-----]],重新在一个Object里写了下代码,run了一下,控制台就能打印“[[-----]]”,郁闷... 难道在worksheet里的打印到控制台操作不能响应?
so easy …