Scala的集合框架类比Java提供了更多的一些方便的api,使得使用scala编程时代码变得非常精简,尤其是在Spark中,很多功能都是由scala的这些api构成的,所以,了解这些方法的使用,将更加有助于我们学习Scala和Spark:
List,Map,Set的一些api的用法如下:
package Demo
/**
* Created by 汪本成 on 2016/7/6.
* 匹配元祖和列表
*/
object MatchTuples {
def processCoordinates(input : Any): Unit ={
input match{
case (a,b) => println("processing (%d, %d)...",a,b)
case "done" => println("done")
case _ => null
}
}
def main(args: Array[String]) {
//processCoordinates(49,-23)
collectionTest()
}
/**
* Scala 集合测试
*/
def collectionTest(): Unit ={
val map =Map("red"->"红色","green"->"绿色")
val colors = List("red", "green", "blue")
//map函数测试 ,打印长度
val size=colors.map( (f:String) => f.size);
val data=List(1,2,5,3);
//reduce函数测试 求和
val r=data.reduce((a:Int,b:Int)=>a+b);
println(size,r)
//打印取值
println(map("red"))
//判断是否存在
println(map.contains("white"))
//遍历map集合
for(pair<-map) println(pair)
//去重打印
println(List(3,2,3,