scala02

 

scala02课件

函数得定义

val funtionName=(param:ParamType...)=>{}

这种定义方式没有返回值类型,会自己进行适配

函数在scala中可以任务是一个可以使用得值

 

函数在放置得时候会显示签名信息,描述当前函数得一个描述信息文件

eg:scala中存在三种类型 class类  object对象  trait特质(接口)

定义得函数其实在scala内部已经存在模板,function0-function22我们根据参数得数量不一致可以得出23中函数

 

函数定义得复杂形式

scala> val add:(Int,Int)=>Int=(x,y)=>{x+y}

add: (Int, Int) => Int = <function2>

val func:(paramType...)=>returnType=(param...)=>{content}

 

如果直接放置函数则显示得是签名信息  function()调用

 

高级函数得使用

scala> val a:(Int,Int)=>Int = add

a: (Int, Int) => Int = <function2>

 

scala> val b:(Int,Int)=>Int=add

b: (Int, Int) => Int = <function2>

函数得放置类型,函数可以作为返回值

 

函数作为参数传递

 

scala> calculate(1,2,add)

res10: Int = 3

 

scala> val cj=(x:Int,y:Int)=>x*y

cj: (Int, Int) => Int = <function2>

 

scala> calculate(2,3,cj)

res11: Int = 6

 

scala> calculate(3,4,(x:Int,y:Int)=>x%y)

res12: Int = 3

注意函数得泛型

 

函数和方法几乎是一个东西?

函数和方法得相互转换?

scala> val sum=(x:Int,y:Int)=>x+y

sum: (Int, Int) => Int = <function2>

 

scala> sum

res14: (Int, Int) => Int = <function2>

 

scala> add _

res15: (Int, Int) => Int = <function2>

 

scala> res15(1,2)

res16: Int = 3

 

scala> add _(2,3)

<console>:1: error: ';' expected but '(' found.

add _(2,3)

     ^

 

scala> (add _)(3,4)

res17: Int = 7

 

函数能够作为方法得参数,如果将方法也放入到方法得参数中

scala> (add _)(3,4)

res17: Int = 7

 

scala> calculate(2,3,add _)

res18: Int = 5

 

scala> calculate(2,3,add)

res19: Int = 5

在默认情况下,方法如果作为参数了,那么可以自动转换为函数

 

scala> println _

res21: () => Unit = <function0>

 

scala> println(1,2,3,4,5)

(1,2,3,4,5)

 

scala> var arr = Array(1,2,3,4,5,6,76)

arr: Array[Int] = Array(1, 2, 3, 4, 5, 6, 76)

 

scala> arr.foreach(println)

1

2

3

4

5

6

76

 

scala> arr.foreach(println _)

经常使用得打印方法

 

总结:

  1. 函数和方法得定义方式不一样 def={}  val=>
  2. 函数可以作为表达式单独存在,能够显示签名信息,但是方法只能被调用
  3. 函数是将scala中内置得function0-22trait进行了实现
  4. 函数是scala中得头等公民
  5. 函数和方法得本质区别不大,默认情况下可以相互转换
  6. 方法转换为函数得时候 method _
  7. 其实还是方法使用得多

  

scala中得集合框架

scala中集合框架分为两种immutable  mutable

常用得五种框架

tuple元组类型 Array数组类型 list  map  set

 

元组tuple

一些列得数据得集合,不可变的

相当于对象,可以将一些完全不相关的数据放置在一起

scala> var tp = ("zhangsan",3500)

tp: (String, Int) = (zhangsan,3500)

 

scala> var tp = ("zhangsan",35000,40,"fs")

tp: (String, Int, Int, String) = (zhangsan,35000,40,fs)

 

scala> val tp:(String,String,String)=("","","")

tp: (String, String, String) = ("","","")

 

scala> val tp = new Tuple[String,Int]("zhangsan",20000)

<console>:11: error: not found: type Tuple

       val tp = new Tuple[String,Int]("zhangsan",20000)

                    ^

 

scala> val tp = new Tuple2[String,Int]("zhangsan",20000)

tp: (String, Int) = (zhangsan,20000)

 

scala> val tp:Tuple2[String,Int]=("zhangsan",23000)

tp: (String, Int) = (zhangsan,23000)

 

scala> val tp:(String,Int) = new Tuple2[String,Int]("zhangsan",20000)

tp: (String, Int) = (zhangsan,20000)

 

scala> tp._1

res25: String = zhangsan

 

scala> tp._2

res26: Int = 20000

 

scala> tp

res27: (String, Int) = (zhangsan,20000)

 

scala> (1,tp)

res28: (Int, (String, Int)) = (1,(zhangsan,20000))

 

scala> res28._2._1

res29: String = zhangsan

元组是Tuple类的实例,总共存在Tuple1-22

取值的时候按照元素的名称取值_1_N

 

一个特殊的元组对偶元组

两个元素的元组就是对偶元组

scala> val tp:Tuple2[String,Int] = ("zhangsan",20000)

tp: (String, Int) = (zhangsan,20000)

 

scala> tp.swap

res30: (Int, String) = (20000,zhangsan)

对偶元组中存在一个swap方法进行元素的互换

对偶元组是map集合中的一个元素

 

zip拉链操作

scala> var arr = Array("hello","world","tom","jack")

arr: Array[String] = Array(hello, world, tom, jack)

 

scala> var arr1 = Array(1,2,3)

arr1: Array[Int] = Array(1, 2, 3)

 

scala> arr zip arr1

res31: Array[(String, Int)] = Array((hello,1), (world,2), (tom,3))

 

scala> arr.zip(arr1)

res32: Array[(String, Int)] = Array((hello,1), (world,2), (tom,3))

zip拉链操作完毕的数据是一个元组的array集合,按照木桶原理

 

zipWithIndex与下标进行拉链

scala> arr

res33: Array[String] = Array(hello, world, tom, jack)

 

scala> arr.zipWithIndex

res34: Array[(String, Int)] = Array((hello,0), (world,1), (tom,2), (jack,3))

与下标进行拉链操作,下标从0开始

 

数组Array

array存在两种 immutable  mutable

scala.Array直接使用就是这个默认的,不可变的数组

scala.collection.mutable.ArrayBuffer可变数组

Array是长度不可变,但是元素可变

ArrayBuffer长度和元素都可以变化

 

数组的声明

 

scala> var arr = Array(1,2,3,4,5,6)

arr: Array[Int] = Array(1, 2, 3, 4, 5, 6)

 

scala> var arr = new Array(3)

arr: Array[Nothing] = Array(null, null, null)

 

scala> arr(1)=3

<console>:13: error: type mismatch;

 found   : Int(3)

 required: Nothing

       arr(1)=3

              ^

 

scala> arr(0)

java.lang.NullPointerException

 

scala> var arr = new Array[Int](4)

arr: Array[Int] = Array(0, 0, 0, 0)

 

scala> var arr = new Array[Boolean](4)

arr: Array[Boolean] = Array(false, false, false, false)

取值方式 arr(index)  赋值arr(index)=N

声明的时候如果是new的数组,那么一定要指定泛型,不然泛型就是Nothing,不可以使用

 

Array的操作方法

scala> arr:+1

res38: Array[AnyVal] = Array(false, false, false, false, 1)

 

scala> arr:+23

res39: Array[AnyVal] = Array(false, false, false, false, 23)

 

scala> arr

res40: Array[Boolean] = Array(false, false, false, false)

 

scala> arr ++ arr

res41: Array[Boolean] = Array(false, false, false, false, false, false, false, false)

 

scala> arr

res42: Array[Boolean] = Array(false, false, false, false)

 

scala> arr.isEmpty

res43: Boolean = false

 

scala> arr.max

res44: Boolean = false

 

scala> arr.min

res45: Boolean = false

 

scala> arr.sum

<console>:13: error: could not find implicit value for parameter num: Numeric[Boolean]

       arr.sum

           ^

 

scala> var arr = Array(1,2,3,4,5,6)

arr: Array[Int] = Array(1, 2, 3, 4, 5, 6)

 

scala> arr.sum

res47: Int = 21

 

scala> arr.length

res48: Int = 6

 

scala> arr.size

res49: Int = 6

 

sorted sortWith sortBy

scala> var arr = Array(4,2,1,6,12,23)

arr: Array[Int] = Array(4, 2, 1, 6, 12, 23)

 

scala> arr.sorted

res51: Array[Int] = Array(1, 2, 4, 6, 12, 23)

 

scala> arr

res55: Array[(String, Int)] = Array((zhangsan,56), (lisi,21), (wangwu,67))

 

scala> arr.sortBy

   def sortBy[B](f: ((String, Int)) => B)(implicit ord: scala.math.Ordering[B]): Array[(String, Int)]

 

scala> arr.sortBy

   def sortBy[B](f: ((String, Int)) => B)(implicit ord: scala.math.Ordering[B]): Array[(String, Int)]

 

scala> arr.sortBy((t:(String,Int))=>t._2)

res56: Array[(String, Int)] = Array((lisi,21), (zhangsan,56), (wangwu,67))

 

scala> arr.sortBy((t:(String,Int))=> -t._2)

res57: Array[(String, Int)] = Array((wangwu,67), (zhangsan,56), (lisi,21))

 

scala> arr.sortBy(t=>t._2)

res58: Array[(String, Int)] = Array((lisi,21), (zhangsan,56), (wangwu,67))

 

scala> val func=(x:(String,Int))=>x._2

func: ((String, Int)) => Int = <function1>

 

scala> arr.sortBy(func)

res61: Array[(String, Int)] = Array((lisi,21), (zhangsan,56), (wangwu,67))

 

scala> arr.sortBy(t=>t._2)

res62: Array[(String, Int)] = Array((lisi,21), (zhangsan,56), (wangwu,67))

 

scala> arr.sortBy(_._2)

res63: Array[(String, Int)] = Array((lisi,21), (zhangsan,56), (wangwu,67))

 

sortWith与谁一起排序

scala> arr.sortWith(func)

res64: Array[(String, Int)] = Array((wangwu,67), (zhangsan,56), (lisi,21))

 

scala> val func=(x:(String,Int),y:(String,Int))=>x._2<y._2

func: ((String, Int), (String, Int)) => Boolean = <function2>

 

scala> arr.sortWith(func)

res65: Array[(String, Int)] = Array((lisi,21), (zhangsan,56), (wangwu,67))

 

scala> arr.sortWith((x:(String,Int),y:(String,Int))=>x._2>y._2)

res66: Array[(String, Int)] = Array((wangwu,67), (zhangsan,56), (lisi,21))

 

scala> arr.sortWith((x,y)=>x._2>y._2)

res67: Array[(String, Int)] = Array((wangwu,67), (zhangsan,56), (lisi,21))

 

scala> arr.sortWith(_._2>_._2)

res68: Array[(String, Int)] = Array((wangwu,67), (zhangsan,56), (lisi,21))

记住下划线的使用方式,统配每一个元素

匿名函数的使用规则

sortWith是按照相邻的两个元素进行比较

以上三种排序方式适用所有的集合

 

ArrayBuffer可变数组

scala> import scala.collection.mutable.ArrayBuffer

import scala.collection.mutable.ArrayBuffer

 

scala> var buff = ArrayBuffer(1,2,3,4,5,6)

buff: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5, 6)

 

scala> var buff:ArrayBuffer[Int] = ArrayBuffer[Int](1,2,3,4,5,6)

buff: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5, 6)

 

scala> var buff = new ArrayBuffer[Int](4)

buff: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()

 

scala> var buff = new ArrayBuffer[Int]()

buff: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()

在声明arrayBuffer的时候不能加上长度,因为arrayBuffer是长度可变的

 

scala> buff

res70: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()

 

scala> buff+=1

res71: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1)

 

scala> buff

res72: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1)

 

scala> buff++=Array(1,2,3,4,5)

res73: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 2, 3, 4, 5)

 

scala> buff

res74: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 2, 3, 4, 5)

 

scala> buff-=(1)

res75: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5)

 

scala> buff--=Array(1,1,1,11,2,3,4)

res76: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(5)

 

scala> buff.insert

insert   insertAll

 

scala> buff.insert

   def insert(n: Int,elems: Int*): Unit

 

scala> buff.insert(0,1,2,3,4,5,6,7,8)

 

scala> buff

res78: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5, 6, 7, 8, 5)

 

scala> buff.remove

   def remove(n: Int): Int   override def remove(n: Int,count: Int): Unit

 

scala> buff.remove(0)

res79: Int = 1

 

scala> buff

res80: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(2, 3, 4, 5, 6, 7, 8, 5)

 

scala> buff.remove(0,3)

 

scala> buffer

<console>:13: error: not found: value buffer

       buffer

       ^

 

scala> buff

res83: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(5, 6, 7, 8, 5)

 

scala> buff.clear

 

scala> buff

res85: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()

 

scala> buff.isEmpty

res86: Boolean = true

 

scala> arr.toBuffer

res88: scala.collection.mutable.Buffer[Int] = ArrayBuffer(1)

 

scala> res88.toArray

res89: Array[Int] = Array(1)

 

数组练习题:

  1. 创建一个方法,方法中的参数是一个Array[Int]集合,求出这个数组的最大值,最小值,平均值,一次性返回

scala> def getMMA(x:Array[Int]):(Int,Int,Double)={

     | (x.max,x.min,x.sum*1.0/x.size)

     | }

getMMA: (x: Array[Int])(Int, Int, Double)

 

scala> getMMA(Array(1,2,3,4,5,6))

res94: (Int, Int, Double) = (6,1,3.5)

 

List集合

scala.list不可变的,元素和长度都不可变

scala.collection.mutable.ListBuffer 长度和内容都可变

scala> var list:List[Int]=List(1,2,3,4,54)

list: List[Int] = List(1, 2, 3, 4, 54)

 

scala> var list:List[Int]=List[Int](1,2,3,4,54)

list: List[Int] = List(1, 2, 3, 4, 54)

 

scala> var list = new List[Int](4)

<console>:12: error: class List is abstract; cannot be instantiated

       var list = new List[Int](4)

                  ^

 

scala> var list = 1::2::3::4::Nil

list: List[Int] = List(1, 2, 3, 4)

::拼接,将元素和list集合进行拼接

Nil他是list的特殊形式,代表一个空的列表

var list = 1::2::3::4::Nil))))拼接的时候从最右侧进行拼接

scala> Nil

res95: scala.collection.immutable.Nil.type = List()

 

scala> 1::Nil

res96: List[Int] = List(1)

list中的元素和长度都不可以发生改变

取值的时候list(N)取第N个位置的元素

 

ListBuffer可变的集合

scala> val buff = ListBuffer(1,2,3,4,5,6)

<console>:12: error: not found: value ListBuffer

       val buff = ListBuffer(1,2,3,4,5,6)

                  ^

 

scala> val buff = scala.collection.mutable.ListBuffer(1,2,3,4,5,6)

buff: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3, 4, 5, 6)

 

scala> import scala.collection.mutable.ListBuffer

import scala.collection.mutable.ListBuffer

 

scala> val buff = new ListBuffer[Int](3)

<console>:13: error: too many arguments for constructor ListBuffer: ()scala.collection.mutable.ListBuffer[Int]

       val buff = new ListBuffer[Int](3)

                  ^

 

scala> val buff = new ListBuffer[Int]()

buff: scala.collection.mutable.ListBuffer[Int] = ListBuffer()

 

-= --= += ++= insert remove clear isEmpty max min sum reverse....ArrayBuffer

 

scala> buff

res106: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 1, 2, 3, 4, 5)

 

scala> buff.toArray

res107: Array[Int] = Array(1, 1, 2, 3, 4, 5)

 

scala> buff.toBuffer

res108: scala.collection.mutable.Buffer[Int] = ArrayBuffer(1, 1, 2, 3, 4, 5)

 

scala> Array().toBuffer

res109: scala.collection.mutable.Buffer[Nothing] = ArrayBuffer()

 

scala> var arr = Array(1,2,3,4,5)

arr: Array[Int] = Array(1, 2, 3, 4, 5)

 

scala> arr.toList

res110: List[Int] = List(1, 2, 3, 4, 5)

toBuffer转换的是ArrayBuffer  toList转换为List  toArray转换为array

 

Map也是两种,一种是可变的,一种不可变的

但是两种map都叫一个名字 Map

scala> var map:Map[String,Int]=Map("zhangsan"->2000)

map: Map[String,Int] = Map(zhangsan -> 2000)

 

scala> var map = Map("zhangsan"->3500)

map: scala.collection.immutable.Map[String,Int] = Map(zhangsan -> 3500)

 

scala> var map = Map(("zhangsan",5000))

map: scala.collection.immutable.Map[String,Int] = Map(zhangsan -> 5000)

元素和长度都不可以发生改变,创建的时候可以放入对偶元组

scala> map+("lisi"->5000)

res111: scala.collection.immutable.Map[String,Int] = Map(zhangsan -> 5000, lisi -> 5000)

 

scala> map

res112: scala.collection.immutable.Map[String,Int] = Map(zhangsan -> 5000)

 

scala> map("zhangsan")=3400

<console>:15: error: value update is not a member of scala.collection.immutable.Map[String,Int]

       map("zhangsan")=3400

 

scala> val map = new scala.collection.immutable.HashMap[String,Int]()

map: scala.collection.immutable.HashMap[String,Int] = Map()

 

scala> map+("zhangsan"->2000)

res114: scala.collection.immutable.HashMap[String,Int] = Map(zhangsan -> 2000)

 

scala> map

res115: scala.collection.immutable.HashMap[String,Int] = Map()

 

scala> map ++ Map("zhangsan"->23111)

res117: scala.collection.immutable.Map[String,Int] = Map(zhangsan -> 23111)

 

scala> map

res118: scala.collection.immutable.HashMap[String,Int] = Map()

eg: ->是一个方法,指向k-v键值对的方法

不可变的map只能生成新的map集合 +  ++

 

map的取值方式

scala> map("zhangsan")

res120: Int = 2000

 

scala> map("zhangsan1")

java.util.NoSuchElementException: key not found: zhangsan1

  at scala.collection.MapLike$class.default(MapLike.scala:228)

  at scala.collection.AbstractMap.default(Map.scala:59)

  at scala.collection.MapLike$class.apply(MapLike.scala:141)

  at scala.collection.AbstractMap.apply(Map.scala:59)

  ... 32 elided

 

scala> map.get("zhangsan")

res122: Option[Int] = Some(2000)

 

scala> map.get("zhangsan1")

res123: Option[Int] = None

map(key)可以取值value

map.get(key)==>Option==>Some/None

scala> map

res126: scala.collection.immutable.Map[String,Int] = Map(zhangsan -> 2000)

 

scala> map.get("zhangsan")

res127: Option[Int] = Some(2000)

 

scala> res127.get

res128: Int = 2000

 

scala> map.get("zhangsan1")

res129: Option[Int] = None

 

scala> res129.get

java.util.NoSuchElementException: None.get

  at scala.None$.get(Option.scala:347)

  at scala.None$.get(Option.scala:345)

  ... 32 elided

 

scala> map.getOrElse("zhangsan1",3000)

res131: Int = 3000

 

scala> map.getOrElse("zhangsan",3000)

res132: Int = 2000

getOrElse可以取值,如果没有这个key,那么可以给一个默认值

 

 

可变map

scala> map+=("lisi"->23456)

res134: scala.collection.mutable.Map[String,Int] = Map(lisi -> 23456, zhangsan -> 20)

 

scala> map

res135: scala.collection.mutable.Map[String,Int] = Map(lisi -> 23456, zhangsan -> 20)

 

scala> map++=Map("wangwu"->32000)

res136: scala.collection.mutable.Map[String,Int] = Map(lisi -> 23456, zhangsan -> 20, wangwu -> 32000)

 

scala> map

res137: scala.collection.mutable.Map[String,Int] = Map(lisi -> 23456, zhangsan -> 20, wangwu -> 32000)

 

scala> map-=("zhangsan")

res138: scala.collection.mutable.Map[String,Int] = Map(lisi -> 23456, wangwu -> 32000)

 

scala> map-=("zhangsan","lisi","zhaoliu")

res139: scala.collection.mutable.Map[String,Int] = Map(wangwu -> 32000)

 

scala> map--=Array("zhangsan","lisi")

res140: scala.collection.mutable.Map[String,Int] = Map(wangwu -> 32000)

 

scala> map.remove("zhaoliu")

res141: Option[Int] = None

 

scala> map.remove("wangwu")

res142: Option[Int] = Some(32000)

 

scala> map.put("zhangsan",2000)

res143: Option[Int] = None

 

scala> map

res144: scala.collection.mutable.Map[String,Int] = Map(zhangsan -> 2000)

 

keySet keys  keysIterator  values valuesIterator

scala> var map = Map("zhangsan"->20000,"lisi"->30000,"wangwu"->49999)

map: scala.collection.immutable.Map[String,Int] = Map(zhangsan -> 20000, lisi -> 30000, wangwu -> 49999)

 

scala> map.keys

res145: Iterable[String] = Set(zhangsan, lisi, wangwu)

 

scala> map.keySet

res146: scala.collection.immutable.Set[String] = Set(zhangsan, lisi, wangwu)

 

scala> map.keysIterator

res147: Iterator[String] = non-empty iterator

 

scala> for(e<-map.keys){

     | println(map(e))

     | }

20000

30000

49999

 

scala> map.keysIterator

res149: Iterator[String] = non-empty iterator

 

scala> while(res149.hasNext){

     | println(map(res149.next))

     | }

20000

30000

49999

 

scala> for(e<-map.values)

     | println(e)

20000

30000

49999

通过以上方法可以直接遍历key或者value

 

scala> for(e<-map){

     | println(e)

     | }

(zhangsan,20000)

(lisi,30000)

(wangwu,49999)

 

scala> for((x,y)<-map)

     | println(x,y)

(zhangsan,20000)

(lisi,30000)

(wangwu,49999)

 

scala> for((x,y)<-map)

     | println(x)

zhangsan

lisi

wangwu

 

scala> for((x,_)<-map)

     | println(x)

zhangsan

lisi

wangwu

 

scala> for((_,x)<-map)

     | println(x)

20000

30000

49999

 

scala> for((_,x)<-map)

     | println(_)

<console>:16: error: missing parameter type for expanded function ((x$1) => println(x$1))

       println(_)

下划线可以统配所有的元素,但是不能取值

toMap方法,可以将一个集合转换为map集合

scala> var arr = Array("zhangsan"->2000,"lisi"->3000)

arr: Array[(String, Int)] = Array((zhangsan,2000), (lisi,3000))

 

scala> var arr1 = Array(("lisi",3000),("zhaosi",3500))

arr1: Array[(String, Int)] = Array((lisi,3000), (zhaosi,3500))

 

scala> arr.toMap

res158: scala.collection.immutable.Map[String,Int] = Map(zhangsan -> 2000, lisi -> 3000)

 

scala> arr1.toMap

res159: scala.collection.immutable.Map[String,Int] = Map(lisi -> 3000, zhaosi -> 3500)

 

scala> var arr = Array(("zhangsan",3000),(1000,20000))

arr: Array[(Any, Int)] = Array((zhangsan,3000), (1000,20000))

 

scala> arr.toMap

res160: scala.collection.immutable.Map[Any,Int] = Map(zhangsan -> 3000, 1000 -> 20000)

 

scala> var map = Map("1"->2,"2"->3,4->"")

map: scala.collection.immutable.Map[Any,Any] = Map(1 -> 2, 2 -> 3, 4 -> "")

toMap的时候,原来的数据一定是一个装有对偶元组的集合

 

set集合

scala> var set = Set(1,2,3,4,5)

set: scala.collection.immutable.Set[Int] = Set(5, 1, 2, 3, 4)

 

scala> var set = new Set[Int](3)

<console>:13: error: trait Set is abstract; cannot be instantiated

       var set = new Set[Int](3)

                 ^

 

scala> var set = new HashSet[Int](3)

<console>:13: error: not found: type HashSet

       var set = new HashSet[Int](3)

                     ^

 

scala> var set = new scala.collection.immutable.HashSet[Int](3)

<console>:13: error: too many arguments for constructor HashSet: ()scala.collection.immutable.HashSet[Int]

       var set = new scala.collection.immutable.HashSet[Int](3)

                 ^

 

scala> var set = new scala.collection.immutable.HashSet[Int]()

set: scala.collection.immutable.HashSet[Int] = Set()

 

scala> set+=1

 

scala> set

res162: scala.collection.immutable.HashSet[Int] = Set(1)

 

scala> var set = Set(1,2,3)

set: scala.collection.immutable.Set[Int] = Set(1, 2, 3)

 

scala> set+=12345

 

scala> set

res164: scala.collection.immutable.Set[Int] = Set(1, 2, 3, 12345)

元素可变,集合长度可变

cala> var set = scala.collection.mutable.Set(1,2,3,4)

set: scala.collection.mutable.Set[Int] = Set(1, 2, 3, 4)

 

scala> set+=123

res169: scala.collection.mutable.Set[Int] = Set(1, 2, 3, 123, 4)

 

scala> set

res170: scala.collection.mutable.Set[Int] = Set(1, 2, 3, 123, 4)

 

scala> set-=(1,2,3)

res171: scala.collection.mutable.Set[Int] = Set(123, 4)

 

scala> set.insert(1,2)

<console>:15: error: value insert is not a member of scala.collection.mutable.Set[Int]

       set.insert(1,2)

           ^

 

scala> set.remove(1)

res173: Boolean = false

 

scala> set

res174: scala.collection.mutable.Set[Int] = Set(123, 4)

 

scala> set.remove(4)

res175: Boolean = true

 

scala> set

res176: scala.collection.mutable.Set[Int] = Set(123)

可变的set集合中存在remove += ++= -= --=

不存在insert ,一般我们使用set都只是为了一个功能去重toSet方法

scala> var arr = Array(1,2,1,1,1,2,3,4,5)

arr: Array[Int] = Array(1, 2, 1, 1, 1, 2, 3, 4, 5)

 

scala> arr.distinct

res178: Array[Int] = Array(1, 2, 3, 4, 5)

 

scala> arr.toSet

res179: scala.collection.immutable.Set[Int] = Set(5, 1, 2, 3, 4)

 

list集合补充

scala> 1::list

res184: List[Int] = List(1, 1, 2, 3)

 

scala> list

res185: List[Int] = List(1, 2, 3)

 

scala> list:::list

res186: List[Int] = List(1, 2, 3, 1, 2, 3)

 

scala> res186.tail

res187: List[Int] = List(2, 3, 1, 2, 3)

 

scala> res186.head

res188: Int = 1

::是在一个list上面拼接一个元素,但是元素只能放在前面

:::拼接的是一个集合List:::List

list的组成方式永远分为两个部分,头元素和尾列表

新拼接的元素就是头元素,后面都是尾列表

posted @ 2019-09-16 14:30  lilixia  阅读(271)  评论(0编辑  收藏  举报