scala集合

一、數組java

  • Array能夠定義不可變數組,這裏的不可變指的是長度不可變
scala> var arr = Array("233", "33")
arr: Array[String] = Array(233, 33)

scala> arr(1) = "22"

scala> println(arr(1))
22

scala> arr(2)
java.lang.ArrayIndexOutOfBoundsException: 2
  ... 48 elided

  能夠指定數組的具體類型,也能夠不指定,Scala會自行斷定es6

scala> val arrStr = Array("scala","spark")
arrStr: Array[String] = Array(scala, spark)

scala> val arrInt = new Array[Int](10)
arrInt: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)

scala> val arrStr = Array("scala",1)
arrStr: Array[Any] = Array(scala, 1)

  遍歷數組數組

scala> var arr = Array("string","spark")
arr: Array[String] = Array(string, spark)

scala> for(i <- 0 until arr.length)print(arr(i) + " ")
string spark 

scala> for(ex <- arr)println(ex)
string
spark

  轉換爲ArrayBufferapp

scala> var arr = Array("233","23333")
arr: Array[String] = Array(233, 23333)

scala> var arr1 = arr.toBuffer
arr1: scala.collection.mutable.Buffer[String] = ArrayBuffer(233, 23333)

scala> arr1 += "222"
res1: scala.collection.mutable.Buffer[String] = ArrayBuffer(233, 23333, 222)
  • ArrayBuffer

    ArrayBuffer是可變數組,使用前須要顯示導入包ide

scala> import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.ArrayBuffer

scala> var arrBufInt = ArrayBuffer[Int]()
arrBufInt: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()

    +=在尾端添加元素es5

scala> arrBufInt+= 1
res3: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1)

    在尾端添加多個元素spa

scala> arrBufInt += (2,3,4,5)
res4: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5)

    ++=操做符能夠追加任何集合scala

scala> var arrBufInt2 = ArrayBuffer[Int]()  //建立一個新的ArrayBuffer
arrBufInt2: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()

scala> arrBufInt2 += (3,4,5)    //從尾端添加3,4,5
res5: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(3, 4, 5)

scala> arrBufInt ++= arrBufInt2     //從尾端添加一個ArrayBuffer
res6: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5, 3, 4, 5)

scala> for(i <- 0 until arrBufInt.length)print(arrBufInt(i) + " ")
1 2 3 4 5 3 4 5 

    ArrayBuffer的一些方法code

scala> arrBufInt.trimEnd(2)        //移除最後兩個元素

scala> for(i <- 0 until arrBufInt.length)print(arrBufInt(i) + " ")
1 2 3 4 5 3 4 5 6 

scala> arrBufInt.remove(2)        //移除索引號爲2的元素
res13: Int = 3

scala> for(i <- 0 until arrBufInt.length)print(arrBufInt(i) + " ")
1 2 4 5 3 4 5 6 

scala> arrBufInt.remove(2,4)    //移除從索引號爲2開始的4個元素

scala> for(i <- 0 until arrBufInt.length)print(arrBufInt(i) + " ")
1 2 5 6 

scala> var arry = arrBufInt.toArray  //轉換爲Array
arry: Array[Int] = Array(1, 2, 5, 6)
    

 

二、Mapblog

  Scala映射就是鍵值對的集合Map,默認狀況下,Scala使用不可變的映射。若是想使用可變集,必須明確地導入scala.collection.mutable.Map

  • 構建不可變映射
    val map = Map("name" -> "steve","age" -> "12","sex" -> "man")
    println(map("name"))      //獲取key爲name的值
    println("map中是否存在name:" + map.contains("name"))  //判斷map中是否有name做爲key的鍵值對
    println(map.getOrElse("age", "24"))     //若map中有age做爲key的鍵值對那麼返回對應的value,若不存在就返回24
    println(map.getOrElse("ages", "24"))
    //遍歷map
    map.keys.foreach{i =>
                        print("key = " + i)
                        println("  value = " + map(i))}

  輸出:

steve
map中是否存在name:true
12
24
key = age  value = 12
key = name  value = steve
key = sex  value = man
  • 構造可變映射
    val map = scala.collection.mutable.Map("name" -> "steve","age" -> "12")
    map += ("sex" -> "boy")//添加鍵值對
    println("map中是否存在sex:" + map.contains("sex"))
    map -= ("sex" )//刪除鍵值對
    println("map中是否存在sex:" + map.contains("sex"))
    //遍歷map
    for((k, v) <- map){println(k + " " + v)}
    //遍歷ma的鍵值
    for(key <- map.keySet){println(key)}

  輸出:

map中是否存在sex:true
map中是否存在sex:false
age 12
name steve
age
name

 

三、Tuple元組

  Tuple元組就是由兩個小括號包住能夠存聽任意類型的數據集合

val t = ("1",2,3.0)
t: (String, Int, Double) = (1,2,3.0)

val t = new Tuple3(1, 3.14, "Fred")
t: (Int, Double, String) = (1,3.14,Fred)

  元組的實際類型取決於它的元素的類型,要注意的是元組的長度是不可變的,目前 Scala 支持的元組最大長度爲 22。對於更大長度你可使用集合,或者擴展元組

scala> t._1
res5: Int = 1

scala> t._2
res6: Double = 3.14

scala> t._3
res7: String = Fred

  這樣就能訪問元組的元素

scala> t.productIterator.foreach{ i =>println("Value = " + i )}
Value = 1
Value = 3.14
Value = Fred

  使用 Tuple.productIterator() 方法來迭代輸出元組的全部元素

 

四、列表List

  列表List是不可變的,其中元素不能被改變,列表List是一個鏈表

  • 定義
    val list1:List[String] = List("apple","oranges","pears") //定義String類型的List
    val list2:List[Int] = List(1,2,3,4)  //定義Int類型的List
    val list3:List[Nothing] = List()    //定義空List
    val list4 = List("apple","oranges","pears")  //不指定類型,Scala將自行判判定義空List
    val list5:List[List[Int]] = List(List(1,2,3),List(4,5,6),List(7,8,9)) //定義二維List
    
    
    val list6 = "apple"::("orange"::("pears"::Nil))//使用Nil定義String類型的List
    val list7 = 1::(2::(3::(4::(5::Nil))))            //使用Nil定義Int類型的List
    val list8 = Nil                                   //使用Nil定義空List
    val list9 = (1::(2::(3::(4::Nil))))::(1::(2::(3::(4::Nil))))::(1::(2::(3::(4::Nil))))::Nil  //使用Nil定義二維List
  • 經常使用方法
    val list1:List[String] = List("apple","oranges","pears") //定義String類型的List
    val list2:List[Nothing] = List()    //定義空List
    val list3:List[List[Int]] = List(List(1,2,3),List(4,5,6),List(7,8,9)) //定義二維List
    println("list1 head  " + list1.head) //返回列表的第一個元素
    println( "list1 tail " + list1.tail )  //返回列表除了第一個元素的其餘元素
    println("list3 head  " + list3.head)
    println( "list3 tail " + list3.tail )
    println( "list1是否爲空 " + list1.isEmpty )
    println( "list2是否爲空 " + list2.isEmpty )

  輸出:

list1 head  apple
list1 tail List(oranges, pears)
list3 head  List(1, 2, 3)
list3 tail List(List(4, 5, 6), List(7, 8, 9))
list1是否爲空 false
list2是否爲空 true
  • 鏈接列表

  可使用 ::: 運算符或 List.:::() 方法或 List.concat() 方法來鏈接兩個或多個列表

    val list1:List[String] = List("apple","oranges","pears") //定義String類型的List
    val list2:List[String] = List("apple1","oranges1","pears1") //定義String類型的List
    val list3:List[List[Int]] = List(List(1,2,3),List(4,5,6),List(7,8,9)) //定義二維List
    val list4:List[List[Int]] = List(List(11,21,31),List(41,51,61),List(71,81,91)) //定義二維List
    val list5 = list1:::list2
    val list6 = list3:::list4
    val list7 = list1.:::(list2)
    val list8 = list3.:::(list4)
    val list9 = List.concat(list1,list2)
    val list10 = List.concat(list3,list4)
    println("list1:::list2  " + list5)
    println("list3:::list4  " + list6)
    println("list1.:::(list2)  " + list7)
    println("list3.:::(list4)  " + list8)
    println("List.concat(list1,list2)  " + list9)
    println("List.concat(list3,list4)  " + list10)

  輸出

list1:::list2  List(apple, oranges, pears, apple1, oranges1, pears1)
list3:::list4  List(List(1, 2, 3), List(4, 5, 6), List(7, 8, 9), List(11, 21, 31), List(41, 51, 61), List(71, 81, 91))
list1.:::(list2)  List(apple1, oranges1, pears1, apple, oranges, pears)
list3.:::(list4)  List(List(11, 21, 31), List(41, 51, 61), List(71, 81, 91), List(1, 2, 3), List(4, 5, 6), List(7, 8, 9))
List.concat(list1,list2)  List(apple, oranges, pears, apple1, oranges1, pears1)
List.concat(list3,list4)  List(List(1, 2, 3), List(4, 5, 6), List(7, 8, 9), List(11, 21, 31), List(41, 51, 61), List(71, 81, 91))

 

五、Set

  Set是不包含重複元素的集合

  • 定義不可變Set集合
    var s:Set[Nothing] = Set()  //定義空的Set
    var s1:Set[Int] = Set(1,2,3,4) //定義Int類型的Set
    var s2 = Set(1,2,3,4) //定義Int類型的Set
  • 定義可變的Set集合須要引入scala.collection.mutable.Set
    var s:Set[Int] = Set(1,2,3,4) //定義Int類型的Set
    println("s: " + s)
    s.add(5)
    println("s: " + s)
    s.remove(4)
    println("s: " + s)
    s += 6
    println("s: " + s)
    s -= 2
    println("s: " + s)

  輸出是:

s: Set(1, 2, 3, 4)
s: Set(1, 5, 2, 3, 4)
s: Set(1, 5, 2, 3)
s: Set(1, 5, 2, 6, 3)
s: Set(1, 5, 6, 3)
  • 經常使用方法
    var s:Set[Int] = Set(1,2,3,4) //定義Int類型的Set
    println("s.head " + s.head)
    println("s.tail " + s.tail)
    println("s.isEmpty " + s.isEmpty)

  輸出:

s.head 1
s.tail Set(2, 3, 4)
s.isEmpty false
  • 鏈接集合

  可使用 ++ 運算符或 Set.++() 方法來鏈接兩個集合。若是元素有重複的就會移除重複的元素

    var s1:Set[Int] = Set(1,2,3,4)
    var s2:Set[Int] = Set(4,5,6,1)
    var s3 = s1 ++ s2
    var s4 = s1.++(s2)
    println("s1 ++ s2  " + s3)
    println("s1.++(s2)  " +s4)

  輸出:

s1 ++ s2 Set(1, 5, 2, 6, 3, 4)s1.++(s2) Set(1, 5, 2, 6, 3, 4)

相關文章
相關標籤/搜索