If you want the short version so you can go back to writing tests instead of arguing about APIs here it is HashMap is not synchronized and not thread safe Hashtable is synchronized and thread safe but old and slow Use HashMap for most single thread code and ConcurrentHashMap for serious concurrent workloads
These two containers both store key value pairs but they behave very differently under pressure HashMap is modern and flexible It lets one null key and many null values and it gives you fast operations when only one thread touches it Hashtable comes from an era when Java was still young It synchronizes most methods and rejects null keys and null values which keeps things safe but also keeps things slower
HashMap has no built in synchronization Multiple threads mutating it at the same time may corrupt the internal structure and cause all sorts of delightful chaos such as infinite loops during iteration Hashtable synchronizes its methods so basic operations are thread safe at the cost of throughput That sounds convenient until you realize the cost adds up quickly
Yes nulls are allowed in HashMap One null key and any number of null values If you like storing absence as an explicit value HashMap lets you do that Hashtable refuses to accept null keys and null values and will throw a runtime exception if you try to be clever
HashMap iterators are fail fast They detect concurrent modification and throw ConcurrentModificationException to wake you up from your bad assumptions Hashtable has legacy enumerations that do not fail fast which can hide concurrent modification bugs and make debugging more interesting than it should be
HashMap is faster in single thread or when you provide external synchronization If you need concurrent reads and writes do not reach for Hashtable Reach for ConcurrentHashMap instead It is designed for concurrency and gives much better throughput than Hashtable or a synchronized HashMap
Map map = new HashMap()
Map legacy = new Hashtable()
Map concurrent = new ConcurrentHashMap()
If your code is single threaded or you manage synchronization elsewhere use HashMap If you need concurrent safety and high performance use ConcurrentHashMap If you maintain ancient code that refuses to be updated then yes use Hashtable and try not to cry about it
I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!
This is a dedicated watch page for a single video.