(二)由浅入深java集合--Hashtable

本文详细对比了Hashtable与HashMap之间的五大不同之处,包括线程安全性、空键值支持、迭代器类型及性能差异等,并提供了代码示例。


什么时候使用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<String,String> hashtable = new Hashtable<String,String>();
  Iterator<String> 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<String> 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,在循环的时候put就会报错如下,hashMap.put("aa","aa");
 //向map中存入值.
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("key","value");
        for(int i = 0 ;i<66;i++){
            map.put("key"+i,"key"+i);
        }

        //1 在不知道key和value的情况下,取出map中的值.(不适用泛型)
        Iterator iterator = map.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry entry = (Map.Entry)iterator.next();
            map.put("aa","aa");
            Object Key = entry.getKey();
            Object value = entry.getValue();
            System.out.println("1 循环输出key为:"+Key+" value为:"+value);
        }





不同点四


       单线程中, 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 . 


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





评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值