0
  1. Can Hashtable have any number of records in it?
  2. Is it efficient to use Hashtable in case we have more records (eg. Some 10000 records)?

And are there any disadvantages of using Hashtable in terms of Efficiency .?

Thanks.

PRATHAP S
  • 675
  • 2
  • 8
  • 26
  • 2
    **go through this `http://stackoverflow.com/questions/40471/differences-between-hashmap-and-hashtable` – NeverGiveUp161 Jun 17 '14 at 10:24
  • 1
    What are you trying to compare here? Disadvantages of Hashtable compared with what? – Jon Skeet Jun 17 '14 at 10:26
  • From the Java API documentation: "As of the Java 2 platform v1.2, this class was retrofitted to implement the Map interface, making it a member of the Java Collections Framework. Unlike the new collection implementations, Hashtable is synchronized. If a thread-safe implementation is not needed, it is recommended to use HashMap in place of Hashtable. If a thread-safe highly-concurrent implementation is desired, then it is recommended to use ConcurrentHashMap in place of Hashtable." – Pablo Lozano Jun 17 '14 at 10:27
  • 1
    @JonSkeet I'm not comparing Hashtable! wanted to know whether Hashtable works effectively when it has got more records as I posted. And will be good if there are any alternatives to Hashtable to use. – PRATHAP S Jun 17 '14 at 10:30
  • @PRATHAPS:- The alternative is to use HashMap – Rahul Tripathi Jun 17 '14 at 10:31
  • 3
    @PRATHAPS: Your title is "Disadvantages of Hashtable" which suggests you're interested in comparing it with something else. An "advantage" or a "disadvantage" is an attribute one option has over another. – Jon Skeet Jun 17 '14 at 10:32
  • 2
    @PRATHAPS: I don't understand your comment - that suggests you *do* want to compare Hashtable with HashMap, contrary to your previous comment of "I'm not comparing Hashtable". You really need to be more specific. (Also, 500 entries is *tiny*. You should be thinking in the 10,000s or 100,000s...) – Jon Skeet Jun 17 '14 at 10:33
  • @JonSkeet So using 500 or 1000 entries in my Hashtable will not decrease the efficiency .? – PRATHAP S Jun 17 '14 at 10:42
  • @PRATHAPS: Not if there aren't any collisions. But my point is that "more records" is already vague (because it presupposes an existing number of records) and 500 isn't a large number, if that's what you were trying to convey. – Jon Skeet Jun 17 '14 at 10:43
  • @JonSkeet Yes Exactly . My understanding was 500 entries was pretty bulk.!! – PRATHAP S Jun 17 '14 at 10:45

5 Answers5

3

The disadvantage of HashTable in terms of efficiency is:

Hash tables become quite inefficient when there are many collisions. While extremely uneven hash distributions are extremely unlikely to arise by chance, a malicious adversary with knowledge of the hash function may be able to supply information to a hash that creates worst-case behavior by causing excessive collisions, resulting in very poor performance, e.g. a denial of service attack.[21] In critical applications, universal hashing can be used; a data structure with better worst-case guarantees may be preferable.[22]

You can use HashMap as they are better and a good option instead of HashTable.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

In general, HashMap will be a better option in terms of efficiency, as it's not synchronized.

Take a look at this:

http://blog.manishchhabra.com/2012/08/the-5-main-differences-betwen-hashmap-and-hashtable/

Andres
  • 10,561
  • 4
  • 45
  • 63
1

Hashtable is (as good as) deprecated. Use HashMap or ConcurrentHashMap based on whether you want your data to be threadsafe or not.

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
0

It depends on the implementation. One way to implement a hash table is to make the initial table not so big, and if the load factor (ratio of used elements vs available slots) increases beyond a threshold, increase the table size Check the wikipedia article for more understanding http://en.wikipedia.org/wiki/Hash_table#Perfect_hash_function

ok thanks for the advice ,I am new on stackoverflow. so answer to question 1 is Yes you can add any number of record as long as you don't overflow. and answer to 2 question is Hashtable is synchronized (Thus overhead) so if you don't need synchronization and thread safety then go for HashMap

wizyashas
  • 95
  • 1
  • 12
  • To be fair, that's also a way to implement an `ArrayList` or any other resizable collection, so I'm not sure exactly how this answers the question... – awksp Jun 17 '14 at 10:42
  • I just said one way , not the only way.. so i didn't answer wrong – wizyashas Jun 17 '14 at 10:59
  • The question wasn't about implementing hash tables... You should add more detail, as right now your answer really is kind of all over the place. – awksp Jun 17 '14 at 11:01
  • Don't overflow *what*? And it might help to point out that in the vast majority of cases you still don't need `Hashtable` as its synchronization method is incorrect for the use case. And I'm not a huge fan of that answer either because it implies that `HashMap` does not suffer from the given problem, even though it does. – awksp Jun 17 '14 at 11:16
  • Stackoverflow error for the "overflow" when filling data into the hashtable, Also the question itself is vague, it doesn't tell what is the use case. also user3414693 answer did not mention that Hashmap doesnot suffer from the collision problem. – wizyashas Jun 17 '14 at 11:23
  • Why would you get a `StackOverflowError` when filling a `HashMap`? There shouldn't be a way for that many stack frames to build up unless you're using a recursive method. That's true, but you almost *never* want to synchronize individual methods -- that kind of lock is too coarse for practical purposes. That answer does not say that explicitly, but it's heavily implied. In addition, there is no elaboration on *how* `HashMap` is better. – awksp Jun 17 '14 at 11:26
0

When using a hash algorithm that causes a lot of collisions, it is not appropriate to use a hash table because it will change lookup and insert from O(1) to O(log n).