频道栏目
首页 > 程序开发 > 软件开发 > Java > 正文
(二)由浅入深java集合--Hashtable
2016-02-01 09:26:03         来源:LOVE JAVA  
收藏   我要投稿

什么时候使用Hashtable?

Hashtable是线程安全的,如果有多个线程访问相同的实例的可能时,可以选择使用Hashtable,反之,如果非线程安全的数据结构能带来更好的性能,HashMap更加灵活一些。

Hashtable的使用

代码见:

https://github.com/summerxhf/j2ee-demo/blob/master/HashTable-demo/src/main/java/HashTableDemo.java

Hashtable数据结构?

 

Hashtable的数据结构和HashMap的一样,是数组+单链表的数据结构,但又依赖于Enumeration接口,如下图所示。

 

Hashtable和HashMap区别?

 

HashMap and Hashtable both implement java.Util.Map interface but there are some differences that Java developers must understand to write more dfficient code. As of the java2 platform v1.2, Hashtable class was retrofitted to implement the Map interface, making it a member of the java Collections Framework.

 

 

不同点一

 

HashMap线程不安全,Hashtable线程安全.

One of the major differences between HashMap and Hashtable is that HashMap is non-synchronized whereas Hashtable is synchronized , which means Hashtable is thread-safe and can be shared between multiple threads but HashMap cannot be shared between multiple threads without proper synchronization. Java 5 introduced ConcurrentHashMap which is an alternative of Hashtable and provides better scalability than Hashtable in Java.Synchronized means only one thread can modify a hash table at one point of time .Basically , it means that any thread before preforming an update on a hashtable will have to acquire a lock on the object while others will wait for lock to be released.

 

不同点二

 

HashMap允许key为null,Hashtable不允许key为null

 

The HashMap class is roughly equivalent to Hashtable, except that it permits nulls. (HashMap allows null values as key and value whereas Hashtable doesn't allow nulls)

 

不同三

 

迭代器不同,HashMap 可以使用Iterator迭代,Hashtable一般使用enumerator迭代, 对于HashMap, 如果非迭代器本身在循环输出的时候增加或者移除元素,则会报异常。而Hashtable,则不会。

 

the third significant difference between HashMap VS Hashtable is that Iterator in the HashMap is a fail-fast iterator while the enumerator for the Hashtable is not and throw ConcurrentModificationException if any other Thread modifies the map structurally by adding or removing any element except Iterator's own remove() method .

 

for eg:

hashtable采用Iterator迭代

Hashtable hashtable = new Hashtable();
  Iterator iterator = hashtable.keySet().iterator();
  hashtable.put("key","value"); 
        while (iterator.hasNext()){
            System.out.println(iterator.next());
            hashtable.put("aa","aa");
            String key = iterator.next();
            System.out.println("方式三 key为:"+key + "value 为: " + hashtable.get(key));
        }

 

使用Enumeration,在循环的时候add 就不会报错。

 Enumeration enumeration = hashtable.keys();
        while(enumeration.hasMoreElements()){

            String key = enumeration.nextElement();
            hashtable.put("aa","aa");
            System.out.println(key);
            System.out.println(hashtable.get(key));

        }

\

 

不同点四

 

单线程中, HashMap的效率大于Hashtable

 

one more notable difference between Hashtable and HashMap is that because of thread-safety and synchronization Hashtable is much slower than HashMap if used in Single threaded environment. So if you don't need synchronization and HashMap is only used by one thread, it out preform Hashtable in Java .

 

不同点五

HashMap 不保证映射的顺序不变.

 

HashMap does not guarantee that the order of the map will remain constant over time .

 

 

总结

同胞的兄弟总有些不同,世界上没有两片完全相同的叶子,但是每片叶子都有其存在的价值,存在必合理,存在必有用。

点击复制链接 与好友分享!回本站首页
相关TAG标签
上一篇:JavaSE入门学习19:Java面向对象之抽象类
下一篇:Java判断文本文件编码格式以及读取
相关文章
图文推荐
文章
推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站