《scala函數式編程》之List和Set

package com.lyzx.day20181005

/**
  * Scala 集合分爲可變的和不可變的集合
  * 默認情況下,Scala 使用的是不可變集合,如果你想使用可變集合,需要引用 scala.collection.mutable.Set包.
  * 默認引用 scala.collection.immutable._,不可變集合實例如下
  */
class DataStructure {
  /**
    * 列表的定義
    *   可以使用 item :: 列表即t2  Nil表示一個空列表即List() == Nil
    *    可以使用 列表 :+ item 在列表後面添加一個元素
    * 通過 indices 可以獲取列表的下標Range 再通過for循環遍歷數組
    */
  def list_f1(): Unit ={
    val t1:List[Int] = List(1,2,3)
//    val t2:List[String] = "C" :: ("A" :: ("B" :: Nil))
    val t2 = List("A","B","C") +: "D"

    for(item <- t2){
      print(item+" , ")
    }
    println()
    for(i <- t1.indices){
      println(i+"    "+t1(i))
    }

    /**
      * List.head  返回List的第一個元素
      * List.tail  返回除了第一個元素的List集合
      */
    println("t2.head="+t2.head)
    println("t2.tail="+t2.tail)
    println("t2.isEmpty="+t2.isEmpty)
  }

  /**
    * 列表的連接
    * 1、通過 :: 向List的頭部添加一個元素
    * 2、
    *   2.1、通過 :::添加   t1 ::: t2                表示把t2的元素放在了t1的後面
    *   2.2、通過 .:::      t1.:::(t2)               表示把t2的元素放在了t1的前面
    *   2.3、List.concat()  List.concat(t1,t2)       表示把t2的元素放在了t1的後面
    */
  def list_f2(): Unit ={
    val t1 = "B" :: ("A" :: Nil)
    val t2 = List("C","D")

    val t2_1 = t1 ::: t2
    val t2_2 = t1.:::(t2)
    val t2_3 = List.concat(t1,t2)

    print(" ::: ")
    for(item <- t2_1){
      print(item+",")
    }
    println()

    print(" .:::() ")
    for(item <- t2_2){
      print(item+",")
    }
    println()

    print("List.concat() ")
    for(item <- t2_3){
      print(item+",")
    }
  }


  /**
    * fill函數 表示填充N次元素
    * List.fill(2)("A")  表示返回一個列表,其中有2個字符串元素 "A"
    */
  def list_f3(): Unit ={
    val t1 = List.fill(2)("A")
    for(item <- t1){
      print(item+"   ")
    }
    println()
    val t2 = (t1 ::: ("X" :: Nil)).reverse
    for(item <- t2){
      print(item+"   ")
    }
  }

  /**
    * set的基本操作
    */
  def set_f4(): Unit ={
    val set = Set(100,1,2,3,3,4)
    println("1:"+set.getClass.getName)
    println("2:"+set.exists( _ >= 4))
    println("3:"+set.drop(1))
    println("4:"+set.dropRight(1))
    println("5:"+set.dropWhile(_ > 3)) //???

    for(item <- set){
        print(item+"  ")
    }
    println()
    println("6:"+set.head)
    println("7:"+set.tail)
    println("8:"+set.isEmpty)
  }

  /**
    * set的連接
    */
  def set_f5(): Unit ={
    val s1 = Set(1,2,3,4)
    val s2 = Set(2,3,4,5)
    val s3 = s1 ++ s2
    println("s3 = "+s3)
    val s4 = s1.++(s2)
    println("s4= "+s4)

    println("min="+s3.min+"    max="+s3.max)
    println("交集:"+s1.intersect(s2))
    println("s1.diff(s2):"+s1.diff(s2))
    println("s2.diff(s1):"+s2.diff(s1))
  }
}

object DataStructure{

  def main(args: Array[String]): Unit = {
    val t = new DataStructure
    t.set_f5()
  }
}

附上完整版的List數據結構API

序號 方法及描述
1

def +:(elem: A): List[A]

爲列表預添加元素

scala> val x = List(1)
x: List[Int] = List(1)

scala> val y = 2 +: x
y: List[Int] = List(2, 1)

scala> println(x)
List(1)
2

def ::(x: A): List[A]

在列表開頭添加元素

3

def :::(prefix: List[A]): List[A]

在列表開頭添加指定列表的元素

4

def :+(elem: A): List[A]

複製添加元素後列表。

scala> val a = List(1)
a: List[Int] = List(1)

scala> val b = a :+ 2
b: List[Int] = List(1, 2)

scala> println(a)
List(1)
5

def addString(b: StringBuilder): StringBuilder

將列表的所有元素添加到 StringBuilder

6

def addString(b: StringBuilder, sep: String): StringBuilder

將列表的所有元素添加到 StringBuilder,並指定分隔符

7

def apply(n: Int): A

通過列表索引獲取元素

8

def contains(elem: Any): Boolean

檢測列表中是否包含指定的元素

9

def copyToArray(xs: Array[A], start: Int, len: Int): Unit

將列表的元素複製到數組中。

10

def distinct: List[A]

去除列表的重複元素,並返回新列表

11

def drop(n: Int): List[A]

丟棄前n個元素,並返回新列表

12

def dropRight(n: Int): List[A]

丟棄最後n個元素,並返回新列表

13

def dropWhile(p: (A) => Boolean): List[A]

從左向右丟棄元素,直到條件p不成立

14

def endsWith[B](that: Seq[B]): Boolean

檢測列表是否以指定序列結尾

15

def equals(that: Any): Boolean

判斷是否相等

16

def exists(p: (A) => Boolean): Boolean

判斷列表中指定條件的元素是否存在。

判斷l是否存在某個元素:

scala> l.exists(s => s == "Hah")
res7: Boolean = true
17

def filter(p: (A) => Boolean): List[A]

輸出符號指定條件的所有元素。

過濾出長度爲3的元素:

scala> l.filter(s => s.length == 3)
res8: List[String] = List(Hah, WOW)
18

def forall(p: (A) => Boolean): Boolean

檢測所有元素。

例如:判斷所有元素是否以"H"開頭:

scala> l.forall(s => s.startsWith("H")) res10: Boolean = false
19

def foreach(f: (A) => Unit): Unit

將函數應用到列表的所有元素

20

def head: A

獲取列表的第一個元素

21

def indexOf(elem: A, from: Int): Int

從指定位置 from 開始查找元素第一次出現的位置

22

def init: List[A]

返回所有元素,除了最後一個

23

def intersect(that: Seq[A]): List[A]

計算多個集合的交集

24

def isEmpty: Boolean

檢測列表是否爲空

25

def iterator: Iterator[A]

創建一個新的迭代器來迭代元素

26

def last: A

返回最後一個元素

27

def lastIndexOf(elem: A, end: Int): Int

在指定的位置 end 開始查找元素最後出現的位置

28

def length: Int

返回列表長度

29

def map[B](f: (A) => B): List[B]

通過給定的方法將所有元素重新計算

30

def max: A

查找最大元素

31

def min: A

查找最小元素

32

def mkString: String

列表所有元素作爲字符串顯示

33

def mkString(sep: String): String

使用分隔符將列表所有元素作爲字符串顯示

34

def reverse: List[A]

列表反轉

35

def sorted[B >: A]: List[A]

列表排序

36

def startsWith[B](that: Seq[B], offset: Int): Boolean

檢測列表在指定位置是否包含指定序列

37

def sum: A

計算集合元素之和

38

def tail: List[A]

返回所有元素,除了第一個

39

def take(n: Int): List[A]

提取列表的前n個元素

40

def takeRight(n: Int): List[A]

提取列表的後n個元素

41

def toArray: Array[A]

列表轉換爲數組

42

def toBuffer[B >: A]: Buffer[B]

返回緩衝區,包含了列表的所有元素

43

def toMap[T, U]: Map[T, U]

List 轉換爲 Map

44

def toSeq: Seq[A]

List 轉換爲 Seq

45

def toSet[B >: A]: Set[B]

List 轉換爲 Set

46

def toString(): String

列表轉換爲字符串

 

附上完整版的Set數據結構的API

序號 方法及描述
1

def +(elem: A): Set[A]

爲集合添加新元素,x並創建一個新的集合,除非元素已存在

2

def -(elem: A): Set[A]

移除集合中的元素,並創建一個新的集合

3

def contains(elem: A): Boolean

如果元素在集合中存在,返回 true,否則返回 false。

4

def &(that: Set[A]): Set[A]

返回兩個集合的交集

5

def &~(that: Set[A]): Set[A]

返回兩個集合的差集

6

def +(elem1: A, elem2: A, elems: A*): Set[A]

通過添加傳入指定集合的元素創建一個新的不可變集合

7

def ++(elems: A): Set[A]

合併兩個集合

8

def -(elem1: A, elem2: A, elems: A*): Set[A]

通過移除傳入指定集合的元素創建一個新的不可變集合

9

def addString(b: StringBuilder): StringBuilder

將不可變集合的所有元素添加到字符串緩衝區

10

def addString(b: StringBuilder, sep: String): StringBuilder

將不可變集合的所有元素添加到字符串緩衝區,並使用指定的分隔符

11

def apply(elem: A)

檢測集合中是否包含指定元素

12

def count(p: (A) => Boolean): Int

計算滿足指定條件的集合元素個數

13

def copyToArray(xs: Array[A], start: Int, len: Int): Unit

複製不可變集合元素到數組

14

def diff(that: Set[A]): Set[A]

比較兩個集合的差集

15

def drop(n: Int): Set[A]]

返回丟棄前n個元素新集合

16

def dropRight(n: Int): Set[A]

返回丟棄最後n個元素新集合

17

def dropWhile(p: (A) => Boolean): Set[A]

從左向右丟棄元素,直到條件p不成立

 

18

def equals(that: Any): Boolean

equals 方法可用於任意序列。用於比較系列是否相等。

19

def exists(p: (A) => Boolean): Boolean

判斷不可變集合中指定條件的元素是否存在。

20

def filter(p: (A) => Boolean): Set[A]

輸出符合指定條件的所有不可變集合元素。

21

def find(p: (A) => Boolean): Option[A]

查找不可變集合中滿足指定條件的第一個元素

22

def forall(p: (A) => Boolean): Boolean

查找不可變集合中滿足指定條件的所有元素

23

def foreach(f: (A) => Unit): Unit

將函數應用到不可變集合的所有元素

24

def head: A

獲取不可變集合的第一個元素

25

def init: Set[A]

返回所有元素,除了最後一個

26

def intersect(that: Set[A]): Set[A]

計算兩個集合的交集

27

def isEmpty: Boolean

判斷集合是否爲空

28

def iterator: Iterator[A]

創建一個新的迭代器來迭代元素

29

def last: A

返回最後一個元素

30

def map[B](f: (A) => B): immutable.Set[B]

通過給定的方法將所有元素重新計算

31

def max: A

查找最大元素

32

def min: A

查找最小元素

33

def mkString: String

集合所有元素作爲字符串顯示

34

def mkString(sep: String): String

使用分隔符將集合所有元素作爲字符串顯示

35

def product: A

返回不可變集合中數字元素的積。

36

def size: Int

返回不可變集合元素的數量

37

def splitAt(n: Int): (Set[A], Set[A])

把不可變集合拆分爲兩個容器,第一個由前 n 個元素組成,第二個由剩下的元素組成

38

def subsetOf(that: Set[A]): Boolean

如果集合中含有子集返回 true,否則返回false

39

def sum: A

返回不可變集合中所有數字元素之和

40

def tail: Set[A]

返回一個不可變集合中除了第一元素之外的其他元素

41

def take(n: Int): Set[A]

返回前 n 個元素

42

def takeRight(n: Int):Set[A]

返回後 n 個元素

43

def toArray: Array[A]

將集合轉換爲數組

44

def toBuffer[B >: A]: Buffer[B]

返回緩衝區,包含了不可變集合的所有元素

45

def toList: List[A]

返回 List,包含了不可變集合的所有元素

46

def toMap[T, U]: Map[T, U]

返回 Map,包含了不可變集合的所有元素

47

def toSeq: Seq[A]

返回 Seq,包含了不可變集合的所有元素

48

def toString(): String

返回一個字符串,以對象來表示

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章