Java collections interview knowledge Lite

set

ArrayList and LinkedList difference

ArrayList LinkedList
Array Doubly linked list
Additions and deletions at the time when the expansion slow, fast query by index, search by subject index slow Additions and deletions fast, slow queries through the index, the index slowly through the object to check
For expansion when the element at the array can not accommodate the added no
After the expansion capacity of 1.5 times the original no

HashMap

  1. JDK 1.8 HashMap previously implemented is + arrays linked lists, hash function to obtain even better, it is difficult to achieve a uniform distribution element hundred percent. When a large number of elements in the HashMap are stored to the same bucket, there is a long chain under the barrel, this time is equivalent to a HashMap single chain, single chain if there are n elements, the time complexity is traversed O (n), completely lost its advantage. In view of this situation, JDK 1.8 introduced the red-black tree (lookup time complexity is O (logn)) to optimize this problem
  2. Why not thread safe? Multithreading may cover just PUT addpiece PUT operation; chain to form an annular expansion operation to make a data structure, an endless loop is formed
  3. The default size of the capacity is 16, the load factor is 0.75, when the HashMap size> 16 * will expansion (capacity and the load factor can be freely adjusted) 0.75 occurs.
  4. Why is the capacity of a multiple of 2? When lookup array elements according hashcode, far below the modulo behavior and performance, and 2 ^ n-1, and the operation performed to ensure the various elements corresponding hashcode can be uniformly distributed in the array

ConcurrentHashMap principle

http://www.jasongj.com/java/concurrenthashmap/

HashTable at each synchronous execution must lock the entire structure. ConcurrentHashMap locking is slightly granular. ConcurrentHashMap The hash table is divided into 16 barrels (default)
number is the maximum number of concurrent Segment, the default value is 16, may be changed once created can not be changed by the constructor, this value is the granularity of concurrency, each following segment management a table array, lock, when in fact the entire segment is locked

Java7

 

 

ConcurrentHashMap class contains two internal static type and HashEntry Segment. Encapsulation mapping table is used to key HashEntry / value pairs; Segment serve as locks for the role of each of the plurality of buckets Segment objects daemon entire hash map. Each barrel is linked by a number of objects up HashEntry list. A ConcurrentHashMap example comprises an array of a plurality of Segment objects.

Java8

 

 

  1. To further improve concurrency, gave up the sub-lock, lock level control in a more granular level table of elements, that is just to lock the head node of this list, and will not affect other elements of the table to read and write , concurrent benefit is more granular, less impact, so that better efficiency concurrency
  2. Use CAS + synchronized to ensure the realization of the put operation: If the array element corresponding to the Key is null, CAS operation is provided by the current value. If the Key corresponding array element (i.e., the root element of the tree or list header) is not null, then the lock request elements synchronized keyword used, then operation. If the chain length of the put operation such that the current exceeds a certain threshold, the list (addressing time complexity is O (N)) is converted into red-black trees (addressing time complexity is O (log (N)), the insertion operation is completed after all, if the number of elements greater than the current capacity (default 16) * load factor (0.75 default) the expansion proceeds.
发布了142 篇原创文章 · 获赞 31 · 访问量 2万+

Guess you like

Origin blog.csdn.net/qq_38905818/article/details/103803353