Java hashtable or hashmap?

This question already has an answer here:

  • Differences between HashMap and Hashtable? 38 answers

  • HashMap (or, more likely, HashSet ) is probably a good place to start, at your point. It's not perfect, and it does consume more memory than eg a list, but it should be your default when you need fast add, remove, and contains operations. The String.hashCode() implementation is not the best hash function, though it is fast, and good enough for most purposes.


    The access time of HashMap (& HashTable as well I believe) is O(1) since the internal bucket placement of the given value during put() is determined by computing (Hash of the value's key) % (Total Number of buckets). This O(1) is average access time, if however many keys hash to the same bucket then the access time will tend towards O(n) as all the values are placed into the same bucket grow and they all grow in linked list fashion.

    As you said considering the overhead of synchronization inside Hashtable, I would probably opt for Hash map. Besides you can fine tune Hashmap by setting its various params like load factor that offers means of memory optimization. I vote for HashMap...


    As you've pointed Hashtable is fully synchronized so it depends on your environment. IF you have many threads then ConcurrentHashMap will be better solution. However you can look at Trove4J - maybe it will better suite your needs. Trove uses chaining hashing similar to hashtables

    链接地址: http://www.djcxy.com/p/16352.html

    上一篇: ConcurrentHashMap和Hash表的区别

    下一篇: Java哈希表或哈希表?