Does “put” overwrite existing values?

前端 未结 2 1570
清歌不尽
清歌不尽 2021-02-06 20:09

New to hashtables with a simple question. For some reason googling hasn\'t gotten me a straight answer. Say I\'ve got an hashtable set up:

相关标签:
2条回答
  • 2021-02-06 20:42

    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
    
    0 讨论(0)
  • 2021-02-06 20:54

    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.

    0 讨论(0)
提交回复
热议问题