The Java Collection Class package is located under Java.util and has many common data structures: arrays, linked lists, queues, stacks, hash tables, and so on. Understanding the characteristics of different collection classes is more important in the development process, thanks to the column Analysis @ Orchid Pavilion, here I also based on their own understanding to do a mild summary, no longer analysis, because the above blog analysis has been quite excellent.
List interface Implementation , generally used for ArrayList and LinkedList, there are less commonly used vector,stack. such as the JDK document declaration
DequeInterfaces and their implementations provide a more complete and consistent set of LIFO stack operations, and this set should be preferred rather than the Stack class. For example:
Deque<Integer> stack = new ArrayDeque<Integer>();
对ArrayList和Vector进行一个比较:
- ArrayList non-thread-safe, vectors are thread-safe, and the ArrayList class that wants to use thread-safe can collections.synchronizedlist () wrap objects back to thread-safe, or use Copyonwritearraylist
- Because ArrayList and vectors are based on an array, the default size is 10, so there is a need to expand the operation, ArrayList expansion is 1.5 times times +1 or the required size, vector is twice times
- Arrays.copyof () and system.arraycopy () are used extensively for replication, native methods, and the advantages and disadvantages of having array operations
ArrayList and LinkedList:
- LinkedList is based on a two-way linked list, and when accessed by index there is an accelerated operation, non-thread-safe
- LinkedList also implements stack and queue operations, so it can also be used as a stack, queue, and double-ended queue
- LinkedList constructing a null-linked list that contains only the head node in a non-parametric construction method
- Two all allow null values
Map Interface Implementation class HashMap and Hashtable:
Hash table to solve the hash value of the common methods of conflict is two, one is the zipper method that is, each hash value corresponds to a linked list, the other is a linear detection method.
- The way HashMap and hashtable handle conflicts is the Zipper method
- HashMap non-thread-safe, hashtable thread-safe
- The initial capacity of HashMap is 16, the loading factor is 0.75, and after the expansion, it must be a power of 2 of the whole number; Hashtable is 12 and does not have a power of 2 for the entire number of times
- When the Hashtable expands, the capacity becomes twice times plus 1, while HashMap expands, the capacity becomes twice times the original.
- HashMap can be null,hashtable cannot be null
- Hashtable calculates the hash value, directly uses the key's Hashcode (), and HashMap recalculates the key hash value Hashtable calculates the hash value, directly uses the key hashcode (), and HashMap re-calculated the hash value of key, HashMap through h& (length-1) method to replace the modulo, the same uniform hash, but the efficiency is much higher, this is hashmap to hashtable an improvement.
TREEMAP:
- Based on the 2-3 balance tree of red and black trees to achieve, the bottom of the red and black trees will be a simple introduction
- The TREEMAP key cannot be null, and when sorting to implement the comparable interface, HashMap requires overriding the Hashcode method and the Equals method to ensure that there is no duplicate key
- TreeMap query, INSERT, delete efficiency are not hashmap high, generally only to sort key to use TreeMap
Red and Black Tree introduction:
The red-black tree is a special 2-3-balance tree that guarantees the equal distance of all black edges to the root node. Red black tree is an orderly two-fork tree, through the left rotation, right rotation, discoloration and other operations, so that the tree basically maintain a balanced state, to prevent the occurrence of one-sided, it has the following properties:
- Each node can only be red or black
- The root node is black
- Each leaf node (nil node, note nil node is an empty node, in the implementation of the Red black tree, the nil node instead of the binary tree null) is black.
- If a node is red, its two child nodes are black. This means that no adjacent two red nodes can appear on a path.
- All paths from any node to each of its leaves contain the same number of black nodes.
A small summary of the Java collection classes