• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

  • Home
  • Test Case Examples
  • Interview Questions
  • Interview Questions Asked
  • Java
  • Java Program
  • Selenium
  • Selenium Programs
  • Manual Testing
  • Difference
  • Tools
  • SQL
  • Contact Us
  • Search
SoftwareTestingo » Difference » Difference Between HashMap VS Hashtable Java With Example

Difference Between HashMap VS Hashtable Java With Example

Last Updated on: April 14, 2019 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • HashMap vs Hashtable
  • When to use HashMap?

HashMap VS Hashtable In Java: In this article, we will discuss the difference between HashMap and Hashtable Java classes in detail i.e.; HashMap vs Hashtable

Read Also: HashMap  VS LinkedHashMap In Java

Hashtable is legacy class and based on hashcode of keys where keys are unique and it is exactly the same as HashMap with few differences with respect to following points

  • Synchronized methods
  • Performance
  • Null key insertion
  • Null value insertion

Lets us move on and discuss the key differences between these 2 Map implemented classes

HashMap vs Hashtable

HashMap
Hashtable
HashMap is introduced in collection framework in Java 1.2 version Hashtable is a legacy class and introduced in Java 1.0 version
HashMap is NOT synchronized Hashtable is synchronized
All methods of HashMap is NOT synchronized i.e.; it is not thread-safe All methods of HashMap is synchronized i.e.; thread-safe
Multiple threads are allowed to access Only one thread is allowed access; other threads have to wait to get access, after obtaining lock/monitor
Performance-wise, this is relatively high comparing with Hashtable, as there is no wait time Performance-wise, this is relatively slow due to synchronized methods as there is only one thread allowed to access, at any given point of time
NULL insertion allowed for both keys and values NULL insertion is not allowed for both keys and values
Maximum of one NULL key and there is no upper limit for values Simply, not allowed for both keys & values

Note: both use the hash table data structure to store key-value pairs

When to use HashMap?

  • HashMap stores key-value pairs which use a hashing technique to store key-value pairs where methods are NOT synchronized
  • So, the search operation is faster with multiple threads access
  • So, if the business requirement is to store key-value pairs for faster search operation or number of search operation on the basis of keys; without concerning concurrent access to the map
  • Then, HashMap is the very apt choice

When to use Java Hashtable?

  • This is exactly the same as that of HashMap, but every method is synchronized
  • Performance-wise is relatively slower than comparing HashMap
  • So, if the business requirement is to store key-value pairs for faster search operation with synchronized access
  • Then, Java Hashtable is the preferred choice over HashMap

    Filed Under: Difference

    Reader Interactions

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Categories

    Copyright © 2023 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy ~ Testing Careers