Useful Implementation of list
1. arraylist 2. arrays list 3. Vector 4. Stack
Discussion 1: Underlying Mechanism (readers should review the data structure knowledge)Both arraylist and vector are implemented based on arrays. This shows that arraylist and vector are suitable for traversal rather than frequent insertion and deletion. The shortlist is implemented based on the linked list, so it is born to insert and delete objects frequently.
Discussion 2: Special FeaturesA stack is a post-in-first-out (LIFO) object stack. In addition to being used as a stack, a consumer list can also be used as a queue or a dual-end queue. The difference is that stack inherits from vector, that is, it is also implemented based on arrays.
Discussion 3: memory usageThe list implemented based on arrays will generate a new array during dynamic expansion, and then copy the content in the old array to the new array, this will generate a large number of Object Reference variables that are no longer used and wait for the system to recycle them. The list implemented based on the linked list does not have this problem.
Discussion 4: SynchronizationVector and stack are inherently synchronous, while arraylist and worker list must use collections. synchronizedlist (list) method to convert to synchronous list. the iterator returned from their objects quickly fails, that is, when the iterator is used for iteration, you must use the remove, add, and set methods of the iterator to add or change the list element, in other threads, the list structure is modified (Structure Modification refers to any operation to add or delete one or more elements, or to explicitly adjust the size of the underlying array; only setting the element value is not modifying the structure), the quick failure iterator will do its best to throw concurrentmodificationexception.
Discussion 5: Usage PolicyIf the data is extracted from the data source, and the data volume is not certain, the data will not be added or deleted after it is extracted, a sequence list should be created to save the data retrieved from the data source, then convert the traversal list to arraylist to optimize the traversal operation. In turn, you can create an arraylist to store the data that is determined by the data volume from the data source. If you need to add or delete the data frequently as needed, the data is converted to the sorted list. If you need to traverse the data frequently, you do not need to convert the data. The conversion method is to use the corresponding list class to encapsulate the target list object. For example, arraylist Al = new arraylist (); sorted list LL = new sorted list (Al); similarly, you can also sort list LL = new sorted list (); arraylist Al = new arraylist (LL );
Discussion 6: toarray () methodList based on Array Implementation directly returns a copy of the underlying array (using the system. arraycopy method). examda prompts that the list based on the linked list will generate an array.
Discussion 7: unchangeableBy using collections. unmodifiablelist (list List) to generate an unchangeable list, an unsupportedoperationexception will be thrown if you try to modify the returned list directly or through its iterator.
Discussion 8: TraversalPlease try to use iterator. enumeration is no longer encouraged. Finally, refer to the java. util. Collections class, which provides many useful methods to manipulate collection objects.
Common map interface implementation classes include:
1. hashmap 2. hashtable 3. treemap 4. linkedhashmap
Discussion 1: Underlying MechanismHashmap and hashtable are implemented based on arrays. treemap is based on a tree structure, and the underlying storage structure is a typical linked list structure. Linkedhashmap inherits from hashmap and is implemented based on arrays.
Discussion 2: inheritance relationshipHashmap and treemap inherit from abstractmap, hashtable inherit from dictionary, and linkedhashmap inherit from hashmap.
Discussion 3: synchronization relationshipHashtable is synchronous, while hashmap and treemap and linkedhashmap are not synchronous. You can use the methods provided in collections to convert them to synchronous.
Discussion 4: iteratorAll iterators fail quickly (NOTE: Refer to the first list article in this series)
Discussion 5: unchangeableUse collections. unmodifiablemap (MAP map) for conversion.