Java Collection Architecture

Source: Internet
Author: User
Tags array length set set

Collection interface:

  1. The root interface of a single-column collection class.

2. Define the method that can be used to manipulate list and set--adding and deleting;

3. Inherit from the Iterable<e> interface, which provides a iterator() method: Returns an iterator that iterates over an element of a set of type T, giving it the privilege of iterating with the foreach statement.

  List interface:

    1. Elements can be duplicated.

2. Elements are ordered: The elements are stored in the same order as the order of extraction.

3. All elements are stored in a linear fashion, where the specified elements in the collection can be accessed through an index.

4. There can be null elements in all lists.

    ArrayList collection:

The 1.ArrayList internally encapsulates a variable-length array object, and when the element is deposited beyond the array length, ArrayList allocates a larger array in memory to store the elements (twice times the number growth), so ArrayList can be thought of as a variable-length array.

The bottom of the 2.ArrayList collection is the use of an array to hold the elements, which, when added or removed, causes a new array to be created, inefficient and not suitable for a large number of additions and deletions.

The array structure of the 3.ArrayList allows the element to be accessed by means of an index, so it is convenient to use the ArrayList collection to find elements.

4.ArrayList is thread insecure, and when running into a multithreaded environment, you need to manage thread synchronization issues yourself.

    Vector collection:

    1.Vector, like ArrayList, is implemented by arrays, and when the deposited elements exceed the length of the array, a larger array is allocated in memory to store the elements (1.5 times times the number growth).

2.Vector supports thread synchronization, which means that only one thread at a time can write vectors and avoid inconsistencies caused by simultaneous writing of multiple threads, but achieving synchronization requires a high cost, so accessing it is slower than accessing ArrayList.

    LinkedList Collection:

The 1.LinkedList internally maintains a two-way loop linked list in which each element of the list uses references to remember the previous and next elements, thus connecting all elements together.

2. when adding to the LinkedList, delete the action without having to do a lot of data movement like the array-based list. As long as you change the relevant information of the reference can be implemented. This is the advantage of LinkedList.

  Set interface:

    1. A collection that does not contain duplicate elements, with a maximum of one null element in the set.

2. Because the set set is unordered, the traversal can only be implemented with iterator.

3.Set all keys that depend on Map,map are grouped together as set.

    HashSet:

      1.HashSet relies on HashMap, which is actually implemented through HashMap.

2. The elements are unordered and do not guarantee the iteration order of the collection.

3. Thread insecure, fast access, need to use the following statement for the S synchronous transformation:
Set s = collections.synchronizedset (new HashSet (...));

4. the Hashcode method of the dependent element and the Equals method guarantee element uniqueness.

    TreeSet:

      1.TreeSet relies on TreeMap, which is actually implemented through TREEMAP.

2. The set collection can be sorted by CompareTo or compare method.

3. Thread is not secure.

4. Internally using a balanced sort binary tree to store elements

MAP:

  1. The "key value" of the abstract interface to the map. The map does not include duplicate keys, and a key corresponds to a value.

The data structure of the 2.Map collection is only valid for the key, regardless of the value.

  HASHMAP:

    1. The underlying data structure is a hash table. The underlying is stored in an array-linked list, and the element is entry.

2. The elements are unordered and there is no obvious order to save the data.

3. Provides the fastest search technology.

4. Thread is not secure.

5. Allow null value and NULL key to allow a maximum of one record's key to be null (many will overwrite), and allow multiple records to have a value of NULL.

  Hashtable:

    1. The underlying data structure is a hash table. The underlying is stored in an array-linked list, and the element is entry.

2. The elements are unordered.

    3. Efficiency is relatively slow.

4. Thread safety.

The value of 5.key and value are not allowed to be null.

  TREEMAP:

    1. The underlying data structure is a red-black tree (a self-balancing two-fork tree).

2. The saved records are sorted by key (key), by default, by ascending order, or by specifying a sort comparer, and when traversing treemap with iterator, the resulting records are sorted out.

3.TreeMap the value of the key is not allowed to be null.

4. Thread is not secure.

Java Collection Architecture

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.