Comparison of array collection list set Map

Source: Internet
Author: User

Array
There are three main differences between arrays and other containers: efficiency, type, and ability to save basic types. in Java, arrays are a highly efficient method for storing and randomly accessing object reference sequences. array is a simple linear sequence, so the access speed is fast, but other features are also lost. after an array object is created, the size is fixed. If the space is insufficient, an array is usually created and all references in the old array are moved to the new array. the array can save the basic type, but the container does not.
The container class does not process objects with specific types, but processes all objects with the object type. Therefore, we can create only one container, and any Java object can be put in. the container class can use the packaging class (integer, double, etc.) to put the basic type into it. the list set map can automatically adjust the capacity, and the array cannot.

Collection represents a group of objects, also known as collection elements. Some collections allow repeated elements, while others do not. Some collections are ordered, while others are disordered. JDK does not provide any direct implementation of this interface. It provides more specific sub-interfaces (such as set and list.

Map maps the key to the value object. A ing cannot contain duplicate keys. Each key can only map one value at most. the map interface provides three collection views, allowing you to view the content of a ing in the form of a key set, value set, or key-value ing relationship set. Some ing implementations can clearly ensure their order, such as the treemap (ordered) Class; some ing implementations do not guarantee the order, such as the hashmap (unordered) class. Map can be expanded to multi-dimensional arrays as an array, as long as each value is also made into a map.

Collection and map are two basic types in Java containers. the difference is that the number of elements saved at each location in the container. each position of the collection can only save one element, including list and set. the list stores a group of elements in the order they enter. The elements in the set cannot be repeated. arraylist is a list and hashset is a set. The add () method can be used to add elements to any collection. map stores the key-value pair. use put () to add elements to map. It requires a health and a value as parameters.

Both arraylist and arraylist implement the list interface. The underlying layer of arraylist is supported by arrays. The list is supported by a two-way linked list. Therefore, it is more appropriate to insert or delete the element 'arraylist' in a table, it is more appropriate to query arraylist frequently.
The implementation of the set includes treeset, hashset, linkedhashset, and hashset. The fastest query speed is achieved. The linkedhashset maintains the element insertion order. The treeset generates a set that is always sorted Based on the treemap.

 

Collection <-- list <-- Vector
Collection <-- list <-- arraylist
Collection <-- list <-- items list
Collection <-- set <-- hashset
Collection <-- set <-- hashset <-- revoke hashset
Collection <-- set <-- sortedset <-- treeset

Vector: array-based list encapsulates some functions not available in array for our convenience, and it cannot be restricted by array. The performance cannot surpass array. Therefore, when possible, we need to use array more. Another important point is the vector "sychronized", which is the only difference between vector and arraylist.

Arraylist: Like a vector, it is an array-based linked list, but the difference is that arraylist is not synchronized. Therefore, the performance is better than that of vector, but when running in a multi-threaded environment, you need to manage the synchronization of threads on your own.

Sort list: the sort list is different from the previous two lists. It is not based on array, so it is not limited by Array Performance. Each node contains two aspects: 1. Data of the node itself; 2. Information of the next node (nextnode ). Therefore, when you add a consumer list and delete it, you do not need to move a large amount of data like an array-based list. You only need to change the relevant information of nextnode. This is the advantage of listing.

List summary:

1. All lists can only contain tables composed of a single object of different types, rather than key-value pairs. Example: [Tom, 1, C];

2. All lists can have the same elements. For example, the vector can have [Tom, koo, too, koo];

3. All lists can have null elements, such as [Tom, null, 1];

4. array-based list (vector, arraylist) is suitable for queries, while sorted list (linked list) is suitable for adding and deleting operations.

Hashset: Although set and list all implement the collection interface, their implementation methods are quite different. List is basically based on array. However, set is implemented based on hashmap, which is the fundamental difference between set and list. The hashset storage method uses the key in hashmap as the corresponding storage item of the set. Let's take a look at the implementation of the add (Object OBJ) method in hashset.

Public Boolean add (Object OBJ)
{
Return map. Put (OBJ, present) = NULL;
}

This is also the root reason why repeated items cannot be found in the set as in the list, because the key of hashmap cannot be duplicated.

Linkedhashset: a subclass of hashset, a linked list.

Treeset: a subclass of sortedset. Unlike hashset, treeset is ordered. It is implemented through sortedmap.

Set summary:

1. Set is implemented based on map (hashmap );

2. Elements in the set cannot be repeated. If you use the add (Object OBJ) method to add an existing object, the previous object will be overwritten;

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.