Java JDK Source code parsing

Source: Internet
Author: User

Hashmap

Using the Java language for system development, the use of a more data structure hashmap, it is [Key,value], the storage, through key can quickly find the corresponding value value, but key,value cannot be the basic type of Java (int, Float,double,long,short,byte) must be a composite type (string,double ...). is not thread safe.

      Storage data format: Default 16 buckets, each bucket corresponding to a hash value, in the form of an array of 16 buckets stored sequentially, after all the key is hashed, and then hash& (the number of barrels 1) to get the corresponding bucket array position, Then the [key,value] stored in the bucket; query time: Through the key query, first through the Key,hash, find the corresponding bucket O (1), if the bucket below only one data, or no, query time the fastest O (1), otherwise traversing the bucket below the list of data, Until the corresponding key is found, or all the chains underneath the bucket are traversed. Delete, add only need to move the linked list of pointers, do not need to move the memory data,       When the current number exceeds the threshold, the need to re-apply a piece of memory (bucket array), is the current memory (bucket array) of twice times, the existing data also need to hash & (Number of barrels-1) Find the location of the new bucket and store it.  Hashtable    On the basis of hashmap, add multithreading concurrency control, add synchronized keyword in HashMap method.  Hashset    HashMap A layer of encapsulation, because the structure of external performance only stored key, the default Value=new Object (), for the HashMap key will only exist one copy.  Linkhashmap    Since the data in the HashMap is stored in different buckets, it is not necessarily the order of the data to be added when the data is traversed on the HashMap, linkhashmap on this basis, Add two pointers to each stored data, and after points to the next data, Befort points, each time a new data is added, the pointer is changed, the map is traversed, the two-way list is traversed, you can guarantee the order.  currenthashmap    Thread-safe HashMap, HashMap is rewritten, HashMap uses an array of linked lists to store the data, Currenthashmap in the array chain and also encapsulates a layer, A CURRENTHASHMAP has multiple shards, each of which corresponds to an array of linked lists, with multiple threads of concurrency control over the operation of the same shard, in effect, a lock (reentrant lock) on each shard, compared to the concurrency control of Hashtable. The structure for multiple threads to access different fragments, do not wait for each other, thereby reducing the granularity of the lock, improve efficiency.  treemap    stores data in the form of a tree, each node contains pointers to the parent node, pointers to the left node, pointers to the right node, and small and right nodes on the left node. This form of storage, insertion and deletion, can quickly find the corresponding location, high efficiency.      

Java JDK Source code parsing

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.