是否“放置”?覆盖现有值?
哈希表新手,有一个简单的问题。由于某种原因,谷歌搜索没有给我一个直接的答案。假设我已经设置了一个
哈希表:
myHashtable.put(1,"bird");
myHashtable.put(2,"iguana");
并且我想将“bird”更改为“fish”(并保持索引不变)。我可以只做一个简单的put
,还是需要删除该条目,或者什么?
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");
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的。
如果到指定键的映射已存在,则旧值将被替换(并返回)。请参阅
Hashtable.put( )
。对于多线程环境,我建议 < code>ConcurrentHashMap 或其他
ConcurrentMap
实现。尽管 Hashtable 是同步的,但现在有更复杂的实现可用于并发映射,例如 Guava 的MapMaker
和CacheBuilder
< /a>.另请记住,
Map
将具有类型参数
,因为不支持原始类型参数。Yes.
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 anotherConcurrentMap
implementation. ThoughHashtable
is synchronized, there are more sophisticated implementations available now for concurrent mapping, such as Guava'sMapMaker
andCacheBuilder
.Also keep in mind the
Map
is going to have the type parameters<Integer, String>
since primitive type parameters aren't supported.嗯,只需添加一行
myHashtable.put(1,"fish");
要了解发生了什么令人惊奇的事情,
请参阅此链接:http://docs.oracle.com/javase/6/docs/api/java/util/Hashtable.html#put(K, V)
hmmm ,just need add a line
myHashtable.put(1,"fish");
to see what's amazing happens
see this links:http://docs.oracle.com/javase/6/docs/api/java/util/Hashtable.html#put(K, V)