Scala 学习笔记之集合(5)】的更多相关文章

建立一个Java类,为了演示Java集合类型向Scala集合的转换: import java.util.ArrayList; import java.util.List; public class School { public List<String> ls = null; public School() { ls = new ArrayList<String>(); ls.add("abc"); ls.add("efg"); } } im…
scala> 1 to 10 res9: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) scala> List(1,2,3,4) res10: List[Int] = List(1, 2, 3, 4) scala> (1 to 10).map(_*3).filter(_%2==1).reduce(_+_) res8: Int = 75 试一试map scala> (…
package com.citi.scala object CollectionDemo { def main(args: Array[String]): Unit = { /** * List */ println("--------------------------List-------------------------") val numbers = List[Int](11, 22, 33, 44, 55) val numbers1 = List(11, 22, 33, 4…
object CollectionDemo8 { def main(args: Array[String]): Unit = { //Option集合的使用,可以用来安全的判断null或非null,放心使用,不会抛出空指针异常 val s: String = "abcd" val sp = Option(s) println(sp.isDefined) println(sp.isEmpty) if (sp == Some("abcd")) { println(&qu…
object CollectionDemo7 { def main(args: Array[String]): Unit = { //数组使用 val arr = Array("red", "blue", "yellow") arr(0) = "white" for(el <- arr){println(el)} //用Seq构建List println(Seq("red", "blue&q…
import collection.mutable.Buffer object CollectionDemo6 { def main(args: Array[String]): Unit = { //可变集合类型推导 val nums = Buffer(1) for (i <- 2 to 10) nums += i println(nums) //可变集合类型定义 val nums2 = Buffer[Int]() for (i <- 1 to 10) nums2 += i println(n…
集合的模式匹配操作: object CollectionDemo5 { def main(args: Array[String]): Unit = { //集合模式匹配1 val ls = List(1, 2, 3, 4, 5) println( ls match { case List(1, 2, 3) => "List(1,2,3)" case List(1, 2, 3, 4) => "List(1,2,3,4)" case List(1, 2, 3…
object CollectionDemo10 { def main(args: Array[String]): Unit = { var ls = List[Int](1, 2, 3) //向后增加元素 ls = ls :+ 4 //向前增加元素 ls = 0 +: ls //基本等价 ls = -1 :: ls //向前增加集合 ls = List(-2) ++: ls //基本等价 ls = List(-2) ::: ls //向后增加集合 ls = ls ++ List(5) print…
import util._ import concurrent.ExecutionContext.Implicits.global import concurrent.Future import concurrent.duration._ object CollectionDemo9 { def main(args: Array[String]): Unit = { //Try捕捉异常 println(Try(10 / 0)) println(Try(10).flatMap { x => Try…
class StudentTT extends StudentT{ def sayBye(name: String, age: Int)(address: String){ println("Hello, " + name + ":" + age + ":" + address) } } object CollectionDemo3 { def main(args: Array[String]): Unit = { val s = new Stu…