集合
List 是不可变的
scala> List
res26: scala.collection.immutable.List.type = scala.collection.immutable.List$@6ada0261
scala> val list = List(1,2,3,4)
list: List[Int] = List(1, 2, 3, 4)
scala> list.head
res27: Int = 1
scala> list.tail
res28: List[Int] = List(2, 3, 4)
注意这里的 ::
scala> val list2=0::list
list2: List[Int] = List(0, 1, 2, 3, 4)
scala> def decorator(l:List[Int],prefix :String){
| if(l != Nil ){
| println(prefix+l.head)
| decorator(l.tail,prefix)
| }
| }
decorator: (l: List[Int], prefix: String)Unit
scala> decorator(List(1,2,3,4,5),"--")
--1
--2
--3
--4
--5
----------------------------------
LinkedList
首个元素 .elem
之后的元素 .next
scala> var linkedList = scala.collection.mutable.LinkedList(1, 2, 3, 4, 5)
<console>:11: warning: object LinkedList in package mutable is deprecated (since 2.11.0): low-level linked lists are deprecated
var linkedList = scala.collection.mutable.LinkedList(1, 2, 3, 4, 5)
^
linkedList: scala.collection.mutable.LinkedList[Int] = LinkedList(1, 2, 3, 4, 5)
scala> linkedList
res0: scala.collection.mutable.LinkedList[Int] = LinkedList(1, 2, 3, 4, 5)
scala> linkedList.elem
res1: Int = 1
scala> linkedList.head
res2: Int = 1
scala> linkedList.next
res3: scala.collection.mutable.LinkedList[Int] = LinkedList(2, 3, 4, 5)
scala> linkedList.tail
res4: scala.collection.mutable.LinkedList[Int] = LinkedList(2, 3, 4, 5)
while 循环 对每个元素 放大2倍
<console>:11: warning: object LinkedList in package mutable is deprecated (since 2.11.0): low-level linked lists are deprecated
var linkedList = scala.collection.mutable.LinkedList(1, 2, 3, 4, 5)
^
linkedList: scala.collection.mutable.LinkedList[Int] = LinkedList(1, 2, 3, 4, 5)
scala> var curr = linkedList
curr: scala.collection.mutable.LinkedList[Int] = LinkedList(1, 2, 3, 4, 5)
scala> while(curr!= Nil){
| curr.elem = curr.elem *2
| curr= curr.next
| }
scala> curr
res6: scala.collection.mutable.LinkedList[Int] = LinkedList()
scala> linkedList
res7: scala.collection.mutable.LinkedList[Int] = LinkedList(2, 4, 6, 8, 10)
while 循环 对每隔元素 放大2倍
scala> var linkedList = scala.collection.mutable.LinkedList(1, 2, 3, 4, 5,6,7,8,9,10)
<console>:11: warning: object LinkedList in package mutable is deprecated (since 2.11.0): low-level linked lists are deprecated
var linkedList = scala.collection.mutable.LinkedList(1, 2, 3, 4, 5,6,7,8,9,10)
^
linkedList: scala.collection.mutable.LinkedList[Int] = LinkedList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
scala> var curr = linkedList
curr: scala.collection.mutable.LinkedList[Int] = LinkedList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
scala> while(curr!= Nil){
| curr.elem = curr.elem *2
| curr= curr.next.next
| }
scala> linkedList
res1: scala.collection.mutable.LinkedList[Int] = LinkedList(2, 2, 6, 4, 10, 6, 14, 8, 18, 10)
----------------------------------
Set 是不可变的
scala> Set
res2: scala.collection.immutable.Set.type = scala.collection.immutable.Set$@4264beb8
scala> val set = Set(1,1,2,2,2,2,3,4)
set: scala.collection.immutable.Set[Int] = Set(1, 2, 3, 4)
LinkedHashSet 会维护添加的顺序
scala> val s = scala.collection.mutable.LinkedHashSet[Int]()
s: scala.collection.mutable.LinkedHashSet[Int] = Set()
scala> s += 1
res6: s.type = Set(1)
scala> s += 3
res7: s.type = Set(1, 3)
scala> s += 5
res8: s.type = Set(1, 3, 5)
scala> s += 4
res9: s.type = Set(1, 3, 5, 4)
SortedSet 会自动给元素排序
scala> val s= scala.collection.mutable.SortedSet[String]()
s: scala.collection.mutable.SortedSet[String] = TreeSet()
scala> s += "d"
res10: s.type = TreeSet(d)
scala> s += "c"
res11: s.type = TreeSet(c, d)
scala> s += "b"
res12: s.type = TreeSet(b, c, d)
scala> s += "a"
res13: s.type = TreeSet(a, b, c, d)
----------------------------------
集合的函数编程
使用map 给list 每个元素添加前缀
scala> val list = List("a","b","c","d")
list: List[String] = List(a, b, c, d)
scala> list.map("+"+ _)
res14: List[String] = List(+a, +b, +c, +d)
flatMap 拆分 打平 为多个元素的集合
scala> val list = List("hello world","hello java","hello scala","hello hadoop")
list: List[String] = List(hello world, hello java, hello scala, hello hadoop)
scala> val list2 = list.flatMap(_.split(" "))
list2: List[String] = List(hello, world, hello, java, hello, scala, hello, hadoop)
scala> list2
res19: List[String] = List(hello, world, hello, java, hello, scala, hello, hadoop)
scala> list
res20: List[String] = List(hello world, hello java, hello scala, hello hadoop)
foreach遍历
scala> list.foreach(println(_))
hello world
hello java
hello scala
hello hadoop
zip 拉链操作
scala> val list1 = List(1,2,3,4)
list1: List[Int] = List(1, 2, 3, 4)
scala> val list2 = List("a","b","c","d")
list2: List[String] = List(a, b, c, d)
scala> list1.zip(list2)
res21: List[(Int, String)] = List((1,a), (2,b), (3,c), (4,d))
scala> val aa = Array(10,2,3)
aa: Array[Int] = Array(10, 2, 3)
scala> aa.reduceLeft(_ - _)
res14: Int = 5
注意这个 reduceRight 相当于 10 - ( 2 - 3)
scala> aa.reduceRight(_ - _)
res15: Int = 11
----------------------------------