動作確認コード
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec
class ScalaMapAddPriorityTest extends AnyWordSpec with Matchers {
"map の + method" when {
"keyが重複" should {
"追加したvalueで上書きされる" in {
(Map("a" -> 1) + ("a" -> 99)).apply("a") should be(99)
}
}
}
"map の ++ method" when {
"keyが重複" should {
"右側のvalueで上書きされる" in {
(Map("a" -> 1) ++ Map("a" -> 99)).apply("a") should be(99)
(Map("a" -> 99) ++ Map("a" -> 1)).apply("a") should be(1)
(Map("a" -> 1) ++ Map("a" -> 99) ++ Map("a" -> 555))
.apply("a") should be(555)
}
}
}
}
Scaladoc
優先順位は書かれていなかった
+
/** Add a key/value pair to this map.
* @param key the key
* @param value the value
* @return A new map with the new binding added to this map
*/
override def updated [V1 >: V](key: K, value: V1): Map[K, V1]
def + [V1 >: V](kv: (K, V1)): Map[K, V1]
++
/** Adds a number of elements provided by a traversable object
* and returns a new collection with the added elements.
*
* @param xs the traversable object consisting of key-value pairs.
* @return a new immutable map with the bindings of this map and those from `xs`.
*/
override def ++[V1 >: V](xs: GenTraversableOnce[(K, V1)]): immutable.Map[K, V1] =
((repr: immutable.Map[K, V1]) /: xs.seq) (_ + _)
+
が呼ばれているので、+
依存
※ /:
は foldLeft
def /:[B](z: B)(op: (B, A) => B): B = foldLeft(z)(op)
未確認事項
- 具体的な
+
実装- Map の実装によるのかもしれない