Scala Immutable Collection(集合)

1.scala.collection.immutable

层次结构如下:

Scala Immutable Collection(集合)_第1张图片

  1. List使用
scala> val a = List(1,2,3,4)
a: List[Int] = List(1, 2, 3, 4)

scala> val b = 0 :: a   // 0连接a
b: List[Int] = List(0, 1, 2, 3, 4)

scala> val c = "x" :: "y" :: "z" ::Nil //z连接Nill连接y连接z
c: List[String] = List(x, y, z)
scala> val d =a :: c
d: List[java.io.Serializable] = Lis

scala> a.head
res0: Int = 1

scala> c.head
res1: String = x

scala> d.head
res2: java.io.Serializable = List(1
//尾链表除了第一个后面的都是尾巴
scala> a.tail
res3: List[Int] = List(2, 3, 4)

scala> c.tail
res4: List[String] = List(y, z)

scala> d.tail
res5: List[java.io.Serializable] = List(x, y, z)

scala> a.isEmpty    //判断是否为空
res6: Boolean = false

scala> Nil.isEmpty
res7: Boolean = true

scala> def walkthru(l: List[Int]): String = {
     | if(l.isEmpty) ""
     | else l.head.toString + " " +walkthru(l.tail)
     | }
walkthru: (l: List[Int])String
scala> walk(a)
scala> walkthru(a)
res9: String = "1 2 3 4 "

2.Scala List高级函数

/* filter */
scala> a.filter(x => x %2 ==1)
res10: List[Int] = List(1, 3)

/*找出链表里面的数字*/
scala> "xiao17is".toList.filter(x => Character.isDigit(x))
res14: List[Char] = List(1, 7)

/*检查链表中不等于s的值输出 x不等于s就是true */
 scala>  "xiao17is".toList.takeWhile(x => x!= 's')
res16: List[Char] = List(x, i, a, o, 1, 7, i)
  1. map
scala> c
res18: List[String] = List(x, y, z)

scala> c.map(x=>x.toUpperCase)
res20: List[String] = List(X, Y, Z)

scala> c.map(_.toUpperCase)
res21: List[String] = List(X, Y, Z)

scala> a.filter(_%2==1)
res22: List[Int] = List(1, 3)

scala> a.filter(_%2==1).map(_+10)
res23: List[Int] = List(11, 13)
/* 两层的List*/

scala> val q = List(a,List(4,5,6))
q: List[List[Int]] = List(List(1, 2, 3, 4), List(4, 5, 6))

scala> q.map(_.filter(_%2 == 0))
res24: List[List[Int]] = List(List(2, 4), List(4, 6))

/合二为一List flatMap*/
scala> q.flatMap(_.filter(_%2 == 0))
res27: List[Int] = List(2, 4, 4, 6)

4.reduceLeft 和foldLeft

Scala Immutable Collection(集合)_第2张图片

scala> a.reduceLeft((x,y) => x+y)
res28: Int = 10

scala> a.reduce(_+_)
res29: Int = 10

Scala Immutable Collection(集合)_第3张图片

foldLeft更通用

scala> a
res32: List[Int] = List(1, 2, 3, 4)

scala> a.foldLeft(0)(_+_)
res30: Int = 10

scala> a.foldLeft(1)(_+_)
res31: Int = 11

5.Range

1. range是个整数数列

scala> 1 to 10
res33: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4,5.6,,789, 10)

/*range 和 until 的区别 */
scala> 1 to 3
res34: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3)
scala> 1 until 3
res38: scala.collection.immutable.Range = Range(1, 2)

scala> 1 to 10 by 2 //步长为2
res35: scala.collection.immutable.Range = Range(1, 3, 5, 7, 9)

scala> (1 to 10 ).toList  //链表的形势表示
res37: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

2.Stream

Stream is a lazy List(惰性列表)

scala> 1 #:: 2 #:: 3#:: Stream.empty
res39: scala.collection.immutable.Stream[Int] = Stream(1, ?)
/*第一个是确定的后面的值需要时候才会求,按需求值*/
scala> val stream =(1 to 1000).toStream
stream: scala.collection.immutable.Stream[Int] = Stream(1, ?)
scala> stream.head
res40: Int = 1

scala> stream.tail
res41: scala.collection.immutable.Stream[Int] = Stream(2, ?)
/*惰性求值与range相比,优点就是需要用的时候才会显示出来不需要的时候不会全部列出来,防止溢出*/

3.Tuple(元组)

scala> (1,2)  //两个元素称之为pair(对)
res42: (Int, Int) = (1,2)

scala> 1-> 2
res42: (Int, Int) = (1,2)

/*也可以定义多个tuple*/
scala> val t = (1,"Alice","age","12")
t: (Int, String, String, String) = (1,Alice,age,12)

scala> t._1
res44: Int = 1

scala> t._2
res45: String = Alice

scala> t._3
res46: String = age

scala> t._4
res47: String = 12

/*需要返回多个值,可以封装在一个Tuple*/

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

scala> def sumSq(in :List[Int]):(Int,Int,Int)=
     | in.foldLeft((0,0,0))((t,v)=>(t._1 +1,t._2 + v,t._3 + v*v))
     /*元素长度,元素求和,元素平方和 */
sumSq: (in: List[Int])(Int, Int, Int)

scala> sumSq(a)
res48: (Int, Int, Int) = (3,6,14)

4.Map(k.v)
k=key v=value

scala> val p = Map(1-> "David",9->"Elwood")
p: scala.collection.immutable.Map[Int,String] = Map(1 -> David, 9 -> Elwood)

/*p取值valuep(key)*/
scala> p(1)
res49: String = David

scala> p(9)
res50: String = Elwood

/*判断values是否存在*/
scala> p.contains(1)
res51: Boolean = true


scala> p.contains(9)
res53: Boolean = true

scala> p.contains(2)
res54: Boolean = false

/*取出key*/
scala> p.keys
res55: Iterable[Int] = Set(1, 9)

/*取出values*/
scala> p.values
res56: Iterable[String] = MapLike(David, Elwood)
/*增加一个value用“+”*/
scala> p +(8-> "Bon")
res57: scala.collection.immutable.Map[Int,String] = Map(1 -> David, 9 -> Elwood, 8 -> Bon)

/*减少一个value用“-”*/
scala> p-1(p-key)
res58: scala.collection.immutable.Map[Int,String] = Map(9 -> Elwood)

/*增加多个value用“++”*/
scala> p ++ List(2->"Jane",5->"jack")
res59: scala.collection.immutable.Map[Int,String] = Map(1 -> David, 9 -> Elwood,
 2 -> Jane, 5 -> jack)

/*减少多个value用“--”*/
scala> p -- List(1,9,2)
res60: scala.collection.immutable.Map[Int,String] = Map()

/*增加和减少多个value可以同时用“++”“--”*/
scala>  p ++ List(2->"Jane",5->"jack") --List(9,1)
res61: scala.collection.immutable.Map[Int,String] = Map(2 -> Jane, 5 -> jack)

Scala快速排序案例

/*:paste用于编辑一大块的代码 */
scala> :paste
// Entering paste mode (ctrl-D to finish)

def qSort(a:List[Int]):List [Int] =
/*输入是一个List,输出是一个排好序Int类型的List */
if (a.length < 2)  a /*如果List只有一个元素或为空,排序就是本身*/
else
qSort(a.filter(_ < a.ahead)) ++ a.filter(_=a.head)++qsort(a.filter(_>a.head))

/*先对左半部分进行排序分2区,比a.head小的放a.head的左边*,然后再拼接比a.head大的放右边,++是拼接/

/*可以看出这是递归的过程*/

/*验证*/
scala> qSort(List(3,1,2))
res0: List[Int] = List(1, 2, 3)

scala> qSort(List(9,6,4,7,8,2,3,5,7))
res1: List[Int] = List(2, 3, 4, 5, 6, 7, 7, 8, 9)

补充阅读材料:
http://docs.scala-lang.org/overviews/collections/introduction.html

你可能感兴趣的:(Scala)