映射
scala> val map1 = Map ( "scala" -> 1,"java" ->2,"python" -> 3)
map1: scala.collection.immutable.Map[String,Int] = Map(scala -> 1, java -> 2, python -> 3)
scala> val map2 = Map(("scala",1),("java",2),("python",3))
map2: scala.collection.immutable.Map[String,Int] = Map(scala -> 1, java -> 2, python -> 3)
scala> map1("scala")
res18: Int = 1
scala> import scala.collection.mutable._
import scala.collection.mutable._
scala> val map1 = Map("scala" -> 1,"java" -> 2,"python" ->3)
map1: scala.collection.mutable.Map[String,Int] = Map(scala -> 1, java -> 2, python -> 3)
scala> map1("scala") = 6
scala> map1("scala")
res20: Int = 6
scala> map1.getOrElse("c#",-1)
res21: Int = -1
元组
scala> ("scala",1)
res22: (String, Int) = (scala,1)
scala> val t = ( "scala",100L,3.14,("spark",1))
t: (String, Long, Double, (String, Int)) = (scala,100,3.14,(spark,1))
scala> t._1
res23: String = scala
scala> t._3
res24: Double = 3.14
scala> t._4
res25: (String, Int) = (spark,1)
scala> t._4._1
res26: String = spark
scala> val t,(a,b,c,d) = ("scala",100L,3.14,("spark",1))
t: (String, Long, Double, (String, Int)) = (scala,100,3.14,(spark,1))
a: String = scala
b: Long = 100
c: Double = 3.14
d: (String, Int) = (spark,1)
scala> val arr = Array(("lulu",21),("ningning",25),("xiaoyueyue",29))
arr: Array[(String, Int)] = Array((lulu,21), (ningning,25), (xiaoyueyue,29))
scala> arr.toMap
res27: scala.collection.immutable.Map[String,Int] = Map(lulu -> 21, ningning -> 25, xiaoyueyue -> 29)
scala> val arr1 = Array("lulu","ningning","xiaoyueyue")
arr1: Array[String] = Array(lulu, ningning, xiaoyueyue)
scala> val arr2 = Array(24,25,26,27)
arr2: Array[Int] = Array(24, 25, 26, 27)
scala> arr1 zip arr2
res28: Array[(String, Int)] = Array((lulu,24), (ningning,25), (xiaoyueyue,26))
scala> val arr2 = Array(24,25,26,27)
arr2: Array[Int] = Array(24, 25, 26, 27)
scala> arr1.zip(arr2)
res29: Array[(String, Int)] = Array((lulu,24), (ningning,25), (xiaoyueyue,26))
集合
集合之seq
scala> val list = List(1,2,3)
list: List[Int] = List(1, 2, 3)
scala> val list2 = 0 :: list
list2: List[Int] = List(0, 1, 2, 3)
scala> val list3 = list .::(0)
list3: List[Int] = List(0, 1, 2, 3)
scala> val list4 = 0 +: list
list4: List[Int] = List(0, 1, 2, 3)
scala> val list5 = list .+:(0)
list5: List[Int] = List(0, 1, 2, 3)
scala> val list6 = list :+ 4
list6: List[Int] = List(1, 2, 3, 4)
scala> val list7 = List(5,6,7)
list7: List[Int] = List(5, 6, 7)
scala> val list8 = list ++ list7
list8: List[Int] = List(1, 2, 3, 5, 6, 7)
scala> val list8 = list ++: list7
list8: List[Int] = List(1, 2, 3, 5, 6, 7)
scala> val list9 = list ++: list7
list9: List[Int] = List(1, 2, 3, 5, 6, 7)
scala> import scala.collection.mutable.ListBuffer
import scala.collection.mutable.ListBuffer
scala> val list1 = ListBuffer(1,2,3)
list1: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3)
scala> list1 += 4
res0: list1.type = ListBuffer(1, 2, 3, 4)
scala> list1
res1: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3, 4)
scala> list1.append(5)
scala> val list2 = ListBuffer(6,7,8)
list2: scala.collection.mutable.ListBuffer[Int] = ListBuffer(6, 7, 8)
scala> list1 ++= list2
res3: list1.type = ListBuffer(1, 2, 3, 4, 5, 6, 7, 8)
scala> list1 ++ list2
res4: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3, 4, 5, 6, 7, 8, 6, 7, 8)
scala> list1 :+ 4
res5: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3, 4, 5, 6, 7, 8, 4)
集合之set
scala> import scala.collection.immutable.HashSet
import scala.collection.immutable.HashSet
scala> val set1 = new HashSet[Int]()
set1: scala.collection.immutable.HashSet[Int] = Set()
scala> val set2 = set1 + 1
set2: scala.collection.immutable.HashSet[Int] = Set(1)
scala> val set3 = set2 ++ Set(2,3,4)
set3: scala.collection.immutable.HashSet[Int] = Set(1, 2, 3, 4)
scala> import scala.collection.mutable.HashSet
import scala.collection.mutable.HashSet
scala> val set1 = new HashSet[Int]()
set1: scala.collection.mutable.HashSet[Int] = Set()
scala> set1 += 1
res6: set1.type = Set(1)
scala> set1 += 2
res7: set1.type = Set(1, 2)
scala> set1.add(3)
res8: Boolean = true
scala> set1 ++= Set(4,5,6)
res9: set1.type = Set(1, 5, 2, 6, 3, 4)
scala> set1 ++= Set(5,6,7)
res10: set1.type = Set(1, 5, 2, 6, 3, 7, 4)
scala> set1 -= 1
res11: set1.type = Set(5, 2, 6, 3, 7, 4)
scala> set1.remove(2)
res12: Boolean = true
集合之map
scala> val map1 = new HashMap[String,Int]()
<console>:11: error: not found: type HashMap
val map1 = new HashMap[String,Int]()
^
scala> import scala.collection.mutable._
import scala.collection.mutable._
scala> val map1 = new HashMap[String,Int]()
map1: scala.collection.mutable.HashMap[String,Int] = Map()
scala> map1("scala") = 1
scala> map1 += (("java",2))
res14: map1.type = Map(scala -> 1, java -> 2)
scala> map1 += (("python",3),("c#",4))
res15: map1.type = Map(scala -> 1, java -> 2, c# -> 4, python -> 3)
scala> map1.put("c++",5)
res16: Option[Int] = None
scala> map1.put("c++",5)
res17: Option[Int] = Some(5)
scala> map1
res18: scala.collection.mutable.HashMap[String,Int] = Map(scala -> 1, c++ -> 5, java -> 2, c# -> 4, python -> 3)
scala> map1 -= "java"
res19: map1.type = Map(scala -> 1, c++ -> 5, c# -> 4, python -> 3)
scala> map1.remove("c++")
res20: Option[Int] = Some(5)
scala> map1
res21: scala.collection.mutable.HashMap[String,Int] = Map(scala -> 1, c# -> 4, python -> 3)
网友评论