The underlying implementation principle of each implementation class of the collection

Summary of the main points of ArrayList implementation principle

references:

http://zhangshixi.iteye.com/blog/674856l
https://www.cnblogs.com/leesf456/p/5308358.html

  1. ArrayList is a mutable array asynchronous implementation of the List interface and allows all elements including null.
  2. The bottom layer is implemented using an array
  3. The collection is a variable-length array. When the array is expanded, the elements in the old array will be copied to the new array, and each time the array capacity increases by about 1.5 times its capacity, this operation is very expensive.
  4. With a Fail-Fast mechanism, the iterator will soon fail completely in the face of concurrent modifications, rather than risking arbitrary indeterminate behavior at some indeterminate time in the future
  5. The remove method will move the element subscripted to the end of the array forward by one unit, and empty the value of the last bit, which is convenient for GC

Summary of the main points of LinkedList implementation principle

: 文献 :
1. http://www.cnblogs.com/ITtangtang/p/3948610.htmll
2. https://www.cnblogs.com/leesf456/p/5308843.html

  1. LinkedList is an asynchronous implementation of a doubly linked list of the List interface and allows all elements including null.
  2. The underlying data structure is based on a doubly linked list, which we call a node
  3. An instance of the class Node corresponding to a doubly linked list node. Node contains member variables: prev, next, item. Among them, prev is the previous node of this node, next is the next node of this node, and item is the value contained in this node.
  4. Its search is divided into two halves, first determine which half of the linked list the index is in, and then go to the corresponding area to search, so that at most only half of the linked list can be found.

Summary of the main points of HashMap implementation principle

References: http://zhangshixi.iteye.com/blog/672697
References: http://blog.csdn.net/lizhongkaide/article/details/50595719

  1. HashMap is an asynchronous implementation of the Hash table-based Map interface that allows null values ​​and null keys, but does not guarantee the order of the map.
  2. The bottom layer is implemented using an array. Each item in the array is a singly linked list, that is, a combination of an array and a linked list. When the length of the linked list is greater than a certain threshold, the linked list is converted into a red-black tree, which reduces the query time of the linked list.
  3. HashMap treats key-value as a whole at the bottom layer, and this whole is a Node object. The bottom layer of HashMap uses a Node[] array to store all key-value pairs. When a Node object needs to be stored, its storage location in the array will be determined according to the hash algorithm of the key, and its storage location in the array will be determined according to the equals method. The storage location in the linked list at the location; when a Node needs to be taken out, it will also find its storage location in the array according to the hash algorithm of the key, and then take out the Node from the linked list at that location according to the equals method.
  4. When HashMap expands the array, it needs to recalculate the position of each element in the array after the expansion, which consumes a lot of performance.
  5. Using the Fail-Fast mechanism, the number of modifications is recorded through a modCount value, and the modification of the HashMap content will increase this value. During the iterator initialization process, this value will be assigned to the expectedModCount of the iterator. During the iteration process, it is judged whether modCount and expectedModCount are equal. If they are not equal, it means that another thread has modified the Map, and an exception is thrown immediately.

Summary of the main points of Hashtable implementation principle

References: http://blog.csdn.net/zheng0518/article/details/42199477

  1. Hashtable is a synchronous implementation of the Map interface based on the hash table, which does not allow null values ​​and null keys
  2. The bottom layer is implemented using an array. Each item in the array is a singly linked list, that is, a combination of an array and a linked list.
  3. Hashtable treats key-value as a whole at the bottom layer, and this whole is an Entry object. The bottom layer of Hashtable uses an Entry[] array to store all key-value pairs. When an Entry object needs to be stored, its storage location in the array will be determined according to the hash algorithm of the key, and its storage location in the array will be determined according to the equals method. The storage location in the linked list at the location; when an Entry needs to be taken out, it will also find its storage location in the array according to the hash algorithm of the key, and then take out the Entry from the linked list at that location according to the equals method.
  4. Synchronized is for the entire Hash table, that is, the entire table is locked each time for exclusive use by the thread

Summary of the main points of the implementation principle of ConcurrentHashMap

References: http://blog.csdn.net/zheng0518/article/details/42199477

  1. ConcurrentHashMap allows multiple modification operations to be performed concurrently. The key is to use lock separation technology.
  2. It uses multiple locks to control modifications to different segments of the hash table, each segment is actually a small hashtable with its own lock. As long as multiple concurrency occurs on different segments, they can proceed concurrently.
  3. ConcurrentHashMap treats key-value as a whole at the bottom layer, and this whole is an Entry object. The bottom layer of Hashtable uses an Entry[] array to store all key-value pairs. When an Entry object needs to be stored, its storage location in the array will be determined according to the hash algorithm of the key, and its storage location in the array will be determined according to the equals method. The storage location in the linked list at the location; when an Entry needs to be taken out, it will also find its storage location in the array according to the hash algorithm of the key, and then take out the Entry from the linked list at that location according to the equals method.
  4. Unlike HashMap, ConcurrentHashMap uses multiple sub-Hash tables, that is, segments (Segment)
  5. ConcurrentHashMap fully allows multiple read operations to be performed concurrently, and read operations do not require locking. If traditional techniques are used, as implemented in HashMap, if elements can be added or removed in the middle of the hash chain, read operations without locking will result in inconsistent data. ConcurrentHashMap implementation technology is to ensure that HashEntry is almost immutable.

Summary of the main points of HashSet implementation principle

References: http://zhangshixi.iteye.com/blog/673143l

  1. HashSet is backed by a hash table (actually a HashMap instance), the iteration order of the set is not guaranteed, and null elements are allowed.
  2. Based on HashMap implementation, the API also encapsulates the behavior of HashMap, please refer to HashMap

Summary of the main points of LinkedHashMap implementation principle

References: http://zhangshixi.iteye.com/blog/673789l

  1. LinkedHashMap inherits from HashMap, the bottom layer uses a hash table and a doubly linked list to store all elements, and it is asynchronous, allowing the use of null values ​​and null keys.
  2. The basic operation is similar to that of the parent class HashMap. By overriding the related methods of HashMap and redefining the element Entry stored in the array, it realizes its own linked list feature. In addition to saving the reference of the current object, the Entry also saves the reference of its previous element before and the next element after, thus forming a doubly linked list.

Summary of the main points of the implementation principle of LinkedHashSet

References: http://zhangshixi.iteye.com/blog/673319l

  1. For LinkedHashSet, it inherits from HashSet and is implemented based on LinkedHashMap. The bottom layer of LinkedHashSet uses LinkedHashMap to save all elements. It inherits from HashSet, and all its methods and operations are the same as HashSet.


Reference :: https://blog.csdn.net/qq_25868207/article/details/55259978

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325585810&siteId=291194637