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. A Mini Locomotive(01背包变型)

    题目链接: https://vjudge.net/problem/POJ-1976 题目描述: A train has a locomotive that pulls the train with i ...

  2. C#读取“我的文档”等特殊系统路径及环境变量

    返回“我的文档”路径字符串 Environment.GetFolderPath(Environment.SpecialFolder.Personal) 本技巧使用GetFolderPath方法来获取指 ...

  3. WPF备忘录(5)怎样修改模板中的控件

    首先,想问大家一个问题,你们如果要给一个Button添加背景图片会怎么做?(呵呵,这个问题又点小白哈) 是这样吗? <Button Height="57" Horizonta ...

  4. Nodejs学习事件模块

    1.nodejs 版本为v6.2.0,events是node.js 最重要的模块,events模块只提供了一个对象EventEmitter,EventEmitter 的核心是事件发射与事件监听器.可以 ...

  5. AssemblyInfo.cs 详解

    前言 ? .net工程(包括Web和WinForm)的Properties文件夹下自动生成一个名为AssemblyInfo.cs的文件,一般情况下我们很少直接改动该文件.但我们实际上通过另一个形式操作 ...

  6. [javaSE] 看知乎学习工厂模式

    factory的“本质”就是根据不同的输入创建出不同类型的对象. 引入factory的原因就是你需要根据不同的输入创建不同类型的对象. 简单工厂模式相当于是一个工厂中有各种产品,创建在一个类中,客户无 ...

  7. Java虚拟机--虚拟机类加载机制

    虚拟机类加载机制 虚拟机把描述类的数据从Class文件加载到内存,并对数据进行校验.转换解析和初始化,最终形成可以被虚拟机直接使用的Java类型,这就是虚拟机的类加载机制. 类的生命周期如下: 加载 ...

  8. 批处理REG学习

    首先在批处理操作注册表之前,应该了解REG命令的使用方式,详情请参阅一下网址: https://www.jb51.net/article/30078.htm 从以上链接内容我们可以详细了解使用reg的 ...

  9. 了解java虚拟机—垃圾回收算法(5)

    引用计数器法(Reference Counting) 引用计数器的实现很简单,对于一个对象A,只要有任何一个对象引用了A,则A的引用计数器就加1,当引用失效时,引用计数器减1.只要对象A的引用计数器的 ...

  10. 了解java虚拟机—堆相关参数设置(3)

    堆相关配置 -Xmx 最大堆空间 -Xms 初始堆空间大小,如果初始堆空间耗尽,JVM会对堆空间扩容,其扩展上限为最大堆空间.通常-Xms与-Xmx设置为同样大小,避免扩容造成性能损耗. -Xmn 设 ...