java map的size我没记错的话是不能直接用的?

chichenzhe 2013-06-23 11:14:52
如果我没记错的话应该是RT那样吧?
是不是和map的实现有关,比如到底是用hashmap还是treemap等.

一般我代码里都会用 map.values().size() 来获取总元素数.这样的话就肯定保险了.

但是我不太理解为什么会这样. 也没有时间做更多查阅性深究, 劳烦知道的兄弟给个全面点的解释.
...全文
1414 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
冰思雨 2013-06-24
  • 打赏
  • 举报
回复
size方法,不是不能用,而是用法问题。 例如:

for(int i=0;i<map.size();i++){
  Object key = createNewKey();
  Object val = createValue();
  map.put(key,val);
}
上述代码中,在遍历Map内容时,由于新增了键值对造成遍历过程中size返回的数值不一致, 从而产生不可预期的结果。这时,要采用迭代器来完成增删操作。
chichenzhe 2013-06-24
  • 打赏
  • 举报
回复
啊......................
菖蒲老先生 2013-06-24
  • 打赏
  • 举报
回复
map.size()我都用了多少年了。。。
zhangxm2015 2013-06-24
  • 打赏
  • 举报
回复
我通常都是直接调用size方法的。 至于你说的先调用values在size,我估计跟线程的安全性有关。 看一下values方法的说明:

      *If the map is
     * modified while an iteration over the collection is in progress
     * (except through the iterator's own <tt>remove</tt> operation),
     * the results of the iteration are undefined.  The collection
     * supports element removal, which removes the corresponding
     * mapping from the map, via the <tt>Iterator.remove</tt>,
     * <tt>Collection.remove</tt>, <tt>removeAll</tt>,
     * <tt>retainAll</tt> and <tt>clear</tt> operations.  It does not
     * support the <tt>add</tt> or <tt>addAll</tt> operations.
也就是说当你调用values时,会形成collection与map的value、值建立映射,相互影响, 而且这里只支持通过Iterator的删除,不支持添加。 这样的话当进行多线程操作时,间接调用size就能保证获取的size是正确的结果。 而直接调用size,当遍历时是支持add的,这样可能会造成结果的不准确! 以上仅是个人见解,如有不正,希望指正!谢谢!
lkq_david 2013-06-24
  • 打赏
  • 举报
回复
查下帮助文档好了
小丑哥_V5 2013-06-24
  • 打赏
  • 举报
回复
为什么不可以直接用..
rumlee 2013-06-24
  • 打赏
  • 举报
回复
map的size可以直接用啊。size是Map接口的方法,所有实现了Map接口的类都应该实现size方法,不管是hashmap还是treemap都实现了这个方法。
LCore 2013-06-24
  • 打赏
  • 举报
回复
引用 7 楼 preferme 的回复:
size方法,不是不能用,而是用法问题。 例如:

for(int i=0;i<map.size();i++){
  Object key = createNewKey();
  Object val = createValue();
  map.put(key,val);
}
上述代码中,在遍历Map内容时,由于新增了键值对造成遍历过程中size返回的数值不一致, 从而产生不可预期的结果。这时,要采用迭代器来完成增删操作。
嘿,真有这个问题呢?在使用size遍历map的同时插入key,value。 不过仅仅获取Map的大小用size没有什么问题的! Map的遍历一般都是使用迭代器进行的,它提供了一种称为 快速失败--Fast-Fail的机制
sd4324530 2013-06-24
  • 打赏
  • 举报
回复
一直都是直接map.size(); 学习了
lcf 2013-06-24
  • 打赏
  • 举报
回复
引用 8 楼 draem0507 的回复:
[quote=引用 4 楼 FirstHelloWorld 的回复:] 我通常都是直接调用size方法的。 至于你说的先调用values在size,我估计跟线程的安全性有关。 看一下values方法的说明:

      *If the map is
     * modified while an iteration over the collection is in progress
     * (except through the iterator's own <tt>remove</tt> operation),
     * the results of the iteration are undefined.  The collection
     * supports element removal, which removes the corresponding
     * mapping from the map, via the <tt>Iterator.remove</tt>,
     * <tt>Collection.remove</tt>, <tt>removeAll</tt>,
     * <tt>retainAll</tt> and <tt>clear</tt> operations.  It does not
     * support the <tt>add</tt> or <tt>addAll</tt> operations.
也就是说当你调用values时,会形成collection与map的value、值建立映射,相互影响, 而且这里只支持通过Iterator的删除,不支持添加。 这样的话当进行多线程操作时,间接调用size就能保证获取的size是正确的结果。 而直接调用size,当遍历时是支持add的,这样可能会造成结果的不准确! 以上仅是个人见解,如有不正,希望指正!谢谢!
正解 不过平时开发很好会涉及到多线程 所以直接调用size即可[/quote] 除了练习题,我还没做过什么单线程的开发。。
draem0507 2013-06-24
  • 打赏
  • 举报
回复
引用 4 楼 FirstHelloWorld 的回复:
我通常都是直接调用size方法的。 至于你说的先调用values在size,我估计跟线程的安全性有关。 看一下values方法的说明:

      *If the map is
     * modified while an iteration over the collection is in progress
     * (except through the iterator's own <tt>remove</tt> operation),
     * the results of the iteration are undefined.  The collection
     * supports element removal, which removes the corresponding
     * mapping from the map, via the <tt>Iterator.remove</tt>,
     * <tt>Collection.remove</tt>, <tt>removeAll</tt>,
     * <tt>retainAll</tt> and <tt>clear</tt> operations.  It does not
     * support the <tt>add</tt> or <tt>addAll</tt> operations.
也就是说当你调用values时,会形成collection与map的value、值建立映射,相互影响, 而且这里只支持通过Iterator的删除,不支持添加。 这样的话当进行多线程操作时,间接调用size就能保证获取的size是正确的结果。 而直接调用size,当遍历时是支持add的,这样可能会造成结果的不准确! 以上仅是个人见解,如有不正,希望指正!谢谢!
正解 不过平时开发很好会涉及到多线程 所以直接调用size即可

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧