Scala的List


点击(此处)折叠或打开

  1. def main(args: Array[String]){
  2.     
  3.     val bigData = List("Hadoop" , "Spark")
  4.     val data = List(1 , 2 , 3)
  5.     
  6.     val bigData_Core = "Hadoop" :: ("Spark" :: Nil) //List("Hadoop" , "Spark")定义方式结果一样
  7.     val data_Int = 1 :: 2 :: 3 :: Nil
  8.     
  9.     println(data.isEmpty)
  10.     println(data.head)
  11.     println(data.tail.head)//第二个元素
  12.     
  13.     val List(a,b) = bigData
  14.     println("a : " + a + " === " + " b: " + b)
  15.     val x :: y :: rest = data //rest是剩下的元素所组成的一个子List
  16.     println("x : " + x + " === " + " y: " + y + " === " + rest )
  17.     
  18.     val shuffledData = List(6,3,5,6,2,9,1)
  19.     println(sortList(shuffledData))
  20.     
  21.     def sortList(list : List[Int]): List[Int] = list match{
  22.       case List() => List()
  23.       case head :: tail => compute (head, sortList(tail))
  24.     }
  25.    
  26.     //递归排序步骤:
  27.     //sortList递归到最底层时,head是1,tail是Nil,compute(1, Nil)时返回List<1>
  28.     //往上,compute(9, List<1>),结果:List<1,9>
  29.     //往上,compute(2, List<1,9>),结果:List<1,2,9>
  30.     //...

  31.     def compute(data : Int , dataSet : List[Int]) : List[Int] = dataSet match{
  32.       case List() => List(data)
  33.       case head :: tail => if (data <= head) data :: dataSet
  34.         else head :: compute(data, tail)
  35.     }
  36.     
  37.   }

List的一阶函数操作

点击(此处)折叠或打开

  1. def main(args: Array[String]) {
  2.     println(List (1,2,3,4) ::: List (4,5,6,7,8) ::: List (10,11)) //将多个List连接在一起(两个冒号是元素与元素、元素与列表的连接)
  3.     println(List (1,2,3,4) ::: (List (4,5,6,7,8) ::: List (10,11)))
  4.     println(List (1,2,3,4).length) //该方法比较慢,尽量不用
  5.     
  6.     val bigData = List("Hadoop" , "Spark" , "Kafka")
  7.     println(bigData.last) //kafka
  8.     println(bigData.init) //除了最后一个元素之外的所有元素集合,这里是List("Hadoop", "Spark")
  9.     println(bigData.reverse)//产生新的List,结果是List("Kafka", "Spark", "Hadoop")
  10.     println(bigData) //还是原来的List("Hadoop", "Spark", "Kafka")
  11.     println(bigData take 2) //取前两个元素,List("Hadoop", "Spark")
  12.     println(bigData drop 2) //去掉前两个元素,List("kafka")
  13.     println(bigData splitAt 2) //将前两个元素一组,其余元素一组进行划分,(List("Hadoop", "Spark"), List("Kafka"))
  14.     println(bigData apply 2) //访问index=2的元素,跟下面的结果一样不常用
  15.     println(bigData(2))
  16.     
  17.     val data=List('a' ,'b', 'c', 'd', 'e', 'f')
  18.     println(data.indices) //返回所有元素的索引,Range(0,1,2,3,4,5)
  19.     println(data.indices zip data) //将两组元素配对,Vector((0,a),(1,b),(2,c),(3,d),(4,e),(5,f))
  20.     println(data.zipWithIndex)//将元素与索引配对更简捷写法,List((0,a),(1,b),(2,c),(3,d),(4,e),(5,f))
  21.     println(data.toString)//List('a' ,'b', 'c', 'd', 'e', 'f')
  22.     println(data.mkString ("[", ",,", "]"))//将集合按照一定样式输出,[a,,b,,c,,d,,e,,f]
  23.     println(data.mkString ("******"))//一个参数指分隔符,a******b******c******d******e******f
  24.     println(data mkString)//输出集合元素,abcdef
  25.     
  26.     val buffer = new StringBuilder
  27.     data addString (buffer, "(", ";;", ")")//跟mkString结果一样,只不过是将结果放到buffer中(a;;b;;c;;d;;e;;f)
  28.     println(buffer)
  29.     println(data) //List(a, b, c, d, e, f)
  30.   
  31.     val array = data.toArray //将List转化为数组
  32.     println(array.toList) //将数组转化为List,List(a, b, c, d, e, f)
  33.     
  34.     val new_Array = new Array[Char](10)
  35.     data.copyToArray(new_Array,3) //将List拷贝至数组,并将数组的前三个位置空出来
  36.     new_Array.foreach(print)
  37.     println
  38.     
  39.     val iterator = data.toIterator //返回List的迭代器
  40.     println(iterator.next) //a
  41.     println(iterator.next) //b
  42.   }

归并排序:

点击(此处)折叠或打开

  1. object MergedSort {

  2.   def main(args: Array[String]){
  3.      
  4.     def mergedsort[T] (less: (T, T) => Boolean) (input: List[T]): List[T] = {
  5.     
  6.       /**
  7.      * @param xList 要合并的有序列表
  8.      * @param yList 要合并的有序列表
  9.      * @return 合并后的列表
  10.      */
  11.     def merge(xList: List[T], yList: List[T]): List[T] =
  12.       (xList, yList) match {
  13.       case (Nil, _) => yList
  14.       case (_, Nil) => xList
  15.       case (x :: xtail, y :: ytail) => //比较两个List的head,谁小谁放前面,剩下两个List再递归merge
  16.         if (less(x, y)) x :: merge(xtail, yList)
  17.         else y :: merge(xList, ytail)
  18.     }
  19.     
  20.     val n = input.length / 2
  21.     if (n == 0) input
  22.     else {
  23.       val (x, y) = input splitAt n //把要排序的列表input平均分成两个列表
  24.       merge(mergedsort(less)(x), mergedsort(less)(y)) //先对分后的两个列表归并排序,再对排好的有序表进行归并
  25.     }
  26.   }
  27.     println(mergedsort((x: Int, y: Int) => x < y) (List (3, 7, 9, 5)))
  28.     val reversed_mergedsort=mergedsort((x: Int, y: Int) => x > y) _
  29.     println(reversed_mergedsort(List(3, 7, 9, 5)))
  30.       
  31. }
  32. }

List的Map、flatMap、froeach、filter操作

点击(此处)折叠或打开

  1. def main(args: Array[String]) {
  2.     println(List(1, 2, 3, 4, 6) map (_ + 1)) //map函数作用于List中的每一个元素,结果:List(2, 3, 4, 5, 7)
  3.     val data = List("Scala", "Hadoop", "Spark")
  4.     println(data map (_.length)) //结果:List(5, 6, 5)
  5.     println(data map (_.toList.reverse.mkString)) //结果:List(alacS, poodaH, krapS)
  6.     
  7.     println(data.map(_.toList)) //结果:List(List(S, c, a, l, a), List(H, a, d, o, o, p), List(S, p, a, r, k))
  8.     println(data.flatMap(_.toList)) //将map操作产生的结果合并,产生新的集合,结果:List(S, c, a, l, a, H, a, d, o, o, p, S, p, a, r, k)
  9.     println(List.range(1, 10) flatMap (i => List.range(1, i) map (j => (i, j))))
  10.     
  11.     var sum = 0
  12.     List(1, 2, 3, 4, 5) foreach (sum += _) //对每个元素进行没有结果的函数操作
  13.     println("sum : " + sum)
  14.     
  15.     println(List(1, 2, 3, 4, 6, 7, 8, 9, 10) filter (_ % 2 ==0))
  16.     println(data filter (_.length == 5)) //过滤出符合要求的元素集合
  17.     
  18.     
  19.     println(List(1, 2, 3, 4, 5) partition (_ % 2 ==0)) //把元素按照某种条件分裂,结果:(List(2, 4),List(1, 3, 5))
  20.     println(List(1, 2, 3, 4, 5) find (_ % 2 ==0)) //找出集合中第一个满足条件的元素,结果:Some(2),因为有可能找不到,所以是Optional的子类Some
  21.     println(List(1, 2, 3, 4, 5) find (_ <=0)) //没有结果,所以是None
  22.     println(List(1, 2, 3, 4, 5) takeWhile (_ < 4))//获取符合条件的元素,结果:List(1, 2, 3)
  23.     println(List(1, 2, 3, 4, 5) dropWhile (_ < 4)) //把符合条件的元素去掉,结果:List(4, 5)
  24.     println(List(1, 2, 3, 4, 5) span (_ < 4)) //把元素按条件分裂成不同部分,结果:(List(1, 2, 3),List(4, 5))
  25.     
  26.     def hastotallyZeroRow(m: List[List[Int]]) = m exists (row => row forall (_ == 0))//forall,只有集合中所有元素都满足条件才返回true;exists,只要有一个元素满足条件就返回true
  27.     val m= List(List(1,0,0), List(0,0,0), List(0,0,1))
  28.     println(hastotallyZeroRow(m))
  29.     
  30.   }

foldLeft、foldRight、sortWith

点击(此处)折叠或打开

  1. def main(args: Array[String]){
  2.     println((1 to 5).foldLeft(0)(_+_) )//List从左往右,依次相加,0位初始值,0+1+2+3+4+5
  3.     println((0 /: (1 to 5))(_+_)) //也可以这么写:\:
  4.     
  5.     println((1 to 5).foldRight(1)(_-_)) //初始值为1,从右往左,依次相减5-1 = 4,然后4-4=0,3-0=3,2-3=-1,1-(-1)=2
  6.     println(((1 to 5):\1)(_-_))
  7.     
  8.     
  9.     println(List(1, -3, 4, 2, 6) sortWith (_ < _))
  10.     println(List(1, -3, 4, 2, 6) sortWith (_ > _))
  11.     
  12.   }

List伴生对象

点击(此处)折叠或打开

  1. def main(args: Array[String]) {
  2.     println(List.apply(1, 2, 3)) //返回一个List(1,2,3),底层调用toList方法
  3.     println(List.make(3, 5)) //构造一个具有重复元素的List,第一个参数是元素重复的次数,第二个参数是元素的内容,结果:List(5,5,5)
  4.     println(List.range(1, 5)) //生成一个List,左闭右开,List(1,2,3,4)
  5.     println(List.range(9, 1, -3))//指定步长,List(9,6,3)
  6.     
  7.     val zipped = "abcde".toList zip List(1, 2, 3, 4, 5)
  8.     println(zipped) //将两个List组合起来,List((a,1), (b,2), (c,3), (d,4), (e,5))
  9.     println(zipped.unzip) //和zip操作相反,结果:(List(a, b, c, d, e),List(1, 2, 3, 4, 5))
  10.     
  11.     println(List(List('a', 'b'), List('c'), List('d', 'e')).flatten)//将所有子List构成一个大的List,结果:List(a, b, c, d, e)
  12.     println(List.concat(List(), List('b'), List('c'))) //也是构成一个大的List,结果:List(b, c)
  13.  
  14.     println(List.map2(List(10, 20), List(10, 10)) (_ * _)) //将两个List进行函数操作,结果:List(100, 200)
  15.   
  16.   }

List继承体系分析以及方法操作源码解读

点击(此处)折叠或打开

  1. sealed abstract class List[+A] extends AbstractSeq[A] //sealed常用来进行模式匹配
  2.                                   with LinearSeq[A]
  3.                                   with Product
  4.                                   with GenericTraversableTemplate[A, List]
  5.                                   with LinearSeqOptimized[A, List[A]] {
  6.   override def companion: GenericCompanion[List] = List

  7.   import scala.collection.{Iterable, Traversable, Seq, IndexedSeq}

  8.   def isEmpty: Boolean
  9.   def head: A
  10.   def tail: List[A]

  11. ...
  12. }
*  This class comes with two implementing case classes `scala.Nil`
*  and `scala.::` that implement the abstract members `isEmpty`,
*  `head` and `tail`.
List有两个实现类,Nil与::
子对象Nil的定义:

点击(此处)折叠或打开

  1. case object Nil extends List[Nothing] { //空对象,全局唯一
  2.   override def isEmpty = true
  3.   override def head: Nothing =
  4.     throw new NoSuchElementException("head of empty list")
  5.   override def tail: List[Nothing] =
  6.     throw new UnsupportedOperationException("tail of empty list")
  7.   // Removal of equals method here might lead to an infinite recursion similar to IntMap.equals.
  8.   override def equals(that: Any) = that match {
  9.     case that1: scala.collection.GenSeq[_] => that1.isEmpty
  10.     case _ => false
  11.   }
  12. }
子类::的定义:

点击(此处)折叠或打开

  1. final case class ::[B](private var hd: B, private[scala] var tl: List[B]) extends List[B] {
  2.   override def head : B = hd
  3.   override def tail : List[B] = tl
  4.   override def isEmpty: Boolean = false

  5.   private def readObject(in: ObjectInputStream) {
  6.     val firstObject = in.readObject()
  7.     hd = firstObject.asInstanceOf[B]
  8.     assert(hd != ListSerializeEnd)
  9.     var current: ::[B] = this
  10.     while (true) in.readObject match {
  11.       case ListSerializeEnd =>
  12.         current.tl = Nil
  13.         return
  14.       case a =>
  15.         val list : ::[B] = new ::(a.asInstanceOf[B], Nil)
  16.         current.tl = list
  17.         current = list
  18.     }
  19.   }

  20.   private def writeObject(out: ObjectOutputStream) {
  21.     var xs: List[B] = this
  22.     while (!xs.isEmpty) { out.writeObject(xs.head); xs = xs.tail }
  23.     out.writeObject(ListSerializeEnd)
  24.   }
  25. }

List的构造是的类型约束逆变、协变、下界

点击(此处)折叠或打开

  1. abstract class Big_Data
  2. class Hadoop extends Big_Data
  3. class Spark extends Big_Data

  4. object List_Constructor_Internals {
  5.   def main(args: Array[String]){
  6.     val hadoop = new Hadoop :: Nil //等价于Nil.::(new Hadoop),结果:hadoopList[Hadoop]  = List(com.dt.scala.list.Hadoop@2666e815),new Hadoop为List的head,Nil为List 的tail
  7.     val big_Data = new Spark :: hadoop //等价于hadoop.::(new Spark),结果:big_Data:List[Big_data] = List(com.dt.scala.list.Spark@4e106082, com.dt.scala.list.Hadoop@2666e815)
  8.   }
  9.     
  10.     //::源码
  11.     def ::[B >: A] (x: B): List[B] =
        new scala.collection.immutable.::(x, this)

  12.     //看了::源码之后就会明白,往List[hadoop]里面.::(new Spark)怎么会返回List[Big_data]了
  13. }

ListBuffer高效遍历

点击(此处)折叠或打开

  1. object ListBuffer_Internals {
  2.     def main(args: Array[String]) {
  3.         val list = List(1,2,3,4,5,6,7,8,9)
  4.         increment(list)
  5.         increment_MoreEffective(list)
  6.         increment_MostEffective(list)
  7.     }

  8.     def increment(list: List[Int]): List[Int] = list match {
  9.         case List() => List()
  10.         case head :: tail => head + 1 :: increment(tail)
  11.     }

  12.     def increment_MoreEffective(list: List[Int]): List[Int] = {
  13.         var result = List[Int]()
  14.         for(element <- list) result = result ::: List(element +1)
  15.         result
  16.     }

  17.     def increment_MostEffective(list: List[Int]): List[Int] = {
  18.         import scala.collection.mutable.ListBuffer
  19.         var buffer = new ListBuffer[Int]
  20.         for(element <- list) buffer += element + 1
  21.         buffer.toList
  22.     }
  23. }

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/28912557/viewspace-1870473/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/28912557/viewspace-1870473/

你可能感兴趣的:(scala,大数据)