From this post :http://www.importnew.com/7010.html

HashMap and Hashtable The comparison is Java Frequently asked questions in an interview , It is used to test whether the programmer can correctly use the collection class and whether he can use a variety of ideas to solve problems .HashMap How it works 、ArrayList And Vector The comparison and the question is about Java The most classic problem of set framework .Hashtable It's an obsolete collection class , Exist in Java API It's been a long time . stay Java 4 It's rewritten in , Realized Map Interface , So since then it has become Java Part of the collection framework .Hashtable and HashMap stay Java It's quite easy to be asked in an interview , It has even become the most frequently asked question in the set frame interview , So in any Java Before the interview , Don't forget to prepare this question .

In this article , We will not only see HashMap and Hashtable The difference between , You'll also see the similarities between them .

HashMap and Hashtable The difference between

HashMap and Hashtable It's all done Map Interface , But before you decide which one to use, you need to find out the difference between them . The main difference is : Thread safety , Sync (synchronization), And speed .

  1. HashMap Almost equal to Hashtable, except HashMap Right and wrong synchronized Of , And can accept null(HashMap It can be accepted as null Key value of (key) And the value (value), and Hashtable No way. ).
  2. HashMap Right and wrong synchronized, and Hashtable yes synchronized, It means Hashtable It's thread safe , Multiple threads can share one Hashtable; And without the right synchronization , Multiple threads are not allowed to share HashMap Of .Java 5 Provides ConcurrentHashMap, It is HashTable An alternative , Than HashTable Better scalability .
  3. Another difference is HashMap The iterator (Iterator) yes fail-fast iterator , and Hashtable Of enumerator Iterator is not fail-fast Of . So when other threads change HashMap Structure ( Add or remove elements ), It will be thrown out. ConcurrentModificationException, But the iterator itself remove() Method does not throw ConcurrentModificationException abnormal . But it's not a certain behavior , Want to see JVM. This one is also Enumeration and Iterator The difference between .
  4. because Hashtable It's thread safe as well synchronized, So in a single threaded environment it's better than HashMap slower . If you don't need synchronization , Just a single thread , So use HashMap Better performance than Hashtable.
  5. HashMap There is no guarantee that over time Map The order of the elements in is constant .

Some important terms to note :

1) sychronized This means that only one thread at a time can change Hashtable. That is to say, any thread needs to update Hashtable The synchronization lock must be obtained first , Other threads can't get the synchronization lock update again until the synchronization lock is released Hashtable.

2) Fail-safe and iterator Iterator related . If a collection object is created Iterator perhaps ListIterator, Then other threads try to “ Structurally ” Change the collection object , It will be thrown out. ConcurrentModificationException abnormal . But other threads can use set() Method is allowed to change collection objects , Because it didn't come from “ Structurally ” Change the collection . But if the structure has been changed , Call again set() Method , It will be thrown out. IllegalArgumentException abnormal .

3) Structural changes refer to deleting or inserting an element , This will affect map Structure .

Can we get HashMap Sync ?

HashMap You can synchronize with the following statement :
Map m = Collections.synchronizeMap(hashMap);

Conclusion

Hashtable and HashMap There are several major differences : Thread safety and speed . Use only when you need full thread safety Hashtable, And if you use Java 5 Or more , Please use ConcurrentHashMap Well .

HashTable vs ConcurrentHashMap The difference between .

As mentioned above, it can be done through Collections.synchronizeMap(hashMap) To get a similar HashTable Of synchronized thread-safe structure .

But whether this structure or HashTable Compared with ConcurrentHashMap It's all different . The main difference is to achieve synchronized Methods , HashTable It's the whole Table Lock the .

but ConcurrentHashMap It's just part of the lock . Usually , Yes 16 strip thread Simultaneous operation , When one of them thread It needs to be locked ConcurrentHashMap It's just part of the lock .

Java Main Differences between HashMap HashTable and ConcurrentHashMap More articles about

  1. Java Main Differences between HashMap and HashTable

    From this post :http://www.importnew.com/7010.html HashMap and Hashtable The comparison is Java Frequently asked questions in an interview , It is used to test whether the programmer can use the collection class correctly and whether it can ...

  2. HashMap HashTable and ConcurrentHashMap The difference between

    HashMap and Hashtable It's all done Map Interface , The main differences are : Thread safety , Sync (synchronization), And efficiency . HashMap and Hashtable There's basically no difference , except HashMa ...

  3. Java Main Differences between Java and C++

    Reprinted from :http://www.cnblogs.com/springfor/p/4036739.html C++ supports pointers whereas Java does not. But ...

  4. [Java aggregate ] Understand thoroughly HashMap,HashTable,ConcurrentHashMap The connection between .

    notes : Today I read an article about hashMap,hashTable,concurrentHashMap A very thorough article , Thanks for sharing . Original address : http://blog.csdn.net/zhanger ...

  5. Java aggregate ——HashMap、HashTable as well as ConCurrentHashMap Comparison of similarities and differences

    0.  Preface HashMap and HashTable A simpler answer is : (1)HashMap It's not thread safe ,HashTable It's thread safe . (2)HashMap Both the key and the value of are allowed null There is , and H ...

  6. Java aggregate ——HashMap,HashTable,ConcurrentHashMap difference

    Map:“ Key value ” Abstract interface to mapping . The mapping does not include duplicate keys , A key corresponds to a value . SortedMap: Ordered key value pair interface , Inherit Map Interface . NavigableMap: Inherit SortedMap, With a search for a given ...

  7. java Interview record 3 :hashmap、hashtable、concurrentHashmap、ArrayList、linkedList、linkedHashmap、Object Class 12 Member method 、 Message queue MQ The type of

    Oral questions 1.HashMap Principle ?( Array + One way linked list .put.get.size Method ) Non-thread safety :(1)hash Conflict : Multithreads operate simultaneously at one time hashmap And implement put In operation , Maybe two key Of hash ...

  8. HashMap、Hashtable、ConcurrentHashMap、ConcurrentSkipListMap Contrast and java Contract issuance (java.util.concurrent)

    One . Basic popularization Interface (interface) class (class) Inheritance class Implemented interface Array √ Collection √ Set √ Collection List √ Collection Map ...

  9. Java Collection Series 14 And Map summary (HashMap, Hashtable, TreeMap, WeakHashMap And so on )

    Summary learned Map The whole content of , Let's go back Map The frame diagram of . This chapter includes : The first 1 part Map Summarize section 2 part HashMap and Hashtable Similarities and differences 3 part HashMap and WeakHashMap similarities and differences turn ...

Random recommendation

  1. Leetcode Move Zeros

    Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...

  2. Error resolution error while loading shared libraries: libXXX.so.X: cannot open shared object file: No such file

    from :http://blog.csdn.net/david_xtd/article/details/7625626 Premise :ubuntu-debug The machine goes up to SVN submitted pdu-IVT, I want to work on other ordinary machines ...

  3. JavaScript senior Object oriented programming ( Two )《JavaScript Advanced programming ( The third edition )》

    Two . Inherit OO Is the most attractive concept of object-oriented language . General OO Both languages implement two kinds of inheritance , Interface inheritance and implementation inheritance . Interface inheritance inherits only method signatures , And the actual inheritance inherits the actual method . And in the JS in , Function has no signature , So it's impossible to implement interface inheritance . ...

  4. python requests Based on learning

    First ,Python In the standard library  urllib2  Modules provide most of what you need HTTP function , But it's API unfriendly . It's for another era . Another Internet created . It takes a lot of work , It even covers all kinds of methods , Come on ...

  5. Notepad++ How to set auto wrap and line width in

    view-->word wrap; setting->preference-->vertical edge settings; Notepad++ How to set auto wrap and line width in  http ...

  6. Oracle to_char Format function Show milliseconds

    racle How to display milliseconds   date Type can only be accurate to seconds , To be precise in milliseconds , Need to use timestamp type .   Application, for example, :   give an example 1:   select to_char(systimestamp,'yy ...

  7. Java New knowledge series 8、 ... and

    What is a deadlock , The cause and necessary condition of deadlock :       [] What is a deadlock , The cause and necessary condition of deadlock : Deadlock : The cause of deadlock is that the process is waiting for some resources occupied by other processes , And its own resources are waiting for other processes , It creates an endless cycle . There is a deadlock ...

  8. 10 Big essential Intellij plug-in unit , Greatly improve your work efficiency

    from : https://blog.csdn.net/qq1404510094/article/details/80379375 1. .ignore Generate all kinds of ignore file , A key to create git igno ...

  9. NK Internship project configuration

    1. Copy eclipse And projects 2. To configure tomcat6, Now we can only use tomcat6 3.tomcat6 plug-in unit eclipse Yes, by default , Just configure http://blog.csdn.net/u014079 ...

  10. go Language net package rpc The use of remote calls

    One . be based on http Of RPC Server side : package main; import ( "net/rpc" "net/http" "log" ) ...