第014讲:Scala中Map和HashMap源码剖析及代码实践(从1000个代码案例中学习人工智能和大数据实战)
2017-12-19 06:48
671 查看
第014讲:Scala中Map和HashMap源码剖析及代码实践
![](http://img.blog.csdn.net/20171219064924951?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZHVhbl96aGlodWE=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
/**
* A generic trait for immutable maps. Concrete classes have to provide
* functionality for the abstract methods in `Map`:
*
* {{{
* def get(key: A): Option[B]
* def iterator: Iterator[(A, B)]
* def + [B1 >: B](kv: (A, B1)): Map[A, B1]
* def -(key: A): Map[A, B]
* }}}
*
* @since 1
*/
trait Map[A, +B] extends Iterable[(A, B)]
// with GenMap[A, B]
with scala.collection.Map[A, B]
with MapLike[A, B, Map[A, B]] { self =>
override def empty: Map[A, B] = Map.empty
override def toMap[T, U](implicit ev: (A, B) <:< (T, U)): immutable.Map[T, U] =
self.asInstanceOf[immutable.Map[T, U]]
/** Returns the value associated with a key, or a default value if the key is not contained in the map.
* @param key the key.
* @param default a computation that yields a default value in case no binding for `key` is
* found in the map.
* @tparam B1 the result type of the default computation.
* @return the value associated with `key` if it exists,
* otherwise the result of the `default` computation.
*
* @usecase def getOrElse(key: A, default: => B): B
* @inheritdoc
*/
def getOrElse[B1 >: B](key: A, default: => B1): B1 = get(key) match {
case Some(v) => v
case None => default
}
package scala.collection
package mutable
import generic._
import scala.collection.parallel.mutable.ParHashMap
/** This class implements mutable maps using a hashtable.
*
* @since 1
* @see [[http://docs.scala-lang.org/overviews/collections/concrete-mutable-collection-classes.html#hash_tables "Scala's Collection Library overview"]]
* section on `Hash Tables` for more information.
*
* @tparam A the type of the keys contained in this hash map.
* @tparam B the type of the values assigned to keys in this hash map.
*
* @define Coll `mutable.HashMap`
* @define coll mutable hash map
* @define thatinfo the class of the returned collection. In the standard library configuration,
* `That` is always `HashMap[A, B]` if the elements contained in the resulting collection are
* pairs of type `(A, B)`. This is because an implicit of type `CanBuildFrom[HashMap, (A, B), HashMap[A, B]]`
* is defined in object `HashMap`. Otherwise, `That` resolves to the most specific type that doesn't have
* to contain pairs of type `(A, B)`, which is `Iterable`.
* @define bfinfo an implicit value of class `CanBuildFrom` which determines the
* result class `That` from the current representation type `Repr`
* and the new element type `B`. This is usually the `canBuildFrom` value
* defined in object `HashMap`.
* @define mayNotTerminateInf
* @define willNotTerminateInf
*/
@SerialVersionUID(1L)
class HashMap[A, B] private[collection] (contents: HashTable.Contents[A, DefaultEntry[A, B]])
extends AbstractMap[A, B]
with Map[A, B]
with MapLike[A, B, HashMap[A, B]]
with HashTable[A, DefaultEntry[A, B]]
with CustomParallelizable[(A, B), ParHashMap[A, B]]
with Serializable
{
initWithContents(contents)
type Entry = DefaultEntry[A, B]
override def empty: HashMap[A, B] = HashMap.empty[A, B]
override def clear() { clearTable() }
override def size: Int = tableSize
def this() = this(null)
override def par = new ParHashMap[A, B](hashTableContents)
// contains and apply overridden to avoid option allocations.
override def contains(key: A): Boolean = findEntry(key) != null
override def apply(key: A): B = {
val result = findEntry(key)
if (result eq null) default(key)
else result.value
}
def get(key: A): Option[B] = {
val e = findEntry(key)
if (e eq null) None
else Some(e.value)
}
override def put(key: A, value: B): Option[B] = {
val e = findOrAddEntry(key, value)
if (e eq null) None
else { val v = e.value; e.value = value; Some(v) }
}
override def update(key: A, value: B): Unit = put(key, value)
override def remove(key: A): Option[B] = {
val e = removeEntry(key)
if (e ne null) Some(e.value)
else None
}
def += (kv: (A, B)): this.type = {
val e = findOrAddEntry(kv._1, kv._2)
if (e ne null) e.value = kv._2
this
}
def -=(key: A): this.type = { removeEntry(key); this }
def iterator = entriesIterator map {e => (e.key, e.value)}
override def foreach[C](f: ((A, B)) => C): Unit = foreachEntry(e => f(e.key, e.value))
/* Override to avoid tuple allocation in foreach */
override def keySet: scala.collection.Set[A] = new DefaultKeySet {
override def foreach[C](f: A => C) = foreachEntry(e => f(e.key))
}
/* Override to avoid tuple allocation in foreach */
override def values: scala.collection.Iterable[B] = new DefaultValuesIterable {
override def foreach[C](f: B => C) = foreachEntry(e => f(e.value))
}
/* Override to avoid tuple allocation */
override def keysIterator: Iterator[A] = new AbstractIterator[A] {
val iter = entriesIterator
def hasNext = iter.hasNext
def next() = iter.next.key
}
/* Override to avoid tuple allocation */
override def valuesIterator: Iterator[B] = new AbstractIterator[B] {
val iter = entriesIterator
def hasNext = iter.hasNext
def next() = iter.next.value
}
/** Toggles whether a size map is used to track hash map statistics.
*/
def useSizeMap(t: Boolean) = if (t) {
if (!isSizeMapDefined) sizeMapInitAndRebuild
} else sizeMapDisable
protected def createNewEntry[B1](key: A, value: B1): Entry = {
new Entry(key, value.asInstanceOf[B])
}
private def writeObject(out: java.io.ObjectOutputStream) {
serializeTo(out, { entry =>
out.writeObject(entry.key)
out.writeObject(entry.value)
})
}
private def readObject(in: java.io.ObjectInputStream) {
init(in, createNewEntry(in.readObject().asInstanceOf[A], in.readObject()))
}
}
/** $factoryInfo
* @define Coll `mutable.HashMap`
* @define coll mutable hash map
*/
object HashMap extends MutableMapFactory[HashMap] {
implicit def canBuildFrom[A, B]: CanBuildFrom[Coll, (A, B), HashMap[A, B]] = new MapCanBuildFrom[A, B]
def empty[A, B]: HashMap[A, B] = new HashMap[A, B]
}
/**
* A generic trait for immutable maps. Concrete classes have to provide
* functionality for the abstract methods in `Map`:
*
* {{{
* def get(key: A): Option[B]
* def iterator: Iterator[(A, B)]
* def + [B1 >: B](kv: (A, B1)): Map[A, B1]
* def -(key: A): Map[A, B]
* }}}
*
* @since 1
*/
trait Map[A, +B] extends Iterable[(A, B)]
// with GenMap[A, B]
with scala.collection.Map[A, B]
with MapLike[A, B, Map[A, B]] { self =>
override def empty: Map[A, B] = Map.empty
override def toMap[T, U](implicit ev: (A, B) <:< (T, U)): immutable.Map[T, U] =
self.asInstanceOf[immutable.Map[T, U]]
/** Returns the value associated with a key, or a default value if the key is not contained in the map.
* @param key the key.
* @param default a computation that yields a default value in case no binding for `key` is
* found in the map.
* @tparam B1 the result type of the default computation.
* @return the value associated with `key` if it exists,
* otherwise the result of the `default` computation.
*
* @usecase def getOrElse(key: A, default: => B): B
* @inheritdoc
*/
def getOrElse[B1 >: B](key: A, default: => B1): B1 = get(key) match {
case Some(v) => v
case None => default
}
package scala.collection
package mutable
import generic._
import scala.collection.parallel.mutable.ParHashMap
/** This class implements mutable maps using a hashtable.
*
* @since 1
* @see [[http://docs.scala-lang.org/overviews/collections/concrete-mutable-collection-classes.html#hash_tables "Scala's Collection Library overview"]]
* section on `Hash Tables` for more information.
*
* @tparam A the type of the keys contained in this hash map.
* @tparam B the type of the values assigned to keys in this hash map.
*
* @define Coll `mutable.HashMap`
* @define coll mutable hash map
* @define thatinfo the class of the returned collection. In the standard library configuration,
* `That` is always `HashMap[A, B]` if the elements contained in the resulting collection are
* pairs of type `(A, B)`. This is because an implicit of type `CanBuildFrom[HashMap, (A, B), HashMap[A, B]]`
* is defined in object `HashMap`. Otherwise, `That` resolves to the most specific type that doesn't have
* to contain pairs of type `(A, B)`, which is `Iterable`.
* @define bfinfo an implicit value of class `CanBuildFrom` which determines the
* result class `That` from the current representation type `Repr`
* and the new element type `B`. This is usually the `canBuildFrom` value
* defined in object `HashMap`.
* @define mayNotTerminateInf
* @define willNotTerminateInf
*/
@SerialVersionUID(1L)
class HashMap[A, B] private[collection] (contents: HashTable.Contents[A, DefaultEntry[A, B]])
extends AbstractMap[A, B]
with Map[A, B]
with MapLike[A, B, HashMap[A, B]]
with HashTable[A, DefaultEntry[A, B]]
with CustomParallelizable[(A, B), ParHashMap[A, B]]
with Serializable
{
initWithContents(contents)
type Entry = DefaultEntry[A, B]
override def empty: HashMap[A, B] = HashMap.empty[A, B]
override def clear() { clearTable() }
override def size: Int = tableSize
def this() = this(null)
override def par = new ParHashMap[A, B](hashTableContents)
// contains and apply overridden to avoid option allocations.
override def contains(key: A): Boolean = findEntry(key) != null
override def apply(key: A): B = {
val result = findEntry(key)
if (result eq null) default(key)
else result.value
}
def get(key: A): Option[B] = {
val e = findEntry(key)
if (e eq null) None
else Some(e.value)
}
override def put(key: A, value: B): Option[B] = {
val e = findOrAddEntry(key, value)
if (e eq null) None
else { val v = e.value; e.value = value; Some(v) }
}
override def update(key: A, value: B): Unit = put(key, value)
override def remove(key: A): Option[B] = {
val e = removeEntry(key)
if (e ne null) Some(e.value)
else None
}
def += (kv: (A, B)): this.type = {
val e = findOrAddEntry(kv._1, kv._2)
if (e ne null) e.value = kv._2
this
}
def -=(key: A): this.type = { removeEntry(key); this }
def iterator = entriesIterator map {e => (e.key, e.value)}
override def foreach[C](f: ((A, B)) => C): Unit = foreachEntry(e => f(e.key, e.value))
/* Override to avoid tuple allocation in foreach */
override def keySet: scala.collection.Set[A] = new DefaultKeySet {
override def foreach[C](f: A => C) = foreachEntry(e => f(e.key))
}
/* Override to avoid tuple allocation in foreach */
override def values: scala.collection.Iterable[B] = new DefaultValuesIterable {
override def foreach[C](f: B => C) = foreachEntry(e => f(e.value))
}
/* Override to avoid tuple allocation */
override def keysIterator: Iterator[A] = new AbstractIterator[A] {
val iter = entriesIterator
def hasNext = iter.hasNext
def next() = iter.next.key
}
/* Override to avoid tuple allocation */
override def valuesIterator: Iterator[B] = new AbstractIterator[B] {
val iter = entriesIterator
def hasNext = iter.hasNext
def next() = iter.next.value
}
/** Toggles whether a size map is used to track hash map statistics.
*/
def useSizeMap(t: Boolean) = if (t) {
if (!isSizeMapDefined) sizeMapInitAndRebuild
} else sizeMapDisable
protected def createNewEntry[B1](key: A, value: B1): Entry = {
new Entry(key, value.asInstanceOf[B])
}
private def writeObject(out: java.io.ObjectOutputStream) {
serializeTo(out, { entry =>
out.writeObject(entry.key)
out.writeObject(entry.value)
})
}
private def readObject(in: java.io.ObjectInputStream) {
init(in, createNewEntry(in.readObject().asInstanceOf[A], in.readObject()))
}
}
/** $factoryInfo
* @define Coll `mutable.HashMap`
* @define coll mutable hash map
*/
object HashMap extends MutableMapFactory[HashMap] {
implicit def canBuildFrom[A, B]: CanBuildFrom[Coll, (A, B), HashMap[A, B]] = new MapCanBuildFrom[A, B]
def empty[A, B]: HashMap[A, B] = new HashMap[A, B]
}
相关文章推荐
- 第015讲: 跟王家林学习从1000个代码案例中学习人工智能和大数据实战第015讲:Scala中Tuple源码剖析及代码实践
- 第007讲-第011讲 从1000个代码案例中学习人工智能和大数据实战
- 第012讲-第013讲 从1000个代码案例中学习人工智能和大数据实战
- 第000讲-第006讲 从1000个代码案例中学习人工智能和大数据实战
- Spark 2.0从入门到精通:Scala编程、大数据开发、上百个实战案例、内核源码深度剖析(278讲全)
- Scala中类型变量Bounds代码实战及其在Spark中的应用源码解析之Scala学习笔记-34
- Scala中Variance代码实战及其在Spark中的应用源码解析之Scala学习笔记-40
- Scala多重界定代码实战及其在Spark中的应用源码解析之Scala学习笔记-38
- 第45讲:Scala中Context Bounds代码实战及其在Spark中的应用源码解析学习笔记
- scala学习之:List的map、flatMap、foreach、filter的操作代码实战
- 云星数据---Scala实战系列(精品版)】:Scala入门教程045-Scala实战源码-Scala Map操作
- ClassTag 、Manifest、ClassManifest、TypeTag代码实战及其在Spark中的应用源码解析之Scala学习笔记-37
- 第44讲:Scala中View Bounds代码实战及其在Spark中的应用源码解析学习笔记
- (升级版)Spark从入门到精通(Scala编程、案例实战、高级特性、Spark内核源码剖析、Hadoop高端)
- Set、Map、TreeSet、TreeMap操作代码实战之Scala学习笔记-31
- List的map、flatMap、foreach、filter操作代码实战之Scala学习笔记-27
- Scala中Context Bounds代码实战及其在Spark中的应用源码解析之Scala学习笔记-36
- 《From Zero to Hero – 从1000个代码案例中学习人工智能和大数据》课程
- 大数据Spark “蘑菇云”行动第79课:Spark GraphX 代码实战及源码剖析
- Scala中View Bounds代码实战及其在Spark中的应用源码解析之Scala学习笔记-35