Hashtable and HashMap are two hash based collection in Java and used to store objects as key value p

Hashtable and HashMap are two hash based collection in Java and used to store objects as key value pair. Despite being hash based and similar in functionality there are significant difference between Hashtable and HashMap and without understanding those difference if you use Hashtable in place of HashMap than you may run into series of subtle programs which is hard to find and debug.Unlike Difference between ArrayList and HashMap, Difference between Hashtable and HashMap are more subtle because both are similar kind of collection. Before seeing difference between HashMap and Hashtable let's see some common things between HashMap and Hashtable in Java.



Similarities between Hashtable and HashMap in Java
What is Difference between HashMap and Hashtable in Java CollectionThere are lot of similar things between Hashtable and HashMap in Java which is good to know and these also helps to find exactly what is different between HashMap and Hashtable in Java:


1) Both Hashtable and HashMap implements java.util.Map interface.
2) Hashtable and HashMap both are hash based collection and works on principle of hashing.
3) Hashtable and HashMap both provide constant time performance for put and get method if objects are distributed uniformly across bucket.
4) From JDK 4 both Hashtableand HashMap are part of Java collection framework.


Difference between Hashtable and HashMap in Java

Despite being so similar there are some differences between Hashtable and HashMap in Java which separates them completely, let's have a look :

1) First and most significant different between Hashtable and HashMapis that, HashMap is not thread-safe while Hashtable is a thread-safe collection.

2) Second important difference between Hashtable and HashMap is performance, since HashMap is not synchronized it perform better than Hashtable.

3) Third difference on Hashtable vs HashMap is that Hashtable is obsolete class and you should be using ConcurrentHashMap in place of Hashtable in Java.

These were some important difference on Hashtable and HashMap in Java. If you know any other difference which is not included here than feel free to add them in comment section. Remember this is an important question on Java interview and good to prepare it well.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HashtableHashMapJava中两种常用的哈希表实现的Map接口的实现类,它们之间有以下几个区别: 1. 线程安全性:Hashtable是线程安全的,而HashMap不是。Hashtable的所有方法都是同步的,可以在多线程环境下使用,但是会影响性能。而HashMap在多线程环境下需要通过外部同步来保证线程安全。 2. 同步性:Hashtable是通过synchronized关键字实现同步的,而HashMap不是。因此,在单线程环境下,HashMap的性能比Hashtable更好。 3. 允许null键和null值:Hashtable不允许键或值为null,而HashMap允许null键和null值。当需要存储null值时,可以选择使用HashMap。 4. 迭代器的失败-fast-fail机制:当在迭代过程中对集合进行修改时,Hashtable会抛出ConcurrentModificationException异常,而HashMap则不会。这是因为Hashtable在迭代过程中使用了一个modCount变量来记录集合被修改的次数,而HashMap没有。 5. 初始容量和扩容机制:Hashtable的初始容量为11,扩容时容量会翻倍加1;HashMap的初始容量为16,扩容时容量会翻倍。因此,HashMap的扩容次数相对较少,性能相对较好。 下面是一个演示HashtableHashMap的区别的例子: ```java import java.util.Hashtable; import java.util.HashMap; public class HashTableHashMapDemo { public static void main(String[] args) { // 创建HashtableHashMap对象 Hashtable<Integer, String> hashtable = new Hashtable<>(); HashMap<Integer, String> hashMap = new HashMap<>(); // 添加键值对 hashtable.put(1, "One"); hashtable.put(2, "Two"); hashtable.put(3, "Three"); hashMap.put(1, "One"); hashMap.put(2, "Two"); hashMap.put(3, "Three"); // 输出键值对 System.out.println("Hashtable: " + hashtable); System.out.println("HashMap: " + hashMap); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值