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. ASP.NET MVC 表格操作

    Beginners Guide for Creating GridView in ASP.NET MVC 5 http://www.codeproject.com/Articles/1114208/B ...

  2. Java NIO系列教程(九) ServerSocketChannel

    Java NIO中的 ServerSocketChannel 是一个可以监听新进来的TCP连接的通道, 就像标准IO中的ServerSocket一样.ServerSocketChannel类在 jav ...

  3. linq中last或者lastordefault不存在的问题

    在使用linq访问数据库的时候发现first以及firstordefault都存在,但是last以及lastordefault不存在.上网找寻一番发现是last只在linq to object中实现了 ...

  4. Redhat6.8安装Oracle11g下遇到两个问题记录

    问题一: 刚刚安装完毕Oracle之后,尝试sqlplus登陆报错,TNS:net service name is incorrectly specified 参考文章:关于环境变量ORACLE_SI ...

  5. C++语言------顺序表实现,用动态数组的方法

    C++ 中常用的一些东西,通过使用动态数组来实现顺序表, 掌握了一下知识点: 1.预处理有三中方法 宏定义,文件包含,条件编译 2.使用同名的变量时,可以在外层使用命名空间 类解决变量名重定义的错误 ...

  6. Mouse单击高亮GridView数据行

    有网友需要对GridView控件作一些操作.不过有些复杂,Insus.NET细分他的要求,一步一步来实现.不过细分的每一步,亦是一个小功能.因此Insus.NET就单独实现,然后一起结合起来,就是可以 ...

  7. Spring基础(9) : 自动扫描

    一  配置xml方式:扫描com包下的bean <?xml version="1.0" encoding="UTF-8" ?> <beans ...

  8. NPOI 之导入导出

    转自https://www.cnblogs.com/zuowj/archive/2015/05/04/4475663.html转别人的,做了一点点改动 using NPOI.HSSF.UserMode ...

  9. python学习之老男孩python全栈第九期_day002作业

    1. 判断下列逻辑语句的True,False.(1) 1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6Tru ...

  10. webstorm激活

    选择 License server http://idea.imsxm.com/ http://idea.iteblog.com/key.php (2016.11.16) http://v2mc.ne ...