88

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?

Ben
  • 54,723
  • 49
  • 178
  • 224
  • 8
    If 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. – Stephen C Aug 27 '11 at 04:13
  • 2
    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. – Ben Jan 30 '17 at 20:44
  • You 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. – Stephen C Jan 30 '17 at 22:41

2 Answers2

103

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.

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
  • OK 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. – Ben Aug 27 '11 at 03:35
  • Yep, you get the old value back. So checking for `null` is a good way to see if you just `put` a fresh key for example. – Paul Bellora Aug 27 '11 at 03:40
  • He can use an `int` as a key because Java will autobox it to an Integer. – Paul Aug 27 '11 at 03:44
  • @Paul understood, but I was referring to the type parameters of the `Map` – Paul Bellora Aug 27 '11 at 03:46
  • This 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.) – Jeremy Aug 27 '11 at 03:50
  • @Jeremy I think you're confusing the idea of a having the same key with that of [hash collision](http://en.wikipedia.org/wiki/Hash_table#Collision_resolution), which is an issue that arises in the implementation of any hash table data structure. – Paul Bellora Aug 27 '11 at 03:58
  • @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. – Jeremy Aug 27 '11 at 04:00
  • no problem - Java's [HashSet](http://download.oracle.com/javase/6/docs/api/java/util/HashSet.html) is just a `Set` implementation backed by a `HashMap` with dummy values, so you're right. – Paul Bellora Aug 27 '11 at 04:04
6

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
JessonChan
  • 216
  • 2
  • 3