【收藏】Scala常用方法(笔记)
一、lazy关键字
class Scala001 { //类里面不能直接声明Main方法。 } //在Scala中用lazy定义的变量叫:惰性变量,会实现延迟加载。 //惰性变量:只能是不可变的变得变量.且只有在调用惰性变量时,才会实例化这个变量。 object Scala001{ def init():Unit = { println("scala init") } def main(args: Array[String]): Unit = { val property = init() //没有用lazy修饰的 println("after init") println(property) } } object Scala0012{ def init():Unit = { println("scala init") } def main(args: Array[String]): Unit = { lazy val property = init() //没有用lazy修饰的 println("after init") println(property) } }
二、常用的方法
object Scala002 { /** * * @param args * 快捷键: * Ctrl+Alt+V:自动补全声明 * Ctrl+Alt+L:格式化代码 * Ctrl+Shift+F10 运行 * */ def main(args: Array[String]): Unit = { //1,新建一个List集合 val list01 = List(4, 6, 5, 1, 3, 2, 7, 8, 0, 9) //2,将list01中的每一个数字*2,生成一个新的集合 val list02 = list01.map(_ * 2) //3. 将list01中的偶数取出来形成一个新的集合 .filter中存放Boolean val list03 = list01.filter(_ % 2 == 0) //4.将list01 排序形成一个新的集合 val list04 = list01.sorted //5.反转排序顺序 val list05 = list04.reverse //6.list01中的元素4个一组,类型为Iterator[list[Int]] val iterator01 = list01.grouped(4).toBuffer //7.将iterator01转换成list val list06 = iterator01.toList //8.将多个list压扁成一个list val list07 = list06.flatten val lines = List("hello world hello scala", "hello python", "hello scala") //9,先按空格区分,再压平 val list08_old = lines.map(_.split(" ")).flatten //旧方式 val list08_new = lines.flatMap(_.split(" ")) //新方式 //10.并行计算求和 val sum_old = list01.sum //非并行 val sum_new = list01.par.sum //并行。和线程有关,每个线程计算一部分。 //11.按照特定的方法进行聚合即(1+2+3+......+10) val sum_order = list01.reduce(_ + _) val sum_order_left = list01.reduceLeft(_ + _) //从左往右 val sum_order_right = list01.reduceRight(_ + _) //从右往左 //12.使用并行方式进行聚合 val sum_par_order = list01.par.reduce(_ + _) //13.折叠,有初始值(无特定顺序)--先添加初始值,再进行聚合 val sum_fold = list01.fold(10)(_ + _) //13.折叠,有初始值(有特定顺序) val sum_fold_left = list01.foldLeft(10)(_ + _) val sum_fold_right = list01.foldRight(10)(_ - _) //14.聚合(对于多个List集合的情况)--使用上面的list06测试 val sum_flatten = list06.flatten.reduce(_ + _) //--先合并,后聚合 //解释一下第一个_+_.sum 表示初始值+第一个list集合的sum(聚合)然后进行循环相加。第二个_+_就是将各个list集合相加得到最后的结果。 val sum_aggregate = list06.aggregate(0)(_+_.sum,_+_) val list10 = List(1,2,3,4,10,11,12) //15.并集 val list11 = list10 union list01 //16.交集 val list12 = list10 intersect list01 //17.差集 val list13 = list10 diff list01 //打印 println("list01 : " + list01) println("list02 : " + list02) println("list03 : " + list03) println("list04 : " + list04) println("list05 : " + list05) println("iterator01 : " + iterator01) println("list06 : " + list06) println("list07 : " + list07) println("list08_old : " + list08_old) println("list08_new : " + list08_new) println("sum_old : " + sum_old) println("sum_new : " + sum_new) println("sum_order : " + sum_order) println("sum_order_left : " + sum_order_left) println("sum_order_right : " + sum_order_right) println("sum_par_order : " + sum_par_order) println("13,sum_fold_left : " + sum_fold_left) println("13,sum_fold_right : " + sum_fold_right) println("14,sum_flatten : " + sum_flatten) println("14,sum_aggregate : " + sum_aggregate) println("14,sum_aggregate : " + sum_aggregate) println("15,list11 : " + list11) println("16,list12 : " + list12) println("17,list13 : " + list13) } }
运行结果:
D:\JDK\bin\java.exe "-javaagent:E:\IDEA\IntelliJ IDEA 2018.2.5\lib\idea_rt.jar=49213:E:\IDEA\IntelliJ IDEA 2018.2.5\bin" -Dfile.encoding=UTF-8 -classpath D:\JDK\jre\lib\charsets.jar;D:\JDK\jre\lib\deploy.jar;D:\JDK\jre\lib\ext\access-bridge-64.jar;D:\JDK\jre\lib\ext\cldrdata.jar;D:\JDK\jre\lib\ext\dnsns.jar;D:\JDK\jre\lib\ext\jaccess.jar;D:\JDK\jre\lib\ext\jfxrt.jar;D:\JDK\jre\lib\ext\localedata.jar;D:\JDK\jre\lib\ext\nashorn.jar;D:\JDK\jre\lib\ext\sunec.jar;D:\JDK\jre\lib\ext\sunjce_provider.jar;D:\JDK\jre\lib\ext\sunmscapi.jar;D:\JDK\jre\lib\ext\sunpkcs11.jar;D:\JDK\jre\lib\ext\zipfs.jar;D:\JDK\jre\lib\javaws.jar;D:\JDK\jre\lib\jce.jar;D:\JDK\jre\lib\jfr.jar;D:\JDK\jre\lib\jfxswt.jar;D:\JDK\jre\lib\jsse.jar;D:\JDK\jre\lib\management-agent.jar;D:\JDK\jre\lib\plugin.jar;D:\JDK\jre\lib\resources.jar;D:\JDK\jre\lib\rt.jar;E:\scala02\target\classes;E:\Scala\scala-2.10.4\lib\scala-actors-migration.jar;E:\Scala\scala-2.10.4\lib\scala-actors.jar;E:\Scala\scala-2.10.4\lib\scala-library.jar;E:\Scala\scala-2.10.4\lib\scala-reflect.jar;E:\Scala\scala-2.10.4\lib\scala-swing.jar;E:\Scala\scala-2.10.4\src\scala-actors-src.jar;E:\Scala\scala-2.10.4\src\scala-library-src.jar;E:\Scala\scala-2.10.4\src\scala-reflect-src.jar;E:\Scala\scala-2.10.4\src\scala-swing-src.jar Scala002 list01 : List(4, 6, 5, 1, 3, 2, 7, 8, 0, 9) list02 : List(8, 12, 10, 2, 6, 4, 14, 16, 0, 18) list03 : List(4, 6, 2, 8, 0) list04 : List(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) list05 : List(9, 8, 7, 6, 5, 4, 3, 2, 1, 0) iterator01 : ArrayBuffer(List(4, 6, 5, 1), List(3, 2, 7, 8), List(0, 9)) list06 : List(List(4, 6, 5, 1), List(3, 2, 7, 8), List(0, 9)) list07 : List(4, 6, 5, 1, 3, 2, 7, 8, 0, 9) list08_old : List(hello, world, hello, scala, hello, python, hello, scala) list08_new : List(hello, world, hello, scala, hello, python, hello, scala) sum_old : 45 sum_new : 45 sum_order : 45 sum_order_left : 45 sum_order_right : 45 sum_par_order : 45 13,sum_fold_left : 55 13,sum_fold_right : 3 14,sum_flatten : 45 14,sum_aggregate : 45 14,sum_aggregate : 45 15,list11 : List(1, 2, 3, 4, 10, 11, 12, 4, 6, 5, 1, 3, 2, 7, 8, 0, 9) 16,list12 : List(1, 2, 3, 4) 17,list13 : List(10, 11, 12) Process finished with exit code 0