是否“放入"?覆盖现有值? [英] Does "put" overwrite existing values?
问题描述
哈希表的新问题很简单.出于某种原因,谷歌搜索并没有给我一个直接的答案.假设我已经设置了<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()
.
对于多线程环境,我建议 MapMaker
和
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屋!