HashMap, HashTable, Linkedhashmap and treemap usage and differences

Source: Internet
Author: User

Java defines an interface Java.util.Mapfor the mappings in the data structure; it has four implementation classes, namely HashMap Hashtable Linkedhashmap and TreeMap

The map is used to store key-value pairs and to get values based on the key, so the keys are not duplicated and the values can be duplicated.

  • HashMap is a hash table, the keys and values are not sorted;
  • The TreeMap is based on the red-black tree structure, and the key values are arranged in order.
  • Linkedhashmap preserves the order in which they are inserted;
  • The Hashtable is synchronous (and HashMap is not synchronized). So if the thread is secure, it should use HashMap instead of Hashtable, because Hashtable has additional overhead for synchronization.

1, HashMap

HashMap is the most commonly used map, which stores data according to the hashcode value of the key, which can be obtained directly from the key, with fast access speed.

HashMap allows a maximum of one record's key to be null, and the value of multiple records is not allowed to be null.

HashMap does not support thread synchronization, where multiple threads can write HashMap at any one time, which can result in inconsistent data. If synchronization is required, the COLLECTIONS.SYNCHRONIZEDMAP (HashMap map) method can be used to enable HASHMAP to have the ability to synchronize;

    System.out.println ("------hashmap unordered output------");  
    HashMap hsmap=new HashMap ();  
    hsmap.put ("3", "Value3")  
    hsmap.put ("1", "Value1");  
    hsmap.put ("2", "Value2");  
    hsmap.put ("B", "Valueb");  
& nbsp;   hsmap.put ("A", "Valuea");  
    Iterator it = Hsmap.entryset (). Iterator ();  
    while ( It.hasnext ()) { 
    map.entry e = (map.entry) it.next ();  
    SYSTEM.O Ut.println ("Key:" + e.getkey () + "--value:"   + e.getvalue ());  
   } 

  ------hashmap unordered output------ 
    Key:3--value: Value3  
    key:2--value:value2  
    key:1--value:value1  
    Key:b--value:valueb  
    key:a--value:valuea  

2, hashtable
< Span style= "LINE-HEIGHT:18PX; Font-family:verdana, ' Ms Song ', Arial, Arial, Microsoft Jacob Black, Helvetica, Sans-serif; > similar to HashMap, the difference is: < Span style= "LINE-HEIGHT:18PX; Font-family:verdana, ' Ms Song ', Arial, Arial, Microsoft Jacob Black, Helvetica, Sans-serif; > , which means that only one thread can write hashtable at any one time, so it also causes the Hashtale to be slow to write;

Sort by Key , by default, sorted in ascending order, or you can specify a sort comparer. When traversing TreeMap with Iteraor, the resulting record is ordered. TreeMap keys and values cannot be empty;

     System.out.println ("------treemap sort output------by key");  
    TreeMap temap=new TreeMap ();  
    temap.put ("3", "Value3");  
    temap.put ("1", "Value1")  
    temap.put ("2", "Value2");  
    temap.put ("B", "Valueb");  
    temap.put ("A", "Valuea");  
& nbsp;   Iterator tit = Temap.entryset (). Iterator ();  
    while (Tit.hasnext ()) { 
    map.entry e = (map.entry) tit.next (); &NBSP
    System.out.println ("Key:" + e.getkey () + "--value:" &NBSP;
    + e.get Value ());  
   }  

< Span style= "LINE-HEIGHT:18PX; Font-family:verdana, ' Ms Song ', Arial, Arial, Microsoft Jacob Black, Helvetica, Sans-serif; > 4, linkedhashmap 

 linkedhashmap Save the insertion Order of records , When traversing linkedhashmap with iterator, the first record must be inserted first. You can also use the parameters in the construction, sort by the number of applications, The Linkedhashmap key and value can be NULL ;

  will be slower than hashmap when traversing, but there is an exception, when the HashMap capacity is large, the actual data is small, the traverse may be slower than Linkedhashmap, because the Linkedhashmap traverse speed is only related to the actual data, is independent of capacity, and HashMap's traverse speed is related to his capacity

    System.out.println ("--linkedhashmap output according to the order of input--");  
    linkedhashmap lhsmap=new linkedhashmap ();  
    lhsmap.put ("3", "Value3");  
    lhsmap.put ("1", "Value1");  
    lhsmap.put ("2", "Value2");  
    lhsmap.put ("B", "Valueb");  
    lhsmap.put ("A", "Valuea");  
    Iterator lit = Lhsmap.entryset (). Iterator ();  
    while ( Lit.hasnext ()) { 
    map.entry e = (map.entry) lit.next ();  
    System . Out.println ("Key:" + e.getkey () + "--value:"   + e.getvalue ());  
    } < /span>

--linkedhashmap output According to the order of input--
Key:3--value:value3
Key:1--value:value1
Key:2--value:value2
Key:b--value:valueb
Key:a--value:valuea

HashMap, HashTable, Linkedhashmap and treemap usage and differences

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.