简体   繁体   English

scala - 为什么map.size在地图不为空时返回0

[英]scala - why map.size return 0 when the map is not empty

I am new to scala. 我是斯卡拉的新手。 Following example I am a bit confused what is going on. 以下示例我对发生的事情感到有些困惑。 I created a mutable map, then pushed three key/value to the map. 我创建了一个可变映射,然后将三个键/值推送到地图。 I can retrieve the queue with values by key, but "web.keys" it shows the map is empty, and "web.size" returns 0! 我可以通过键检索队列的值,但“web.keys”表示地图为空,“web.size”返回0! why is this, and how can I retrieve the correct map size? 为什么会这样,我该如何检索正确的地图大小?

scala> import scala.collection.mutable.{Map, Set, Queue, ArrayBuffer}

scala> val web = Map[Int, Queue[Long]]().withDefaultValue(Queue()) 
web: scala.collection.mutable.Map[Int,scala.collection.mutable.Queue[Long]] = Map()

scala> web(123).enqueue(567L)

scala> web(123).enqueue(1L)

scala> web(123).enqueue(2L)

scala> web(123)
res96: scala.collection.mutable.Queue[Long] = Queue(567, 1, 2)

scala> web
res97: scala.collection.mutable.Map[Int,scala.collection.mutable.Queue[Long]] = Map()

scala> web.size
res98: Int = 0

scala> web.keys
res99: Iterable[Int] = Set()

A simple map works fine. 一个简单的地图工作正常。

scala> val w= Map[Int,Int]()
w: scala.collection.mutable.Map[Int,Int] = Map()

scala> w(1)=1

scala> w
res10: scala.collection.mutable.Map[Int,Int] = Map(1 -> 1)

scala> w(2)=2

scala> w
res12: scala.collection.mutable.Map[Int,Int] = Map(2 -> 2, 1 -> 1)

scala> w.size
res13: Int = 2

I tried more experiment, seems it has something to do with "withDefaultValue". 我尝试了更多实验,似乎与“withDefaultValue”有关。 But how do I fix it? 但是我该如何解决呢?

scala> val ww= Map[Int,Int]().withDefaultValue(0) 
ww: scala.collection.mutable.Map[Int,Int] = Map()

scala> ww
res14: scala.collection.mutable.Map[Int,Int] = Map()

scala> ww(1) += 1

scala> ww(2) += 2

scala>  w.size
res17: Int = 0

When default value is returned from map it is not added to map! 从地图返回默认值时,它不会添加到地图中! So when invoking web(123) 所以在调用web(123)

nothing is added to map, only default value is returned. 没有添加到地图,只返回默认值。 Use getOrElseUpdate method to read data instead of using map with default value. 使用getOrElseUpdate方法读取数据而不是使用具有默认值的map。 Or simply take into consideration that default value is not in map as other key - value pairs. 或者只是考虑到默认值不在地图中作为其他键 - 值对。

I think you are misunderstanding your examples: 我认为你误解了你的例子:

In first example web(123).enqueue(567L) you are retrieveing default value and adding 567L to default value (Queue). 在第一个示例web(123).enqueue(567L)您正在检索默认值并将567L添加到默认值(Queue)。 Nothing is added to map. 地图上没有添加任何内容。

In second example w(1)=1 you are adding data to map 在第二个示例中, w(1)=1您要将数据添加到地图中

In third example ww(1) += 1 you are retrieving default value (0) and adding 1 to it. 在第三个示例中, ww(1) += 1您将检索默认值(0)并向其中添加1。

In general using map(K) will return value for key K, while map(K) = V will set value V for key K. 通常,使用map(K)将返回键K的值,而map(K) = V将为键K设置值V.

Under the hood invoking map(K) and map(K) = V uses apply and update methods. 在引擎盖下调用map(K)map(K) = V使用apply和update方法。 See http://otfried.org/scala/apply.html or other scala documentation for more details. 有关详细信息,请参阅http://otfried.org/scala/apply.html或其他scala文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM