Summary of Collection framework classes in Java

Source: Internet
Author: User

First look at these three interfaces: List, set, map

Implementation classes: arraylist, rule list, treeset, hashset, hashmap, treemap, and hashtable

 

For the inheritance relationship of Java Collection classes, see:
Java Collection framework

Differences between vector and arraylist
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.
However, we use the collections method to obtain a synchronized list.
As follows:
List list = new arraylist ();
List = collections. synchronizedlist (list );
Similarly, we can get a synchronized map:
Map hashmap = new hashmap ();
Hashmap = collections. synchronizedmap (hashmap );

Differences between arraylist and rule list
For processing a column of data items, Java provides two classes: arraylist and arraylist. The internal implementation of arraylist is based on the internal array object []. So Conceptually, it is more like an array, however, the internal implementation of The shortlist is based on a set of connected records. Therefore, it is more like a linked list structure. Therefore, they differ greatly in performance.
From the above analysis, we can see that when inserting data in front of or in the middle of the arraylist, you must move all the data after it. This will inevitably take a lot of time, when you add data to the end of a column of data rather than in the front or middle, and you need to randomly access the elements in the column, using arraylist will provide better performance.
When accessing an element in the linked list, you must start from the end of the linked list and search for the element one by one in the connection direction until you find the required element. Therefore, when you add or delete data in front or middle of a column of data and access the elements in the column in sequence, you should use the sort list.
If, in programming, the first and second situations alternate, you can consider using a general interface such as list, without worrying about the specific implementation. In specific circumstances, its performance is guaranteed by the specific implementation.
Configure the initial size of the Collection class
The size of most classes in the Java Collection framework can increase accordingly as the number of elements increases. We don't seem to care about its initial size, however, if we consider the performance of the class, we must set the initial size of the Set object as much as possible, which will greatly improve the Code performance.
For example, the default initial size of hashtable is 101, and the load factor is 0.75. If the number of elements exceeds 75, it must increase the size and reorganize the elements, if you know that the exact number of elements is 110 when creating a new hashtable object, you should set its initial size to 110/0. 75 = 148. In this way, you can avoid reorganizing the memory and increasing the size.

List summary:
Arraylist is essentially an array and is suitable for adding data at the end and for random access.
Similar to a linked list, tranquility list is suitable for modifying, adding, and deleting elements at any time and in sequence. It is suitable for storing a set of data that is frequently inserted, deleted, and ordered.

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.

Differences between hashmap, linkedhashmap, and treemap
MAP is mainly used to store key-value pairs. Therefore, duplicate keys are not allowed, but repeated values are allowed.

Hashmap is the most commonly used map. It stores data based on the hashcode value of the key, and can directly obtain its value based on the key, with fast access speed. Synchronization of threads, that is, multiple threads can write hashmap at the same time at any time, which may cause data inconsistency. If synchronization is required, you can use the synchronizedmap method of collections to synchronize hashmap. Hashmap allows a maximum of null keys for one record.

Linkedhashmap is also a hashmap, But it maintains a two-way linked list internally, which can maintain the order and is a subclass of hashmap.
Treemap not only maintains the order, but also can be used for sorting. Because it is used for sorting, hashmap requires the added key class to clearly define the implementation of hashcode () and equals.

Map summary:
Hashmap is the best choice for inserting, deleting, and locating elements in a map. However, if you want to traverse keys in the natural or custom order, it is better to use treemap. The key classes required to be added using hashmap clearly define the implementation of hashcode () and equals. Treemap has no optimization option because the tree is always in the balance state.

Set is implemented based on map (hashmap), so the difference is easier to understand than map.

Objects in the set are not arranged in a specific way and there are no repeated objects. Some of its implementation classes can arrange objects in the Set in a specific way.
The Set interface mainly has two implementation classes: hashset and treeset. The hashset class uses the hash algorithm to access objects in the set. The access speed is relatively fast. The hashset class also has a subclass of the javashashset class, not only implements the hash algorithm, but also implements the Linked List data structure. The treeset class implements the sortedset interface and has the sorting function.

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

Note that the treemap (sort keys) and treeset sorting functions call the compareto () method of the object to compare the object size in the collection, and then sort the objects in ascending order. This method is called natural sorting.
The following is the result of natural sorting:
Treemap: {1 = A, 2 = B, 3 = C, A = aaa, B = BBB, A = aaa, B = BBB}
Treeset: [1, 2, B, F, A, B, D, F]
If you want to sort Chinese characters, you need to sort them by customization:
The following code sorts Chinese characters (note: the parameter is collections. Sort ):
List list = new arraylist (treeset );
List = collections. synchronizedlist (list );
Collections. Sort (list, collator. getinstance (Java. util. locale. China ));
Result After sorting:
[1, 2, A, B, B, D, F, F, A, Bi, talent, hair, you, eh]

 

 

 

 

 

Related Article

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.