ArrayList
Implementation principle: implemented by an array. Elements are ordered and repetitions are allowed.
// The method of increasing the length can be seen to create a new array and pass in the old array and the new array length. private void grow( int minCapacity) { // overflow-conscious code int oldCapacity = elementData.length; int newCapacity = oldCapacity + (oldCapacity >> 1 ); if (newCapacity - minCapacity < 0 ) newCapacity = minCapacity; if (newCapacity - MAX_ARRAY_SIZE > 0) newCapacity = hugeCapacity(minCapacity); // minCapacity is usually close to size, so this is a win: elementData = Arrays.copyOf(elementData, newCapacity); }
Pros: Random access to elements is fast.
Disadvantage: The speed of inserting and removing elements in the middle is slow.
Purpose: List.
LinkedList
Implementation principle: implemented by a doubly linked list. Elements are ordered and repetitions are allowed.
// Among them, the method of finding a subscript element, you can see that it is found through the chain. Node<E> node( int index) { // assert isElementIndex(index); if (index < (size >> 1)) { // ">> 1" is equivalent to dividing by 2 Node<E> x = first; for ( int i = 0; i < index; i++ ) x = x.next; return x; } else { Node<E> x = last; for (int i = size - 1; i > index; i--) x = x.prev; return x; } }
Advantages: fast insertion and deletion in the middle.
Disadvantage: Random access is relatively slow.
Purpose: stack, queue, doubly linked list.
HashMap
Implementation principle: Implement a hash table. When storing elements (key/value), use the key to calculate the hash value. If the hash value does not collide, only the array is used to store the element; if the hash value collides, the elements of the same hash value will be used. Stored in a linked list; if there are more than 8 identical hash values, elements with the same hash value are stored in a red-black tree. When obtaining an element, use the key to calculate the hash value, use the hash value to calculate the subscript of the element in the array, and return if the element is hit; if not, find it in the red-black tree or linked list.
PS: Arrays storing elements are redundant.
// where put method. final V putVal( int hash, K key, V value, boolean onlyIfAbsent, boolean evict) { Node <K,V>[] tab; Node<K,V> p; int n, i; // To determine whether the table is empty, create it if it is empty. if ((tab = table) == null || (n = tab.length) == 0 ) n = (tab = resize()).length; // Calculate the index value and get the existing p element. if ((p = tab[i = (n - 1) & hash]) == null ) tab[i] = newNode(hash, key, value, null); else { Node <K,V> e; K k; // Determine whether the obtained p element is the one you want if (p.hash == hash && ((k = p.key) == key || (key != null && key.equals(k)))) e = p; // Determine whether the element is an element of the red-black tree else if (p instanceof TreeNode) e = ((TreeNode<K,V>)p).putTreeVal( this , tab, hash, key, value); // None, there are multiple elements with the same hash value, these elements are stored in a linked list else { for ( int binCount = 0; ; ++ binCount) { if ((e = p.next) == null ) { p.next = newNode(hash, key, value, null ); // If there are more than 8 linked list data, it will be converted to red-black tree storage if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st treeifyBin(tab, hash ); break ; } if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) break; p = e; } } //写入 if (e != null) { // existing mapping for key V oldValue = e.value; if (!onlyIfAbsent || oldValue == null) e.value = value; afterNodeAccess(e); return oldValue; } } ++modCount; if (++size > threshold) resize(); afterNodeInsertion(evict); return null; }
Features: store key-value pairs (keys allow null, keys cannot be repeated), asynchronous, not guaranteed order, and no guarantee that the order will not change over time.
HashSet
Implementation principle: use a HashMap instance to store data, which is essentially the set of HashMap.
// Add element method public boolean add(E e) { return map.put(e, PRESENT)== null ; }
Features: null elements are allowed, elements cannot be repeated, asynchronous, and ordering is not guaranteed.
TreeMap
Implementation principle: red-black tree.
Features: store key-value pairs (key does not allow null, key cannot be repeated), asynchronous, and keys are ordered.
TreeSet
Implementation principle: Use a TreeMap instance to store data, which is essentially the same set of TreeMap.
Features: null elements are not allowed, elements cannot be repeated, asynchronous, and guaranteed order.
thread safe collection
The collections mentioned above are all non-thread-safe collections.
Thread safe collections are:
Vector: Less efficient and not recommended.
Hashtable: The implementation principle is similar to HashMap, but there is no process of converting it into a red-black tree. Store key-value pairs (both key and value are not allowed to be null).
Stack: Stack class.