Scala tuple与map

阅读更多

一 tuple

  1. scala>(1,2)
  2. res27:(Int,Int)=(1,2)
  3. scala>(1->2)
  4. res28:(Int,Int)=(1,2)
  5. scala>(1,"Alice","Math",97.5)
  6. res30:(Int,String,String,Double)=(1,Alice,Math,97.5)
  7. scala> val t =(1,"Alice","Math",97.5)
  8. t:(Int,String,String,Double)=(1,Alice,Math,97.5)
  9. scala> t._1
  10. res31:Int=1
  11. scala> t._2
  12. res32:String=Alice
  13. scala> t._3
  14. res33:String=Math
  15. scala> t._4
  16. res34:Double=97.5
  17. scala> l
  18. res35:List[Int]=List(1,2,3)
  19. scala>def sumSq(in:List[Int]):(Int,Int,Int)=
  20. |in.foldLeft((0,0,0))((t,v)=>(t._1+1,t._2+v,t._3+v*v))
  21. sumSq:(in:List[Int])(Int,Int,Int)
  22. scala> sumSq(l)
  23. res36:(Int,Int,Int)=(3,6,14)
二 Map[K,V]
  1. scala> val p =Map(1->"Davie",2->"Elwood")
  2. p: scala.collection.immutable.Map[Int,String]=Map(1->Davie,2->Elwood)
  3. scala> p(1)
  4. res37:String=Davie
  5. scala> p(2)
  6. res38:String=Elwood
  7. scala> p.contains(1)
  8. res39:Boolean= true
  9. scala> p.contains(3)
  10. res40:Boolean= false
  11. scala> p.keys
  12. res41:Iterable[Int]=Set(1,2)
  13. scala> p.values
  14. res42:Iterable[String]=MapLike.DefaultValuesI

你可能感兴趣的:(Scala,tuple,map)