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

來源:互聯網
上載者:User

(二)由淺入深java集合--Hashtable
什麼時候使用Hashtable?Hashtable是安全執行緒的,如果有多個線程訪問相同的執行個體的可能時,可以選擇使用Hashtable,反之,如果非安全執行緒的資料結構能帶來更好的效能,HashMap更加靈活一些。Hashtable的使用代碼見:https://github.com/summerxhf/j2ee-demo/blob/master/HashTable-demo/src/main/java/HashTableDemo.javaHashtable資料結構? 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 .  總結同胞的兄弟總有些不同,世界上沒有兩片完全相同的葉子,但是每片葉子都有其存在的價值,存在必合理,存在必有用。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.