Both the HashMap and Hashtable implement the interface java.util.Map but there are some slight differences which has to be known to write a much efficient code.

  • The Most important difference between HashMap and the Hashtable is that Hashtable is synchronized and HashMap is non-synchronized , which means Hashtable is thread-safe and can be shared among multiple threads and you don’t need to worry about the synchronization problems. As only one thread can access the Hashtable at a time whereas Hashmap is not thread-safe and it cannot be shared between threads without synchronization. You can synchronize the HashMap using this below code.
Map m = Collections.synchronizedMap(hashMap);
  • HashMap permits one null key and multiple null values whereas the Hashtable will not allow null key or value.
  • Since the Hashtable is thread-safe it is comparatively slower than the HashMap in the environment where Synchronization factor is not considered.
  • HashMap can be traversed by using the iterator, whereas the Hashtable can be traversed by using enumerator and iterator.
  • HashMap inherits AbstractMap class and Hashtable inherits Dictionary class.
Did you find apk for android? You can find new Free Android Games and apps.

Filed Under: JavaJava InterviewTagged With: differencehashmaphashmap and hashtablehashtablesynchronizationthread-safe

Difference between HashMap and Hashtable | HashMap Vs Hashtable的更多相关文章

  1. HashSet HashTable HashMap的区别 及其Java集合介绍

    (1)HashSet是set的一个实现类,hashMap是Map的一个实现类,同时hashMap是hashTable的替代品(为什么后面会讲到). (2)HashSet以对象作为元素,而HashMap ...

  2. Hashtable,HashMap实现原理

    http://blog.csdn.net/czh0766/article/details/5260360 昨天看了算法导论对散列表的介绍,今天看了一下Hashtable, HashMap这两个类的源代 ...

  3. C# Hashtable 使用说明 以及 Hashtable和HashMap的区别

    一,哈希表(Hashtable)简述 在.NET Framework中,Hashtable是System.Collections命名空间提供的一个容器,用于处理和表现类似key/value的键值对,其 ...

  4. java该HashTable,HashMap和HashSet

    同一时候我们也对HashSet和HashMap的核心方法hashcode进行了具体解释,见<探索equals()和hashCode()方法>. 万事俱备,那么以下我们就对基于hash算法的 ...

  5. 多线程之Map:Hashtable HashMap 以及ConcurrentHashMap

    1.Map体系参考:http://java.chinaitlab.com/line/914247.htmlHashtable是JDK 5之前Map唯一线程安全的内置实现(Collections.syn ...

  6. 为什么HashMap线程不安全,Hashtable和ConcurrentHashMap线程安全

    HashMap源码 public V put(K key, V value) { return putVal(hash(key), key, value, false, true); } final ...

  7. HashTable & HashMap & ConcurrentHashMap 原理与区别

    一.三者的区别     HashTable HashMap ConcurrentHashMap 底层数据结构 数组+链表 数组+链表 数组+链表 key可为空 否 是 否 value可为空 否 是 否 ...

  8. Hashtable,HashMap,TreeMap有什么区别?Vector,ArrayList,LinkedList有什么区别?int和Integer有什么区别?

    接着上篇继续更新. /*请尊重作者劳动成果,转载请标明原文链接:*/ /*https://www.cnblogs.com/jpcflyer/p/10759447.html* / 题目一:Hashtab ...

  9. 10 HashMap,Map.Entry,LinkedHashMap,TreeMap,Hashtable,Collections类

    Map集合的功能概述 添加功能 * V put(K key,V value):添加元素.            * 如果键是第一次存储,就直接存储元素,返回null            * 如果键不 ...

随机推荐

  1. 自我总结(四) ---java web项目完结,j2ee的开始

    自我完善的过程就是在不断的自我总结不断的改进. 前半个月刚好把项目做完了,项目也答辩了.总的来说吧,我觉得自己在java web这块知识上不算是彻彻底底把他弄懂了,就是说到的知识点都能够回答的上来一些 ...

  2. docker部署jenkinsci blueocean

    1.使用docker-compose # cat docker-compose.ymlversion: '2'services: jenkinsci: image: jenkinsci/blueoce ...

  3. winform窗体 小程序【三级联动】

    三级联动[省,市,区] 类似地区选择,当选的某个省份,后面的下拉框相对变成对应省份的区县 实现省市区联动关键是数据库的表,[每个省内区的AreaCode列是同样的] public Form2() { ...

  4. Retrofit2+Rxjava+OkHttp的使用和网络请求

    Retrofit2+Rxjava+OkHttp的使用和网络请求 https://blog.csdn.net/huandroid/article/details/79883895 加入Rxjava 如果 ...

  5. hadoop 用户

    上篇文章说过,hadoop实际上就是就是一个liunx虚拟机,它的文件操作命令同linux,而它的用户呢?它的用户实际上就是linux主机的用户.这里我们做的例子就是:新建一个liunx用户,并为该l ...

  6. php魔术方法__tostring的应用

    当echo一个对象的时候,会报错误 Object of class Person could not be converted to string 我们可以通过魔术方法__tostring()  把对 ...

  7. HDU1402(fft)

    A * B Problem Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  8. cakephp怎么默认显示index/index文件

    在配置好cakephp之后,我们输入网址后都一般默认显示index/index 文件,那么怎么设置呢? 1.D:\www\cakephp\app\Config\routes.php

  9. vertical-align 详解

    vertical-align用来设置垂直对齐方式,所有垂直对齐的元素都会影响行高 值: baseline | sub | super | top | text-top | middle | botto ...

  10. Google AdSense怎么在新窗口打开

    Google AdSense早在十年前就支持在新窗口打开了,为什么我的AdSense广告还在当前页面打开? 德顺查了一下,发现最早在2007年就有网站记载,谷歌AdSense开始试验新窗口打开功能. ...