JDK source code analysis collection 01 overall framework, jdk source code 01 overall

Source: Internet
Author: User

JDK source code analysis collection 01 overall framework, jdk source code 01 overall

A collection class is a tool class provided by java, mainly including List, Set, Map, and vector. All collection-related classes are located in java. util.

The overall framework of the Collection class is shown in:

Description of the overall framework:

The top-level interfaces in the Collection framework are Iterator, Collection, and Map. Among them, Itireator is the interface to be implemented by all iterators, Collection is the interface to be implemented by all linear structure sets, and Map is the interface to be implemented by all non-linear sets, elements are saved as keys and values.

The lower-level Collection contains three self-Interfaces: Queue, List, and set. These three interfaces have their own characteristics. The data structure corresponding to the Queue is the Queue. It features two basic operations: adding elements at the end of the Queue and deleting elements in the Queue's header, with the features of first-in-first-out; the elements in the List are also stored in a linear structure and can have repeated elements. Set has the same interface as Collection. Set is characteristic that repeated elements cannot be stored, elements in the Set must overwrite the equals method to ensure the uniqueness of the elements. The structure at the next layer defines three abstract classes: AbstractCollection, AbstractList, AbstractSet, and AbstractSequentialList. Some methods are implemented in the abstract class. This design uses the adapter design mode, the advantage of doing so is that sub-classes can selectively implement some methods. The bottom layer is the specific Collection class in the Collection class system. There are six Collection classes: ArrayList, sorted list, Vector, Stack, HashSet, and TreeSet. Here, ArrayList uses arrays to store objects. The efficiency of accessing elements through indexes is relatively high, but the insertion and deletion efficiency is low. It uses a linked list to store elements, when using indexes to access elements, the validity rate is relatively low, but the insert and delete operations are highly efficient. The Vector uses arrays to save elements, but the Vector supports thread synchronization, that is to say, only the same thread can operate on the Vector at the same time. The Stack is the data structure of the Stack, and only the insert and pop-up operations at the top are supported. Neither HashSet nor TreeSet can store duplicate elements, however, TreeSet can store elements in ascending order, while HashSet stores elements in a hash table.

The lower layer of Map also defines a AbstractMap to implement some methods in the Map interface, so that subclass can selectively implement the methods in Map. The implementation classes in Map include TreeMap, HashMap, WeakHashMap, and HashTable. TreeMap stores elements in an ordered manner and sorts keys in the Map. HashMap is a hash table data structure with relatively high efficiency. HashTable is a subclass of Dirctionary and can map any Key to the corresponding value, therefore, in HashTable, the existence of null key or value is not allowed, while in HashMap, a null key and any null value are allowed.

The Iterator interface has the ListIterator interface, which must be implemented by all iterators. You can use an iterator to traverse collection elements.

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.