HashMap与HashTable区别(Differences between HashMap and Hashtable)
来源:互联网 发布:外国身份证 知乎 编辑:程序博客网 时间:2024/05/10 22:57
Differences between HashMap and Hashtable?
如何来选用:
如果同步不是主要问题,推荐使用HashMap,如果同步问题是个问题,应该参照ConcurrentHashMap这个类。
There are several differences between HashMap and Hashtable in Java:
Hashtable is synchronized, where as HashMap is not. This makes
HashMap
better for non-threaded applications, as unsynchronized Objects typically perform better than synchronized ones.Hashtable
does not allow null keys or values.HashMap
allows one null key and any number ofnull
values.One of HashMap's subclasses is LinkedHashMap, so in the event that you'd want predictable iteration order (which is insertion order by default), you could easily swap out the
HashMap
for aLinkedHashMap
. This wouldn't be as easy if you were usingHashtable
.
Since synchronization is not an issue for you, I'd recommend HashMap
. If synchronization becomes an issue, you may also look at ConcurrentHashMap.
类ConcurrentHashMap:
ConcurrentHashMap和
java.util.HashTable
类非常相似, ConcurrentHashMap
比 HashTable
有更好的并发性。 ConcurrentHashMap在读取数据的时候
不会给 Map加锁
。除此之外, ConcurrentHashMap
在写入数据的时候,不锁住整个。 它只对Map中将要写入的数据的那一部分内存加锁,
另一个不同点是如果ConcurrentHashMap在迭代的过程中改变了数据,ConcurrentHashMap
不抛出ConcurrentModificationException
异常。迭代器 Iterator
不是设计来为多线程使用的。
例子:
ConcurrentMap concurrentMap = new ConcurrentHashMap();concurrentMap.put("key", "value");Object value = concurrentMap.get("key");
- HashMap与HashTable区别(Differences between HashMap and Hashtable)
- Differences between HashMap and Hashtable
- Differences between hashmap and hashtable
- 004:Differences between HashMap and Hashtable?
- hashmap and hashtable 区别
- HashMap 与HashTable 区别
- HashMap与HashTable区别
- Hashtable与HashMap区别
- HashMap与HashTable区别
- hashTable与HashMap区别
- HashMap与HashTable区别
- HashMap与Hashtable区别
- HashMap与HashTable区别
- hashMap与hashTable区别
- HashMap与Hashtable区别
- HashTable与HashMap区别
- hashMap与HashTable区别
- hashmap与hashtable区别
- 给独立开发者的经验分享——领域知识
- Qt安装教程
- 浅谈算法时间复杂度
- Starling Feathers Controls ButtonGroup
- c++堆和栈
- HashMap与HashTable区别(Differences between HashMap and Hashtable)
- ArcGIS教程:网络分析工作流
- 64位Win7下安装与配置PHP环境【Apache+PHP+MySQL】
- ifconfig lo的作用
- POJ3414——Pots
- this project is not a myeclipse hibernate project . assuming hibernate 3 cap
- 体验JAVA中的构造函数
- 日志字段提取优化方案
- Cocos2d-x:制作一个动态的精灵