Does "put" overwrite existing values?

104,008

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
Share:
104,008

Related videos on Youtube

Ben
Author by

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, 2020

Comments

  • Ben
    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
      Stephen C over 12 years
      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.
    • Ben
      Ben over 7 years
      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.
    • Stephen C
      Stephen C over 7 years
      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.
  • Ben
    Ben over 12 years
    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.
  • Paul Bellora
    Paul Bellora over 12 years
    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
    Paul over 12 years
    He can use an int as a key because Java will autobox it to an Integer.
  • Paul Bellora
    Paul Bellora over 12 years
    @Paul understood, but I was referring to the type parameters of the Map
  • Jeremy
    Jeremy over 12 years
    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.)
  • Paul Bellora
    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
    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
    Paul Bellora over 12 years
    no problem - Java's HashSet is just a Set implementation backed by a HashMap with dummy values, so you're right.