Java basics, set, HashMap, source code parsing, javahashmap

Source: Internet
Author: User
Tags rehash

Java basics, set, HashMap, source code parsing, javahashmap

Most afraid, you think you know, but you still don't understand;

Look at the collections we are used to. through relevant definitions, source code, and analysis, we can deepen our understanding of them and improve our coding capabilities. We can be a programmer with a little understanding;

Make several common collection classes. HashMap

 

 

HashMap 1. Implementation of the Map interface based on the hash table. This implementation provides all optional ing operations and allows the use of null values and null keys. 2. Why does null key be allowed? In the put method, special processing can be performed for null values.
 1     private V putForNullKey(V value) { 2         for (Entry<K,V> e = table[0]; e != null; e = e.next) { 3             if (e.key == null) { 4                 V oldValue = e.value; 5                 e.value = value; 6                 e.recordAccess(this); 7                 return oldValue; 8             } 9         }10         modCount++;11         addEntry(0, null, value, 0);12         return null;13     }
3. Default size: 16. The critical factor is 0.75. When the critical factor is reached, the initial length will be doubled, that is, length * 2, and permanent is a multiple of 2, when an internal table is reached, the while loop is used to assign the original entry to the new entry (this step often affects performance)
1 // initialization size 2 static final int DEFAULT_INITIAL_CAPACITY = 16; 3 // 1 shifts left 30 bits 4 static final int MAXIMUM_CAPACITY = 1 <30; 5 // Critical Factor 6 static final float DEFAULT_LOAD_FACTOR = 0.75f; 7 8 9 void addEntry (int hash, K key, V value, int bucketIndex) {10 if (size> = threshold) & (null! = Table [bucketIndex]) {11Resize (2 * table. length); // The length is doubled for switching.12 hash = (null! = Key )? Hash (key): 0; 13 bucketIndex = indexFor (hash, table. length); 14} 15 16 createEntry (hash, key, value, bucketIndex); 17} 18 19 20 void resize (int newCapacity) {21 Entry [] oldTable = table; 22 int oldCapacity = oldTable. length; 23 if (oldCapacity = MAXIMUM_CAPACITY) {24 threshold = Integer. MAX_VALUE; 25 return; 26} 27 28 Entry [] newTable = new Entry [newCapacity]; 29 boolean oldAltHashing = useAltHashing; 30 useAltHashing | = sun. misc. VM. isBooted () & 31 (newCapacity> = Holder. ALTERNATIVE_HASHING_THRESHOLD); 32 boolean rehash = oldAltHashing ^ useAltHashing; 33Transfer (newTable, rehash );34 table = newTable; 35 threshold = (int) Math. min (newCapacity * loadFactor, MAXIMUM_CAPACITY + 1); 36} 37 38 // exchange element 39 voidTransfer (Entry [] newTable, booleanRehash){40 int newCapacity = newTable. length; 41For (Entry <K, V> e: table ){42While (null! = E ){43 Entry <K, V> next = e. next; 44 if (rehash) {45 e. hash = null = e. key? 0: hash (e. key); 46} 47 int I = indexFor (e. hash, newCapacity); 48 e. next = newTable [I]; 49 newTable [I] = e; 50 e = next; 51} 52} 53}
3. Due to the problem of hashmap resizing, set the length whenever possible when using hashmap to save a large number, otherwise it will affect the efficiency; 4. the inheritance relationship of hashmap is compared to the inheritance relationship of hashtable on the right. One is AbstractMap and the other is Dictionary 5. When the new key value of put is the same and the value is different, in hashmap, the entry will pay the new value to the value in the original key. The put method has a return value. When the value is replaced, the original value is returned. Otherwise, null is returned.
 1  public V put(K key, V value) { 2         if (key == null) 3             return putForNullKey(value); 4         int hash = hash(key); 5         int i = indexFor(hash, table.length); 6         for (Entry<K,V> e = table[i]; e != null; e = e.next) { 7             Object k; 8             if (e.hash == hash && ((k = e.key) == key || key.equals(k))) { 9                 V oldValue = e.value;10                 e.value = value;11                 e.recordAccess(this);12                 return oldValue;13             }14         }15 16         modCount++;17         addEntry(hash, key, value, i);18         return null;19     }
6. In comparison, it is not thread-safe hashtable, but hashmap is mainly about thread security. Many hashtable methods have synchronization settings for synchronized; 7. What are the methods for containkey containvalue Method 8 and hashmap traversal? Hashmap implements the map interface, so you can use keyset, entrySet (), values (), and Iterator <Map. entry <String, String> it = map. entrySet (). iterator (); (iterator interface implemented by map implements traversal)

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.