Difference between HashMap and Hashtable | HashMap Vs Hashtable
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.
Filed Under: Java, Java InterviewTagged With: difference, hashmap, hashmap and hashtable, hashtable, synchronization, thread-safe
Difference between HashMap and Hashtable | HashMap Vs Hashtable的更多相关文章
- HashSet HashTable HashMap的区别 及其Java集合介绍
(1)HashSet是set的一个实现类,hashMap是Map的一个实现类,同时hashMap是hashTable的替代品(为什么后面会讲到). (2)HashSet以对象作为元素,而HashMap ...
- Hashtable,HashMap实现原理
http://blog.csdn.net/czh0766/article/details/5260360 昨天看了算法导论对散列表的介绍,今天看了一下Hashtable, HashMap这两个类的源代 ...
- C# Hashtable 使用说明 以及 Hashtable和HashMap的区别
一,哈希表(Hashtable)简述 在.NET Framework中,Hashtable是System.Collections命名空间提供的一个容器,用于处理和表现类似key/value的键值对,其 ...
- java该HashTable,HashMap和HashSet
同一时候我们也对HashSet和HashMap的核心方法hashcode进行了具体解释,见<探索equals()和hashCode()方法>. 万事俱备,那么以下我们就对基于hash算法的 ...
- 多线程之Map:Hashtable HashMap 以及ConcurrentHashMap
1.Map体系参考:http://java.chinaitlab.com/line/914247.htmlHashtable是JDK 5之前Map唯一线程安全的内置实现(Collections.syn ...
- 为什么HashMap线程不安全,Hashtable和ConcurrentHashMap线程安全
HashMap源码 public V put(K key, V value) { return putVal(hash(key), key, value, false, true); } final ...
- HashTable & HashMap & ConcurrentHashMap 原理与区别
一.三者的区别 HashTable HashMap ConcurrentHashMap 底层数据结构 数组+链表 数组+链表 数组+链表 key可为空 否 是 否 value可为空 否 是 否 ...
- Hashtable,HashMap,TreeMap有什么区别?Vector,ArrayList,LinkedList有什么区别?int和Integer有什么区别?
接着上篇继续更新. /*请尊重作者劳动成果,转载请标明原文链接:*/ /*https://www.cnblogs.com/jpcflyer/p/10759447.html* / 题目一:Hashtab ...
- 10 HashMap,Map.Entry,LinkedHashMap,TreeMap,Hashtable,Collections类
Map集合的功能概述 添加功能 * V put(K key,V value):添加元素. * 如果键是第一次存储,就直接存储元素,返回null * 如果键不 ...
随机推荐
- B+树原理及mysql的索引分析
转自:http://blog.csdn.net/qq_23217629/article/details/52512041 B+/-Tree原理 B-Tree介绍 B-Tree是一种多路搜索树(并不是二 ...
- nginx 跳转系列
1.nginx强制跳转https配置,通过http状态吗实现,http状态吗地址: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/3 ...
- Servlet中forward和redirect的区别
forward方式:request.getRequestDispatcher("/somePage.jsp").forwardrequest, response); re ...
- Redhat6.8安装Oracle11g下遇到两个问题记录
问题一: 刚刚安装完毕Oracle之后,尝试sqlplus登陆报错,TNS:net service name is incorrectly specified 参考文章:关于环境变量ORACLE_SI ...
- 阿里云服务器搭建SVN
简单步骤介绍 1:安装svn apt-get install subversion 2. 开启svn服务器 svnserve -d 检查是否开启:ps aux | grep svnserve 若出现如 ...
- ApiGen安装
# 首先,下载ApiGen(http://apigen.org/apigen.phar) # *nix系统 下载phar文件后, 移动到PATH目录中,以保证全局有权限调用 $ mv apigen.p ...
- C#快速读写文件
一.写入 //在应用程序当前目录下的File1.txt文件中追加文件内容,如果文件不存在就创建,默认编码 File.AppendAllText("File1.txt", " ...
- Android自定义Aop的Gradle Plugin
[上一篇文章]中讲解了如何在Android使用AOP,会发现在Gradle配置aop会比较麻烦,每个module使用了aop都需要配置.接下来看如何简化配置. 1.创建Module 首先,需要建立一个 ...
- RxJava1升级到RxJava2的注意事项
1.package更改 rx1包名由原来的rx.xxx更改为io.reactivex.xxx,并且在同一个module之下,rx1和rx2是不兼容的. 2.背压支持 RxJava在1.0只有一个个观察 ...
- equals与hashcode区别
哈希码: hashCode的作用是用来获取哈希码,也可以称作散列码.实际返回值为一个int型数据.用于确定对象在哈希表中的位置. Object中有hashcode方法,也就意味着所有的类都有has ...