Java Collection Framework Usage considerations

Source: Internet
Author: User
useful implementation of list1.ArrayList 2.LinkedList 3.Vector 4.Stack Discussion 1: The underlying mechanism (the knowledge of the data structure involved, please review the reader)Both ArrayList and vectors are based on arrays, which means that ArrayList and vectors are suitable for traversal rather than for frequent insertions and deletions. LinkedList is based on a linked list, so it is born to insert and delete objects frequently. Discussion 2: Special featuresA stack is a LIFO (LIFO) object stack, and LinkedList can be used as a queue or a two-terminal queue, in addition to being used as a stack. The difference is that the stack inherits from the vector, which means it is also based on an array. Discussion 3: Memory footprintThe list, based on the array implementation, produces a new array when dynamically expanding, and then copies the contents of the old array into the new array, which results in a large number of object reference variables that are no longer being used to wait for the system to recycle. The list that is based on the linked list does not have this problem. Discussion 4: Synchronization issuesVector and Stack are born to sync, and ArrayList and LinkedList need to use Collections.synchronizedlist (List List) method to convert to Sync list. The iterators returned from their objects are fast failures, meaning that when iterating using iterators, you must use the Remove, add, and set methods of the iterator itself to add or change the list element, if, at the same time as the iteration, The list is structurally modified in other threads (structural modification means any action that adds or deletes one or more elements, or an explicit resizing of the underlying array; only the value of the element is not a structural modification), The fast fail iterator will do its best to throw concurrentmodificationexception. Discussion 5: Using PoliciesIf the data is extracted from the data source, the amount of data is not determined, the data is almost no longer added or deleted once it is extracted, then a linkedlist should be established to hold the data taken from the data source and then convert the LinkedList to ArrayList to optimize the traversal operation.   Conversely, the data determined by the data from the data source can be set up a ArrayList to save, according to the need for frequent additions and deletions, conversion to linkedlist, such as frequent traversal does not need to convert. The method of conversion is to use the corresponding list class to encapsulate the target list object.   such as ArrayList al = new ArrayList (); LinkedList ll = new LinkedList (AL); In turn, it can also linkedlist ll = new LinkedList (); ArrayList al = new ArrayList (ll); discuss the 6:toarray () methodThe list based on array implementation returns a copy of the underlying array directly (using the System.arraycopy method), Examda hints: The list implemented based on the list will be reborn into an array. Discussion 7: not modifiableBy using Collections.unmodifiablelist (list) to generate a list that cannot be modified, an attempt is made to modify the returned lists, whether directly modified or through their iterators. Will cause the unsupportedoperationexception to be thrown. Discussion 8: the Loop devicePlease try to use iterator,enumeration has not been encouraged to use. Finally, refer to the Java.util.Collections class, which provides a number of useful ways to manipulate collection objects.

the common implementation classes for the map interface are:1.HashMap 2.Hashtable 3.TreeMap 4.LinkedHashMap Discussion 1: underlying mechanismsHashMap and Hashtable based on array implementation, TREEMAP based on tree structure, the underlying storage structure is a typical linked list structure. Linkedhashmap inherits from HashMap, so it is also based on an array implementation. Discussion 2: Inheritance relationshipsHashMap and TreeMap inherit from the Abstractmap,hashtable inheritance from the Dictionary,linkedhashmap inheritance from HashMap. Discussion 3: Synchronizing relationshipsHashtable are synchronized, and HashMap are not synchronized with TreeMap and Linkedhashmap, and can be converted to synchronous using the methods provided in collections. Discussion 4: iteratorsIterators are fast failures (note: Refer to the first list in this series) Discussion 5: not modifiableConverted by using the Collections.unmodifiablemap (map map).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.