Scala深入浅出进阶经典 第82讲:Scala中List的ListBuffer是如何实现高效的遍历计算的

来源:互联网 发布:上海美工培训班 编辑:程序博客网 时间:2024/06/05 08:42
package com.dt.scalaInAction.demo_082/** * Scala中List的构造是的类型约束逆变、协变、下界详解 */object ListBuffer_Internals {    def main(args: Array[String]): Unit = {        val list = List(1, 2, 3, 4, 5, 6, 7, 8, 9)        println(increment(list))        println(increment_MoreEffective(list))        println(increment_MostEffective(list))    }        def increment(list: List[Int]): List[Int] = list match {        case List() => List()        case head :: tail => head+1 :: increment(tail)    }        def increment_MoreEffective(list: List[Int]): List[Int] = {        var result = List[Int]()        for(e <- list) result = result ::: List(e+1)        result    }        def increment_MostEffective(list: List[Int]): List[Int] = {        import scala.collection.mutable.ListBuffer        var buffer = new ListBuffer[Int]        for(e <- list) buffer += e + 1        buffer.toList    }    }


以上内容是从王家林老师DT大数据课程第82讲的学习笔记和个人整理。
DT大数据微信公众账号:DT_Spark
王家林老师QQ:1740415547
王家林老师微信号:18610086859
第82讲视频网站地址:http://pan.baidu.com/s/1pJKdKqn


0 0
原创粉丝点击