Java cheat sheet set specially prepared for Java beginners (1)
The features, implementation methods, and performance of all sets and concurrent sets are retained in the shortest length. It is suitable for those who are not so confident in reading "proficient in Java.
Keep updating. Visit the original blog as much as possible.
List
ArrayList
In array format. Space is saved, but the array capacity is limited. When the limit is exceeded, the System. arraycopy () is used to copy to the new array. Therefore, it is best to estimate the size of the array. By default, an array with a size of 10 is created when an element is inserted for the first time.
Accessing element-get (I)/set (I, e) by array subscript has high performance, which is the basic advantage of array.
Adding the element-add (e) directly at the end of the array has high performance. However, if you insert or delete the element-add (I, e), remove (I) by subscript ), remove (e), use System. when arraycopy () is used to move some affected elements, the performance degrades, which is a basic disadvantage.
Shortlist
It is implemented using a two-way linked list. The linked list has no capacity limit, but the two-way linked list itself uses more space and requires additional linked list pointer operations.
To access the element-get (I)/set (I, e) by subscript, move the pointer to the point of the tragic traversal chain table (if I> half of the array size, will be removed from the end ).
You can modify the pointer of the front and back nodes when inserting or deleting elements, but you still need to traverse some linked lists to move the pointer to the position indicated by the subscript. Only the operation-add () at both ends of the linked list can be performed (), addFirst (), removeLast (), or remove () on iterator () can save pointer movement.
CopyOnWriteArrayList
The ArrayList of concurrency optimization. With the CopyOnWrite policy, a snapshot is copied to modify the modification, and the internal pointer is directed to the new array after modification.
Because modification to the snapshot is invisible to read operations, only the write lock does not have a read lock, and the copy cost is high, this is a typical scenario that is suitable for reading more and writing less. If the update frequency is high or the array size is large, it is still Collections. synchronizedList (list). It is better to use the same lock for all operations to ensure thread security.
The addIfAbsent (e) method is added to traverse the array to check whether the element already exists. The performance is not good.
Supplement
Regardless of the implementation, returns the subscript-contains (e), indexOf (e), and remove (e) by value. All elements need to be traversed for comparison, and the performance is not as good as imagined.
There is no SortedList sorted by element values, and there is no concurrent1_list of lock-free algorithms in the thread security class. When we use the equivalence classes in the Set and Queue, some List-specific methods are missing.