Hashtable is widely used. hashmap is a class used to replace hashtable in the new framework. In other words, hashmap is recommended instead of hashtable. Maybe you think hashtable is very useful. Why not? Here we will briefly analyze their differences.
1. The hashtable method is synchronous, And the hashmap is not synchronized. Therefore, the difference between manual synchronization of hashmap in multithreading is like that of vector and arraylist.
You can check the source code of hashtable and find that except the constructor, all the public method declarations of hashtable have the synchronized keyword, while the source code of hashmap does not even have the shadow of synchronized. Of course, except for annotations.
2. hashtable does not allow null values (neither key nor value). hashmap allows null values (both key and value ).
First, let's look at an example of normal hashtable output:
[Java]
View plaincopyprint?
- Hashtable table = new hashtable ();
- Table. Put ("a-key", "a-value ");
- Table. Put ("B-key", "B-value ");
- Table. Put ("C-key", "C-value ");
Hashtable table = new Hashtable();table.put("a-key", "a-value");table.put("b-key", "b-value");table.put("c-key", "c-value");
The output is as follows:
[Java]
View plaincopyprint?
- A-key-a-Value
- C-key-C-Value
- B-key-B-Value
a-key - a-valuec-key - c-valueb-key - b-value
Let's look at another example of hashtable rejecting NULL:
[Java]
View plaincopyprint?
- Table. Put (null, "a-value ");
table.put(null, "a-value");
Exceptions after running are as follows:
[Java]
View plaincopyprint?
- Exception in thread "Main" Java. Lang. nullpointerexception
- At java. util. hashtable. Put (hashtable. Java: 399)
- At com. darkmi. sandbox. hashtabletest. Main (hashtabletest. Java: 20)
Exception in thread "main" java.lang.NullPointerExceptionat java.util.Hashtable.put(Hashtable.java:399)at com.darkmi.sandbox.HashtableTest.main(HashtableTest.java:20)
Hashmap example:
[Java]
View plaincopyprint?
- Hashmap map = new hashmap ();
- Map. Put (null, "a-value ");
- Map. Put ("B-key", null );
- Map. Put ("C-key", null );
HashMap map = new HashMap();map.put(null, "a-value");map.put("b-key", null);map.put("c-key", null);
After running, the output is as follows:
[Java]
View plaincopyprint?
- B-key-Null
- Null-a-Value
- C-key-Null
b-key - nullnull - a-valuec-key - null
PS: From the above example, we can find that hashtable is the same as hashmap: unordered storage.
3. The traversal methods of the two are similar. hashtable only has one more elements method than hashmap.
[Java]
View plaincopyprint?
- Enumeration em = table. Elements ();
- While (Em. hasmoreelements ()){
- String OBJ = (string) Em. nextelement ();
- System. Out. println (OBJ );
- }
Enumeration em = table.elements();while (em.hasMoreElements()) {String obj = (String) em.nextElement();System.out.println(obj); }
Hashtable and
Hashmap can return a collection using the values () method, and then traverse the collection:
[Java]
View plaincopyprint?
- Collection coll = map. Values ();
- Iterator it = Coll. iterator ();
- While (it. hasnext ()){
- String OBJ = (string) it. Next ();
- System. Out. println (OBJ );
- }
Collection coll = map.values();Iterator it = coll.iterator();while (it.hasNext()) {String obj = (String) it.next();System.out.println(obj);}
Both of them can also return a set through the entryset () method, and then traverse the process:
[Java]
View plaincopyprint?
- Set set = table. entryset ();
- Iterator it = set. iterator ();
- While (it. hasnext ()){
- Entry entry = (entry) it. Next ();
- System. Out. println (entry. getkey () + "-" + entry. getvalue ());
- }
Set set = table.entrySet();Iterator it = set.iterator();while (it.hasNext()) {Entry entry = (Entry) it.next();System.out.println(entry.getKey() + " - " + entry.getValue());}
4. hashtable uses enumeration and hashmap uses iterator
The following two points are compared from the internal implementation mechanism,
Learn more:
5. For different hash values, hashtable directly uses the hashcode of the object. The Code is as follows:
[Java]
View plaincopyprint?
- Int hash = key. hashcode ();
- Int Index = (hash & 0x7fffffff) % tab. length;
int hash = key.hashCode();int index = (hash & 0x7FFFFFFF) % tab.length;
Hashmap recalculates the hash value and replaces the modulo:
[Java]
View plaincopyprint?
- Int hash = hash (k );
- Int I = indexfor (hash, table. Length );
- Static int Hash (Object X ){
- Int H = x. hashcode ();
- H + = ~ (H <9 );
- H ^ = (H >>> 14 );
- H + = (H <4 );
- H ^ = (H >>> 10 );
- Return h;
- }
- Static int indexfor (int h, int length ){
- Return H & (length-1 );
int hash = hash(k);int i = indexFor(hash, table.length);static int hash(Object x) { int h = x.hashCode(); h += ~(h << 9); h ^= (h >>> 14); h += (h << 4); h ^= (h >>> 10); return h;}static int indexFor(int h, int length) { return h & (length-1);
6. The default size of the hash array in hashtable is 11, and the increase is in the old * 2 + 1 mode. The default size of the hash array in hashmap is 16, and it must be an index of 2.