The default mechanism for capacity expansion as well as a collection of commonly used Java

List of default related to capacity expansion and mechanisms

ArrayList

  • ArrayList default capacity is 10
  • ArrayList maximum capacity Integer.MAX_VALUE - 8
  • ArrayList expansion mechanism, the expansion of 1.5 times the original length of the array. If the size is smaller than the size of the actual needs of the expansion, to expand the array size to the actual needs

Vector

Vector is an internal ArrayList implementations are thread-safe version of an array implementation. Vector achieved by modifying the thread synchronization functionality before using synchronized method 

  • - Vector default capacity is 10 
  • - Vector maximum capacity Integer.MAX_VALUE - 8 
  • - Vector expansion mechanism, if the user does not specify the expansion step, 2-fold expansion of the original length of the array, designated by the user or the expansion step expansion. If the size is smaller than the size of the actual needs of the expansion, to expand the array size to the actual needs

Stack

Stack inherited from Vector. Added synchronization push (E e), pop (), peek () method, with the default capacity and expansion mechanisms Vector 

  • - Stack default capacity is 10 
  • - Stack maximum capacity Integer.MAX_VALUE - 8 
  • - Stack expansion mechanism, if the user does not specify the expansion step, 2-fold expansion of the original length of the array, designated by the user or the expansion step expansion. If the size is smaller than the size of the actual needs of the expansion, to expand the array size to the actual needs

CopyOnWriteArrayList

CopyOnWriteArrayList array collection is thread synchronization. CopyOnWriteArrayList usage scenario primarily multi-threaded environment, query, traversal operation significantly more than add, delete operation. 

  • - CopyOnWriteArrayList default capacity is zero, starting from 0 
  • - CopyOnWriteArrayList not specify a maximum capacity (suitable for use in the scene of frequent query operation, the capacity of small change) 
  • - CopyOnWriteArrayList expansion mechanism, each +1

LinkedList

LinkedList is a doubly-linked list implementation. There is no requirement for capacity and does not need expansion

 

Queue-related default capacity and expansion mechanism

ArrayBlockingQueue

ArrayBlockingQueue is based on an array of thread-safe implementation bounded queue. Its capacity is passed in user. (Internal use ReentrantLock implement thread synchronization)

ConcurrentLinkedQueue

ConcurrentLinkedQueue is based on unbounded thread-safe queue singly-linked list implementation. (Internal thread synchronization is achieved using CAS optimistic locking)

DelayQueue、PriorityQueue

Non-thread-safe unbounded queue.

LinkedBlockingQueue

LinkedBlockingQueue is based on unbounded thread-safe queue singly-linked list implementation. (Internal takeLock putLock and separate read and write locks for)

 

Map and capacity expansion related to default mechanism

HashMap

HashMap is an array and a linked list / red-black tree-based implementation. HashMap capacity must be a power of two (because the (n-1) & hash is a modulo operation, n must be a power of 2) 

  • - HashMap default capacity is 16 
  • - HashMap maximum capacity of the 30th power of 2 
  • - HashMap expansion mechanism, expanded to twice the original array

ConcurrentHashMap

// all

Hashtable

  • Hashtable default size is 11 (default size is 11 Hashtable because good dispersion effect in addition to the (approximate) primes remainder of :)
  • Hashtable maximum capacity Integer.MAX_VALUE - 8
  • Hashtable expansion mechanism, expanded to twice the original array +1

LinkedHashMap

With the expansion mechanism inherited from HashMap HashMap

TreeMap

TreeMap implemented by a red-black tree, there is no limit capacity

WeakHashMap

同HashMap

Guess you like

Origin blog.csdn.net/qq_42239765/article/details/90576096