分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
Scala的集合有三大类:序列Seq、集Set、映射Map,所有的集合都扩展自Iterable特质
在Scala中集合有可变(mutable)和不可变(immutable)两种类型,immutable类型的集合初始化后就不能改变了(注意与val修饰的变量进行区别)
不可变的序列 import scala.collection.immutable._
在Scala中列表要么为空(Nil表示空列表)要么是一个head元素加上一个tail列表。
9 :: List(5, 2) :: 操作符是将给定的头和尾创建一个新的列表
注意::: 操作符是右结合的,如9 :: 5 :: 2 :: Nil相当于 9 :: (5 :: (2 :: Nil))
package cn.toto.scala/** * Created by toto on 2017/6/28. */object ImmutListDemo { def main(args: Array[String]): Unit = { //创建一个不可变的集合 val lst1 = List(1,2,3) println(lst1) //将0插入到lst1的前面生成一个新的List val lst2 = 0::lst1 println(lst2) val lst3 = lst1.::(0) println(lst3) val lst4 = 0 +: lst1; println(lst4) val lst5 = lst1.+:(0) println(lst5) //将一个元素添加到list1的后面产生一个新的集合 val lst6 = lst1 :+3 val lst0 = List(4,5,6) //将2个list合并成一个新的List val lst7 = lst1 ++ lst0 println(lst7) //将lst0插入到lst1前面生成一个新的集合 val lst9 = lst1.:::(lst0) println(lst9) }}
运行结果:
List(1, 2, 3)List(0, 1, 2, 3)List(0, 1, 2, 3)List(0, 1, 2, 3)List(0, 1, 2, 3)List(1, 2, 3, 4, 5, 6)List(4, 5, 6, 1, 2, 3)
可变的序列 import scala.collection.mutable._
package cn.toto.scalaimport scala.collection.mutable.ListBuffer/** * Created by toto on 2017/6/28. */object MuListDemo { def main(args: Array[String]): Unit = { //构建一个可变列表,初始有3个元素,1,2,3 val lst0 = ListBuffer[Int](1,2,3) println(lst0) //创建一个空的可变列表 val lst1 = new ListBuffer[Int] //向lst1中追加元素,注意:没有生成新的集合 lst1 += 4 println(lst1) lst1.append(5) println(lst1) //将lst1中的元素最近到lst0中,注意:没有生成新的集合 lst0 ++= lst1 println(lst0) //将lst0 和 lst1合并成一个新的ListBuffer,注意:生成一个集合 val lst2 = lst0 ++ lst1 println(lst2) //将元素追加到lst0的后面生成一个新的集合 val lst3 = lst0 :+ 5 println(lst3) }}
运行后的结果如下:
ListBuffer(1, 2, 3)ListBuffer(4)ListBuffer(4, 5)ListBuffer(1, 2, 3, 4, 5)ListBuffer(1, 2, 3, 4, 5, 4, 5)ListBuffer(1, 2, 3, 4, 5, 5)
其它案例:
package cn.toto.scala/** * Created by toto on 2017/6/28. */object ListTest { def main(args: Array[String]): Unit = { //创建一个List val lst0 = List(1,7,9,8,0,3,5,4,6,2) //将lst0中每个元素乘以10后生成一个新的集合 val lst1 = lst0.map(x => x * 2) //结果是:List(2, 14, 18, 16, 0, 6, 10, 8, 12, 4) println(lst1) //将lst0中的偶数取出来生成一个新的集合 val lst2 = lst0.filter(x => x % 2 == 0) //运行结果是:List(8, 0, 4, 6, 2) println(lst2) //将lst0排序后生成一个新的集合 val lst3 = lst0.sorted //运行结果:List(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) println(lst3) val lst4 = lst0.sortBy(x => x) //运行结果:List(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) println(lst4) val lst5 = lst0.sortWith((x,y) => x
不可变的Set
package cn.toto.collectimport scala.collection.immutable.HashSetobject ImmutSetDemo extends App{ val set1 = new HashSet[Int]() //将元素和set1合并生成一个新的set,原有set不变 val set2 = set1 + 4 //set中元素不能重复 val set3 = set1 ++ Set(5, 6, 7) val set0 = Set(1,3,4) ++ set1 println(set0.getClass)}
可变的Set
package cn.toto.scalaimport scala.collection.mutable/** * Created by toto on 2017/6/28. */object MutSetDemo { def main(args: Array[String]): Unit = { //创建一个可变的HashSet val set1 = new mutable.HashSet[Int](); //向HashSet中添加元素 set1 += 2 //add等价于+= set1.add(4) println(set1) set1 ++= Set(1,3,5) println(set1) //删除一个元素 set1 -= 5 println(set1) set1.remove(2) println(set1) }}
运行结果:
Set(2, 4)Set(1, 5, 2, 3, 4)Set(1, 2, 3, 4)Set(1, 3, 4)
package cn.toto.scalaimport scala.collection.mutable/** * Created by toto on 2017/6/28. */object MutMapDemo { def main(args: Array[String]): Unit = { val map1 = new mutable.HashMap[String,Int](); //向map中添加数据 map1("spark") = 1 map1 += (("hadoop",2)) map1.put("storm",3) println(map1) //从map中移除元素 map1 -= "spark" map1.remove("hadoop") println(map1) }}
运行后的结果如下:
Map(hadoop -> 2, spark -> 1, storm -> 3)Map(storm -> 3)
其它零碎的内容:
下面的代码是在命令提示符上输入的内容:
scala> def m(x:Int,y:Int):Int={ x + y }m: (x: Int, y: Int)Intscala> m(1,2)res0: Int = 3scala> (x:Int,y:Int) => x + yres1: (Int, Int) => Int = $$Lambda$1044/698062929@191a0351scala> val f = (x:Int,y:Int) => x + yf: (Int, Int) => Int = $$Lambda$1045/1726169577@5b48f0f4scala> val f1 :(Int,Int) => Int = {(x,y) => x + y}f1: (Int, Int) => Int = $$Lambda$1046/535361000@39449465scala> f1(1,2)res2: Int = 3scala> Array(1,2,3,4,5,6,7,8,9,10)res3: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)scala> val arr = Array(1,2,3,4,5,6,7,8,9,10)arr: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)scala> arr.map(_ * 10)res4: Array[Int] = Array(10, 20, 30, 40, 50, 60, 70, 80, 90, 100)scala> arrres6: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)scala> arr.map((x:Int) => x * 10)res7: Array[Int] = Array(10, 20, 30, 40, 50, 60, 70, 80, 90, 100)scala> arr.map(x => x * 10)res8: Array[Int] = Array(10, 20, 30, 40, 50, 60, 70, 80, 90, 100)scala> val f2 = (x:Int) => x * 10f2: Int => Int = $$Lambda$1172/1943764464@44a485bcscala> arr.map(f2)res9: Array[Int] = Array(10, 20, 30, 40, 50, 60, 70, 80, 90, 100)scala> def m1(x:Int):Int = x * 10m1: (x: Int)Intscala> arr.map(m1)res10: Array[Int] = Array(10, 20, 30, 40, 50, 60, 70, 80, 90, 100)scala> arr.sortBy(x => x)res11: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)#成了按照字符串进行排序了。scala> arr.sortBy(x => x + "")res12: Array[Int] = Array(1, 10, 2, 3, 4, 5, 6, 7, 8, 9)scala> arr.fold(0)(_+_)res13: Int = 55scala> arr.reduce(_+_)res14: Int = 55scala> arr.fold(10)(_+_)res15: Int = 65scala> arr.parres16: scala.collection.parallel.mutable.ParArray[Int] = ParArray(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)#这里相当于是使用多个线程进行处理的scala> arr.par.sumres17: Int = 55聚合scala> val arr = List(List(1,2,3),List(3,4,5),List(2),List(0))arr: List[List[Int]] = List(List(1, 2, 3), List(3, 4, 5), List(2), List(0))这里的a代表的是0,b代表的是上面的List中的每个list,(x,y) => x + y表示给b.sum中的结果进行求和scala> arr.aggregate(0)((a,b)=> a + b.sum,(x,y) => x + y)res0: Int = 20scala>