"Java" Java Common face test questions (ii) Java collection Class __ Storage

Source: Internet
Author: User
Java Collection class diagram:

1. HashMap principle, and Hashtable differenceHashMap in Java store elements in the form of key-value pairs (key-value). HashMap requires a hash function that uses the hashcode () and Equals () methods to add and retrieve elements to the collection/from the collection. When the put () method is invoked, HashMap calculates the hash value of the key and then stores the key value pairs on the appropriate index in the collection. If the key already exists, value is updated to the new value. Some of the key features of HashMap are its capacity (capacity), load factor (loading factor), and expansion limit (threshold resizing).

Attach the put source code: [Java]  View Plain  copy public v put (k key, v value)  {      &NBSP;&NBSP;//&NBSP;HASHMAP allows null keys and null values to be stored.        //  When key is null, call the Putfornullkey method and place value in the first position of the array.        if  (key == null)             return putfornullkey (value);       //  Recalculate the hash value based on the keycode of the key.        int hash = hash (Key.hashcode ());        //  Search for the index of the specified hash value in the corresponding table.        int i = indexfor (hash, table.length);       //  if  Entry  at  i  index is not  null, iterate through the next element of the  e  element continuously.        for  (entry<k,v> e = table[i]; e !=  null; e = e.next)  {           Object k;            if  (e.hash == hash &&  (K = e.key)  ==  key | |  key.equals (k))  {                V oldValue = e.value;                e.value = value;                e.recordaccess (this);                return oldValue;           }        }       //  If the entry at the I index is NULL, it indicates that there is no entry here.        modcount++;      //This mod is for thread safety, as described in the following sections   &nbsP     //  adds key and value to the I index.        addentry (hash, key, value, i);        return null;  }  
AddEntry:
[Java]  View Plain  copy void addentry (int hash, k key, v value, int  Bucketindex)  {       //  get  entry at the specified  bucketIndex  index         Entry<K,V> e = table[bucketIndex];       // <strong><span style= "COLOR: #ff0000;" > Place the newly created  Entry  into the  bucketIndex  index and let the new  Entry  point to the original  entry  </ span></strong>       table[bucketindex] = new entry<k, V> (hash, key, value, e);       //  if  Map   key-value  the number of pairs exceeded the limit        if  (size++ >= threshold)        //  extends the  table  object to twice times the original length.            resize (2 * table.length);  }  
For more detailed principles, see: http://zhangshixi.iteye.com/blog/672697
difference: http://blog.csdn.net/shohokuf/article/details/3932967
HashMap allows keys and values to be null, while Hashtable does not allow keys or values to be null. Hashtable is synchronized, and HashMap is not. Therefore, HashMap is more suitable for single-threaded environments, while Hashtable is suitable for multi-threaded environments. HashMap provides a collection of keys that are available for application iterations, so HashMap is a quick failure (see below). On the other hand, Hashtable provides an enumeration of keys (enumeration). Hashtable is generally considered to be a legacy class. 2. Two ways to make HashMap into thread-safe

Method One: Return a new map via Collections.synchronizedmap (), the new map is thread-safe. This requires everyone to get used to programming on the interface, because the return is not hashmap, but a map implementation.

[Java] view plain copy

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.