Scala中的队列
在scala中我们可以直接使用队列类型Queue,分为可变队列和不可变队列
一般俩说我们在开发中通常使用可变集合中的队列
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| import scala.collection.mutable
val q1 = new mutable.Queue[Int]
q1 += 9
q1 ++= List(8, 7, 6)
val quene = new mutable.Queue[Int]() quene += 0 quene += 1
quene.enqueue(12)
println(quene)
println(quene.head)
println(quene.last)
println(quene.tail)
|
Scala中的Set
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
val set = Set(1, 2, 3, 4, 5)
val setn = mutable.Set(1, 2, 3, 4, 5)
println(set)
val set1 = set + (6) val set2 = set + (6, 7, 8, 9) val set3 = set ++ List(11, 12, 13)
println(set.sum)
println(set.count(x => x > 3))
println(set.isEmpty)
println(set.drop(1))
println(set.diff(mutable.Set(3, 4, 5)))
setn.remove(4) println(setn)
for (s <- set) { println(s) }
|
Scala中的tuple
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| def main(args: Array[String]): Unit = { val tp = (1, 2, "hadoop", "scala") val tuple = new Tuple6(1, 2, 3, 4, 5, "hadoop") val t1 = 2 -> "hdfs" val t2 = 3 -> "hdfs" -> "mapreduce"
println(tuple._1) for (i <- 0 until tuple.productArity) { println(tuple.productElement(i)) }
val tuple1: (String, String) = new Tuple2("first", "second")
tuple1.swap
val tup = (("hadoop", 30), 89) println(tup._1._2) }
|
Scala中的Map
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| def main(args: Array[String]): Unit = { val map = Map("hadoop" -> 20, "scala" -> 30) val mmap: mutable.HashMap[String, Int] = new mutable.HashMap[String, Int]() println(map("hadoop")) println(map.getOrElse("kafka", 12))
mmap += ("hadoop" -> 12) mmap.remove("hadoop") println(mmap)
val map2 = Map("hadoop" -> 20, "scala" -> 30, "kafka" -> 23) println(map2.keys) println(map2.keySet) println(map2.values.toList) println(map2.get("hadoop")) println(map2.get("hadoop").get)
for ((k, v) <- map2) { println("key: " + k + " value: " + v) } for (key <- map2.keySet) { println("key: "+key+ " value: " + map2.get(key).get) } }
|
Scala 中的Some
Some 和 None 都是样例类,继承自Option,Option 里面的方法isEmpty() get() getOrElse() 等方法都可以使用。此外Option里面还有一些高阶函数如map、flatMap等,使用get 方法获取里面的具体值
Scala中的模式匹配
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| object MatchApp { def main(args: Array[String]): Unit = {
val courses = Array("Hadoop", "HDFS", "MapReduce", "Spark", "Flink") val course = courses(Random.nextInt(courses.length)) course match { case "Hadoop" => println("Hadoop") case "HDFS" => println("HDFS") case "Spark" => println("Spark") case _ => println("Other") }
def matchArray(array: Array[String]) = { array match { case Array("Hadoop") => println("Hadoop") case Array(x, y) => println("Hi: " + x + " , " + y) case Array("Hadoop", _*) => println("匹配动态参数") case _ => println("Default") } }
def matchList(array: List[String]) = { array match { case "Hadoop" :: Nil => println("match single") case x :: y :: Nil => println("match two") case "Hadoop" :: tail => println("match start with Hadoop") case _ => println("Others") } }
def matchType(obj: Any) = { obj match { case i: Int => println("Int") case l: String => println("Stirng") case m: Map[_, _] => println("Map") case _ => println("Other") } }
val grades = Map("Hadoop" -> "A", "Spark" -> "B", "Flink" -> "C")
def matchGrade(name: String) = { val grade = grades.get(name) grade match { case Some(grade) => println("your grade is " + grade) case None => println("Error") } } try { val i = 1 / 0 } catch { case e: ArithmeticException => println("除数不能为0") case e: Exception => println(e.getMessage) }
} }
|
Scala中下划线的使用
Scala中的匿名函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| def sayHello(name: String) = { println("hello:\t" + name) }
val say = sayHello _ val say2 = sayHello(_)
{ (x: Int) => x + 2 }
|