HashMap, HashTable, TreeMap difference and usage, hashmaptreemap

Source: Internet
Author: User

HashMap, HashTable, TreeMap difference and usage, hashmaptreemap

When I started learning HashTable, HashMap and TreeMap, I was dizzy and felt that the role was similar. However, I found many differences in practical use. You must note that the actual development depends on your needs.

Java defines an interface java. util. Map for ing in the data structure, and HashMap Hashtable and TreeMap are its implementation classes. Map is an object that maps keys to values. A ing cannot contain duplicate keys. Each key can only Map one value at most.

Hashmap is the most commonly used Map. It stores data based on the HashCode value of the key, and can directly obtain its value based on the key, with fast access speed. HashMap allows a maximum of Null keys for one record, and Null values for multiple records. HashMap does not support thread synchronization, that is, multiple threads can write HashMap simultaneously at any time; data inconsistency may occur. If synchronization is required, use the synchronizedMap method of Collections to synchronize HashMap.

Hashtable is similar to HashMap, but the main difference is 6 points.

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.

2. HashTable does not allow null values, neither key nor value. HashMap allows null values, and both key and value values. HashMap allows only one null value for the key value, because if the key value of hashmap is the same, the new key and value replace the old one.

3. HashTable has the same contains (Object value) function as containsValue (Object value.

4. HashTable uses Enumeration and HashMap uses Iterator.

5. 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.

6. For different hash values, HashTable directly uses the hashCode of the object.

TreeMap can sort the records stored by the key. By default, the record is sorted in ascending order. You can also specify a comparator. When you use Iterator to traverse the TreeMap, the obtained records are sorted in ascending order.

The following is a classic example of HashTable, HashMap, and TreeMap.

Package com. taobao. luxiaoting;

Import java. util. Map;

Import java. util. HashMap;

Import java. util. Set;

Import java. util. HashSet;

Import java. util. Iterator;

Import java. util. Hashtable;

Import java. util. TreeMap;

Class HashMaps

{

Public static void main (String [] args)

{

Map map = new HashMap ();

Map. put ("a", "aaa ");

Map. put ("B", "bbb ");

Map. put ("c", "ccc ");

Map. put ("d", "ddd ");

Iterator iterator = map. keySet (). iterator ();

While (iterator. hasNext ()){

Object key = iterator. next ();

System. out. println ("map. get (key) is:" + map. get (key ));

}

Hashtable tab = new Hashtable ();

Tab. put ("a", "aaa ");

Tab. put ("B", "bbb ");

Tab. put ("c", "ccc ");

Tab. put ("d", "ddd ");

Iterator iterator_1 = tab. keySet (). iterator ();

While (iterator_1.hasNext ()){

Object key = iterator_1.next ();

System. out. println ("tab. get (key) is:" + tab. get (key ));

}

TreeMap tmp = new TreeMap ();

Tmp. put ("a", "aaa ");

Tmp. put ("B", "bbb ");

Tmp. put ("c", "ccc ");

Tmp. put ("d", "ddd ");

Iterator iterator_2 = tmp. keySet (). iterator ();

While (iterator_2.hasNext ()){

Object key = iterator_2.next ();

System. out. println ("tmp. get (key) is:" + tmp. get (key ));

}

}

}

 

 

Result:

=========map=========map.get(key) is :dddmap.get(key) is :bbbmap.get(key) is :cccmap.get(key) is :aaa=========Hashtable=========tab.get(key) is :bbbtab.get(key) is :aaatab.get(key) is :dddtab.get(key) is :ccc=========TreeMap=========tmp.get(key) is :aaatmp.get(key) is :bbbtmp.get(key) is :ccctmp.get(key) is :ddd

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.