一、构造映射
val scores = Map("Jim"->10, ("Tom",20), "Sam"->44) //key->value, (key,value)两种方式表示, 不可变映射, val s = scala.collection.mutable.Map("Jim"->10, ("Tom",20), "Sam"->44)//可变映射 val s1 = new scala.collection.mutable.HashMap[String, Int] //空的Map //scores.+=(("Lily"->10)) //不可变映射,map中不能新增元素 s.+=(("Lily"->10)) s1.+=(("Jim"->10)) println(scores) println(s) println(s1)
结果:
Map(Jim -> 10, Tom -> 20, Sam -> 44) Map(Jim -> 10, Tom -> 20, Lily -> 10, Sam -> 44) Map(Jim -> 10)
对偶
"A"->1
("A",1)
二、获取映射中的值
val scores = Map("Jim"->10, ("Tom",20), "Sam"->44) val score1 = scores("Lily") //直接通过()获取里面的值 val score2 = if(scores.contains("Lily")) scores("Lily") else 0 //判断是否存在,然后再取值 val score3 = scores.getOrElse("Lily",0) //通过getOrElse方法实现,不存在给默认值 println(score1)//没有值,会报异常 println(score2) println(score3)
三、更新映射中的值
可变映射
val scores = scala.collection.mutable.Map("Jim"->10, ("Tom",20), "Sam"->44) scores("Jim") = 11 //更新值 scores("Fred") = 33 //新增值 scores += ("Lily"->30, "Li"-> 27) //增加多个 scores.+=(("Zhao"->44)) scores -= "Jim" //删除元素 println(scores)
结果
Map(Li -> 27, Zhao -> 44, Fred -> 33, Tom -> 20, Lily -> 30, Sam -> 44)
不可变映射 通过声明var变量来重新赋值,完成元素的增、删
var scores = Map("Jim"->10, ("Tom",20), "Sam"->44) scores = scores +("Lily"-> 23) scores -= "Jim" println(scores)
结果
Map(Tom -> 20, Sam -> 44, Lily -> 23)
四、迭代映射
var scores = Map("Jim"->10, ("Tom",20), "Sam"->44) for((k, v) <- scores) println (k+","+v) for(i<- scores.keySet) print(i+" ") println() for(i<- scores.values) print(i+" ")
结果
Jim,10 Tom,20 Sam,44 Jim Tom Sam 10 20 44
五、已排序映射
val sortedScores = scala.collection.immutable.SortedMap("Jim"->10, ("Tom",20), "Sam"->44)
println(sortedScores)
结果
Map(Jim -> 10, Sam -> 44, Tom -> 20)
六、与Java的互操作
七、元组
val tuple = (1, 3.14, "Hello") println(tuple._1) //访问第一个元组,下标值不是0而是1 val(first, second, _) = tuple //获取元组的元素 println(second)
结果
1 3.14
八、拉链操作
把元组的多个值绑定在一起
val symbols = Array("<","-",">") val counts = Array(2,10,2) val pairs = symbols.zip(counts) for((k,v)<- pairs) print(k * v)
结果
<<---------->>
keys.zip(values).toMap
通过拉链组合成映射
1.
val m = Map(("a",10),("b",18),("d",17))
m.mapValues(_*0.9).foreach(println)
2.
val m = new scala.collection.mutable.HashMap[String,Int]() val in = new java.util.Scanner(new java.io.File("file/wordcount.txt")) while(in.hasNext){ val key = in.next() m(key)=if (m.contains(key)) m(key)+1 else 1 }
3.
var word = Map[String,Int]() val in = new java.util.Scanner(new java.io.File("file/wordcount.txt")) while(in.hasNext){ val key = in.next() word = word + ((key, if (word.contains(key)) word(key)+1 else 1)) }
4.
var sortedWords = scala.collection.immutable.SortedMap[String,Int]() val in = new java.util.Scanner(new java.io.File("file/wordcount.txt")) while(in.hasNext){ val key = in.next() sortedWords = sortedWords + ((key, if (sortedWords.contains(key)) sortedWords(key)+1 else 1)) }
5.
import scala.collection.JavaConversions.mapAsScalaMap var words:scala.collection.mutable.Map[String,Int] = new java.util.TreeMap[String,Int]() val in = new java.util.Scanner(new java.io.File("file/wordcount.txt")) while(in.hasNext){ val key = in.next() words(key)=if(words.contains(key)) words(key)+1 else 1 }
6.
val week = new mutable.LinkedHashMap[String,Int]() week("Monday")=java.util.Calendar.MONDAY week("Tuesday")=java.util.Calendar.TUESDAY week("Wednesday")=java.util.Calendar.WEDNESDAY for((k,v)<- week){ println(k,v) }
7.
import scala.collection.JavaConversions.propertiesAsScalaMap val props:scala.collection.Map[String,String]=System.getProperties() val len = props.keys.maxBy(_.length).length() val format = "%-"+len+"s|%s\n" for((k,v) <- props){ printf(s"${format}",k,v) }
8. 匿名函数实现
val minmax = (values:Array[Int])=>{
(values.max,values.min)
}
9.匿名函数实现
val lteqgt = (values:Array[Int], v:Int)=>{ var lt=0 var eq=0 var gt=0 for(i<-values) if (i>v) gt+=1 else if(i==v) eq+=1 else lt+=1 (lt,eq,gt) }
10.
Vector((H,W), (e,o), (l,r), (l,l), (o,d))
参考《快学Scala》