是否“放入"?覆盖现有值? [英] Does "put" overwrite existing values?

查看:70
本文介绍了是否“放入"?覆盖现有值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哈希表的新问题很简单.出于某种原因,谷歌搜索并没有给我一个直接的答案.假设我已经设置了<int,String>哈希表:

New to hashtables with a simple question. For some reason googling hasn't gotten me a straight answer. Say I've got an <int,String> hashtable set up:

myHashtable.put(1,"bird");
myHashtable.put(2,"iguana");

,我想将"bird"更改为"fish"(并使索引保持不变).我可以只做一个简单的put,还是需要删除该条目,或者什么?

and I want to change "bird" to "fish" (and leave the index the same). Can I just do a simple put, or do I need to delete the entry, or what?

推荐答案

是.

如果已经存在到指定键的映射,则旧值将被替换(并返回).参见 Hashtable.put()

If a mapping to the specified key already exists, the old value will be replaced (and returned). See Hashtable.put().

对于多线程环境,我建议

For multi-threaded environment, I'd recommend ConcurrentHashMap or another ConcurrentMap implementation. Though Hashtable is synchronized, there are more sophisticated implementations available now for concurrent mapping, such as Guava's MapMaker and CacheBuilder.

还请记住,由于不支持基本类型参​​数,Map将具有类型参数<Integer, String>.

Also keep in mind the Map is going to have the type parameters <Integer, String> since primitive type parameters aren't supported.

这篇关于是否“放入"?覆盖现有值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆