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. mysql 获取自增主键

    MyBatis 3.2.6插入时候获取自增主键方法有二 以MySQL5.5为例: 方法1: <insert id="insert" parameterType="P ...

  2. 【IT笔试面试题整理】二叉搜索树转换为双向链表

    [试题描述] 将二叉搜索树转换为双向链表 对于二叉搜索树,可以将其转换为双向链表,其中,节点的左子树指针在链表中指向前一个节点,右子树指针在链表中指向后一个节点. 思路一: 采用递归思想,对于二叉搜索 ...

  3. 解决U盘拷贝时提示文件过大问题(不能拷贝超过4个g的文件)

    为什么一个16G的U盘却拷不进一个4G大点的文件呢,想必很多朋友们都在疑问? 其实这跟U盘的磁盘格式有关,目前市面上常用的U盘大都是FAT32格式,我们可以查看U盘属性. 那么FAT32是什么呢? F ...

  4. [九省联考2018] 一双木棋 chess

    Description 菲菲和牛牛在一块n 行m 列的棋盘上下棋,菲菲执黑棋先手,牛牛执白棋后手. 棋局开始时,棋盘上没有任何棋子,两人轮流在格子上落子,直到填满棋盘时结束. 落子的规则是:一个格子可 ...

  5. core Animation之CAKeyframeAnimation(关键帧动画)

    CABasicAnimation的区别是:CABasicAnimation只能从一个数值(fromValue)变到另一个数值(toValue),而CAKeyframeAnimation会使用一个NSA ...

  6. WCF 学习总结1 -- 简单实例

    从VS2005推出WCF以来,WCF逐步取代了Remoting, WebService成为.NET上分布式程序的主要技术.WCF统一的模型整合了以往的 WebService.Remoting.MSMQ ...

  7. 关于.net程序集引用不匹配的问题

    今天启动asp.net mvc 程序,其中也用到了web api ,autofac等,为了版本兼容性问题,将mvc和 web api 的版本控制到5.2.0.0,Newtonsoft.Json 的版本 ...

  8. SQL Server T—SQL 存储过程 触发器

    一.存储过程 存储过程是一组编译在单个执行计划中的T-SQL语句 存储过程:就像函数一样的会保存在数据库中(可编程性) 存储过程的优点: 1.允许模块化程序设计 2.允许更快执行如果某操作需要大量T- ...

  9. oracle中数据类型number(m,n)

    oracle中数据类型number(m,n)中m表示的是所有有效数字的位数,n表示的是小数位的位数.m的范围是1-38,即最大38位. 1> .NUMBER类型细讲:Oracle   numbe ...

  10. First Date (hnoj12952)日期计算

    First Date Time Limit: 3000ms, Special Time Limit:7500ms, Memory Limit:65536KB Total submit users: 7 ...