标签:
接着昨天的list,也是学习集合的相关知识
1.ListBuffer、ArrayBuffer代码实现:ListBuffer与ArrayBuffer都是mutable可变的与java中的定义也差不多,可以追加的。
2.Queue、Stack相关操作:队列是一端进去一端出来,而stack也是后进先出的原则,他们都是mutable可变的
import scala.collection.mutable.ListBuffer val listBuffer = new ListBuffer[Int] listBuffer += 1 listBuffer += 2 println(listBuffer) //ArrayBuffer是可变的 import scala.collection.mutable.ArrayBuffer val arrayBuffer = new ArrayBuffer[Int]() arrayBuffer += 1 arrayBuffer += 2 println(arrayBuffer) //队列是先入先出 也是可变的 val empty = Queue[Int]() val queue1 = empty.enqueue(1) val queue2 = queue1.enqueue(List(2,3,4,5)) println(queue2) val (element, left) = queue2.dequeue println(element + " : " + left) import scala.collection.mutable.Queue val queue = Queue[String]() queue += "a" queue ++= List("b", "c") println(queue) println(queue.dequeue) println(queue) //栈也是可变的,先进后出 import scala.collection.mutable.Stack val stack = new Stack[Int] stack.push(1) stack.push(2) stack.push(3) println(stack.top) println(stack) println(stack.pop) println(stack)
1.Set、Map相关操作:set与Map的元素都是mutable可变的也是无序的
2.TreeSet、TreeMap相关操作:treeMap与treeSet都是可以用来排序的
import scala.collection.mutable import scala.collection.mutable.TreeSet import scala.collection.immutable.TreeMap import scala.collection.immutable.HashMap object Set_Map { def main(args: Array[String]) { //set是可变的并且无序的 val data = mutable.Set.empty[Int] data ++= List(1, 2, 3) data += 4; data --= List(2, 3); println(data) data += 1; println(data) data.clear println(data) //map也是可变的无序的 向map中放值map(key)=value val map = mutable.Map.empty[String, String] map("Java") = "Hadoop" map("Scala") = "Spark" println(map) println(map("Scala")) //treeSet用来排序 val treeSet = TreeSet(9, 3, 1, 8, 0, 2, 7, 4, 6, 5) println(treeSet) val treeSetForChar = TreeSet("Spark", "Scala", "Hadoop") println(treeSetForChar) //treeMap用来排序 var treeMap = TreeMap("Scala" -> "Spark", "Java" -> "Hadoop") println(treeMap) } }
1.List的继承体系分析和源码解读:List通过继承其他类并且自己也是抽象类还是sealed修饰的,所带的参数是泛型继承父类的
sealed abstract class List[+A] extends AbstractSeq[A] with LinearSeq[A] with Product with GenericTraversableTemplate[A, List] with LinearSeqOptimized[A, List[A]] with Serializable
//sealed用于模式匹配的指在该包下面所有实现方法可以使用
+A:可以是泛型也可以子类的对象指向父类
2.List方法操作以及源码解读:通过实现Nil和::主要是里面的head和tail以及isEmpty函数
@SerialVersionUID(0 - 8256821097970055419L) case object Nil extends List[Nothing] { override def isEmpty = true override def head: Nothing = throw new NoSuchElementException("head of empty list") override def tail: List[Nothing] = throw new UnsupportedOperationException("tail of empty list") // Removal of equals method here might lead to an infinite recursion similar to IntMap.equals. override def equals(that: Any) = that match { case that1: scala.collection.GenSeq[_] => that1.isEmpty case _ => false } } -->主要实现里面的head、tail、isEmpty函数等
@SerialVersionUID(509929039250432923L) // value computed by serialver for 2.11.2, annotation added in 2.11.4 final case class ::[B](override val head: B, private[scala] var tl: List[B]) extends List[B] { override def tail : List[B] = tl override def isEmpty: Boolean = false } -->主要是实现::空的实现
1.Scala中泛型类型实现:泛型类型在Scala中运用的相当多
2.Scala中泛型函数实现
3.Scala中的泛型在spark中广泛使用
def zip[A1 >: A, B, That](that: GenIterable[B])(implicit bf: CanBuildFrom[Repr, (A1, B), That]): That = { sealed abstract class List[+A] extends AbstractSeq[A] with LinearSeq[A] with Product with GenericTraversableTemplate[A, List] with LinearSeqOptimized[A, List[A]] with Serializable -->在Scala中泛型用的相当广泛,可以说是随处可见,Scala也是可以自动推断出是什么样类型的
以上是今天的所学习的,不是很深,感觉应用从Scala源码开始了解,才能深入学习其中的中东西。
该内容都是从王家林老师教程中学习,他的微信号:18610086859
最新课程视频77讲:http://yun.baidu.com/s/1dD10KFZ
该文章的地址39-42讲:http://pan.baidu.com/s/1jGlbG2q
标签:
原文地址:http://www.cnblogs.com/524646016-zhang/p/zhangsh_dt_scala_11.html