What are the differences between HashMap and Hashtable?

+1 vote
258 views
asked Dec 16, 2016 by Hitesh Garg (352 points)  

I have to use hash collection in my project but I am not sure of which to use and exactly why? I am basically looking for some differences between a HashMap and a Hashtable in Java?

Which is more efficient for threaded and for non-threaded applications?

1 Answer

+1 vote
answered Apr 20, 2017 by clintwelbar (35 points)  
selected Apr 24, 2017 by Hitesh Garg
 
Best answer
  • Hashtable does not allow null keys or values. HashMap allows one null key and any number of null values.
  • In hashmap you can remove element while iterating, safely. where as
    it is not possible in hashtables.
  • One of HashMap's subclasses is LinkedHashMap, so in the event that you'd want predictable iteration order , you could easily swap out the HashMap for a LinkedHashMap. This wouldn't be as easy if you were using Hashtable.
  • Considering performance, Hashmap is much faster and uses less memory
    than Hashtable
    as former is unsynchronized . Unsynchronized objects
    are often much better in performance in compare to synchronized
    object like Hashtable in single threaded environment.

More on..... Hashmap

Clint

124 questions

41 answers

3 comments

928 users

Most popular tags

java python python3 oracle database java-8 sql-server python2 oracle11g git oracle-12g command-line maven sql-server-2012 sql input array list fenitoina dilantin javaçš„ angular javascripts git-branch intellij-idea hashmap collection file-io io temovate clobetasol ampicillin principen daclatasvir daklinza vasotec enalapril nexium esomeprazole tadalafil tadlis sildenafil citrate pefomax xeloda capecitabina clobetasol temovate stemetil prochlorperazine methotrexate methotrexate sildenafilo y dapoxetina super kamagra norvasc amlodipine citalopram celexa amitriptylin saroten vitamin b12 methylcobalamin valaciclovir valtrex zudena udenafil sildenafil citrate vigour depakote divalproex retin-a tretinoine sildenafil citrate ip-max nizoral ketoconazol ciaton tadalafil norethisteronacetat aygestin indomethacin indocin tiova tiotropium bromid finasterida proscar lithium carbonate eskalith finasterida propecia levitra vardenafil vantin cefpodoxima loxitane loxapine rulide roxithromycine avodart dutasterid tecnomax sildenafil citrate methylcobalamin vitamin b12 permethrin acticin etinilestradiol norgestrel ovral pk-merz amantadin cycrin medroxyprogesterone serophene clomiphene spironolactone aldactone mesalazin pentasa zebeta bisoprolol cytodrox hydroxyurea ampicillin ampicillin pletal cilostazol paracetamol panadol pepcid famotidina meclizine hydrochloride antivert donaton tadalafil sdf dsfs sdgfgsdg mac null-pointer-exception constructor oracle-12c terminal hibernate-5 hibernate git-merge es6 programming git-amend git-commit java-stream git-remote multithreading access-modifiers nested-classes
Welcome to Codingeek Q&A, where you can ask questions and receive answers from other members of the community.
...