“ put”是否覆盖现有的值?

对于散列表来说,只有一个简单的问题。出于某种原因,谷歌并没有给我一个直接的答案。假设我设置了一个 <int,String>散列表:

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

我想把“ bird”改成“ fish”(索引保持不变)。我可以只做一个简单的 put,或者我需要删除条目,还是什么?

109440 次浏览

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 another ConcurrentMap implementation. Though Hashtable is synchronized, there are more sophisticated implementations available now for concurrent mapping, such as Guava's MapMaker and CacheBuilder.

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

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)

Returns:
the previous value of the specified key in this hashtable, or null if it did not have one