Collection Framework Architecture in Java

Source: Internet
Author: User
Tags comparable set set

The architecture of the collection:    |--collection (the root interface of a single column collection)         |--list (sub-Interface): elements are ordered and elements can be duplicated. Because the collection system has an index.             |--arraylist: The underlying data structure uses the array structure. Features: Fast query speed. But additions and deletions are slightly slower. Thread is out of sync.                 |--because the underlying uses an array of data structures, and the elements in the array are continuously allocated in the heap memory, and indexed, so queries are fast, and additions and deletions are slightly slower                 |--when iterating through elements using iterators, you can no longer use the collection's methods to manipulate the elements in the collection           &NBSP ;     |--calls the Equals method when the contains () or remove () method of the collection is called, and if the object that is stored in the collection does not implement Equals (), then the Equals () method of the object is called             |--linkedlist: Linked list data structures used at the bottom. Features: Deletion speed quickly, query slightly slow. Thread is out of sync.             |--vector: The bottom layer is the array data structure. Thread synchronization. Replaced by the ArrayList. Because the efficiency is low.         |--set (sub-Interface): Elements are unordered (the order in which they are deposited and fetched is not necessarily consistent), and elements cannot be duplicated.             |--hashset: The underlying data structure is a hash table. is not thread-safe. Different steps.                 |--hashset How do you ensure element uniqueness?                    |--is done through the elements of the two methods, hashcode and equals. |--If the hashcode value of the element is the same, the equals is determined to be true. |--If the hashcode value of the element is different, equals is not called. |--Note that for actions such as determining whether an element exists, as well as deletions, the dependent method is the hashcode of the element and the Equals method. |--linkedhashset|--treeset: You can sort the elements in the set collection. |--The underlying data structure is a two-fork tree.the basis for guaranteeing the uniqueness of the element: CompareTo method return 0.                    |--treeset The first way to sort: make the elements themselves comparable. The element needs to implement the comparable interface, overriding the CompareTo method.                         This approach also becomes the natural order of the elements, or is called the default order.                     |--treeset The second way to sort. When the elements themselves are not comparable, or have the comparative is not required.                         This will require a comparison of the set itself. When the collection is initialized, there is a comparison method.                         Create a comparator (implementing the comparator interface, overriding the Compare method), When you build a TreeSet collection, you pass the comparer to the collection .    |--map collection: The collection stores key-value pairs. A pair of pairs went into the deposit. and to guarantee the uniqueness of the key.         |--hashtable: The underlying is a hash table data structure that cannot be stored as a null key null value. The collection is thread-synchronized. jdk1.0. Low efficiency.         |--HASHMAP: The underlying is a hash table data structure that allows NULL values and NULL keys, which are not synchronized. Replace Hashtable, jdk1.2. High efficiency.         |--TREEMAP: The bottom layer is a two-fork tree data structure. Different steps. Can be used to sort keys in the map collection.         |--and set are very similar. In fact, the set bottom is using the Map collection   &NBSP;         |--MAP collection two ways to remove:                |--set<k> KeySet : Deposits all the keys in the map into the set collection. Because set has iterators.                     All of the keys can be iterated out, in accordance with the Get method. Gets the value corresponding to each key.                     Map collection principle: Turns the Map collection into a set set. is removed by an iterator.                 |--set<map.entry<k,v>> EntrySet: The mapping relationship in the map collection is stored in the set set, and the data type of the relationship is:map.entry                  and nbsp Entry is actually a static internal interface in the map.                     Why should it be defined internally?                     because only the map collection, with a key-value pair, will have a key-value mapping relationship.                     relationships belong to an internal thing in the map collection. And the object directly accesses the elements in the map collection

Collection Framework Architecture in Java

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.