Does "put" overwrite existing values?
Solution 1
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.
Solution 2
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
Related videos on Youtube
Ben
javascript 1167th user (yes!) php 1312th user (wow!) html 476th user (he's good!) css 360th user (quick, hire this guy!) Proud owner of the Tumbleweed badge (his weaknesses are actually strengths!)
Updated on July 26, 2020Comments
-
Ben almost 4 years
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?-
Stephen C over 12 yearsIf you want to understand how a specific Java API works, don't waste your time "Googling it". Just go to the online Javadocs for the class and read the docs for the class / method.
-
Ben over 7 yearsI did read that documentation but was a little unclear on the "returns" line: the previous value of the specified key in this hashtable, or null if it did not have one. It sounds like the old value is returned.
-
Stephen C over 7 yearsYou are not using the value returned by
put
in the example, so I don't see how that part of the javadoc is relevant to you. But the javadoc is crystal clear ... the old value will be returned, if there was one.
-
-
Ben over 12 yearsOK great, thanks. I did read that documentation but was a little unclear on the "returns" line:
the previous value of the specified key in this hashtable, or null if it did not have one
. It sounds like the old value is returned...anyway back to the code, will have a look. -
Paul Bellora over 12 yearsYep, you get the old value back. So checking for
null
is a good way to see if you justput
a fresh key for example. -
Paul over 12 yearsHe can use an
int
as a key because Java will autobox it to an Integer. -
Paul Bellora over 12 years@Paul understood, but I was referring to the type parameters of the
Map
-
Jeremy over 12 yearsThis confused the hell out of me at first because in my data structures class we put considerable time into discussing the various ways to deal with collisions, such as the item at the next available index or putting arrays in each key. I couldn't conceive that Java's implementation for this would be the simplest possible solution -- when a collision occurs, disregard the old value! Of course, this is an extremely useful way to do it, and make Hash Maps a great data structure for very many problems (or every problem, If you believe interview questions.)
-
Paul Bellora over 12 years@Jeremy I think you're confusing the idea of a having the same key with that of hash collision, which is an issue that arises in the implementation of any hash table data structure.
-
Jeremy over 12 years@Kublai Khan Interesting! I never made that distinction. I wonder if this same concept applies to a hash set, where I'd imagine it's equally as relevant? Thank you for clarifying that.
-
Paul Bellora over 12 yearsno problem - Java's HashSet is just a
Set
implementation backed by aHashMap
with dummy values, so you're right.