Explanation of the collection framework in Java

Source: Internet
Author: User

here's how to start learning about the relevant collection framework in Java.

Is it ready? Let ' s go~~

The Collection framework in Java to be explained today.

1) First look at the source code of the collection class in the JDK will find the following:

...
* @see abstractcollection * @since 1.2 */public interface collection<e> extends iterable<e> { //Quer Y Operations

By looking at it, you can see that collection is an interface class that inherits Java iterative interface iterable.

It is well known that when we use the storage of classes in Java, we often use some container, linked list concept, this article will help you to understand the list of various concepts and MODELS!!!!

  Attention to Understanding OH ~ ~ ~ The framework is as follows:                                                                                 is stored in the form of a---->ArrayList  similar array, so its random access speed is extremely fast                                                                                |                                                                               |                         ----->list--------------------------->                          |       (to achieve the corresponding sorting                   |                          |       can be repeated)                                 |                          |                                                     ----->LinkedList   is suitable for frequent insertions and deletions in the middle of a linked list.                          |                          |                          |                          | Collection-------(unordered,  between elements |    can repeat the individual elements of the group, that is, each of its positions holds only one element)                |                          |                          |                          |                                                ---->HashSet  can be quickly positioned, but objects placed in HashSet need to be implemented Hashcode () Method                          |                                                 |                          |                                                 |                         ----->set------------------------->                                (realized not repeating, kinda like set of |                                concept , unordered)                          |                                                                            ---->TreeSet  to achieve the appropriate sorting

Map: The Key-value object that is paired with the collection group, that is, key-value pairs. There can be no duplicate key in the map, it has its own internal arrangement mechanism. However, it is possible to implement multiple key values corresponding to a numeric value.

Iterator:iterator, also known as the overlap, can sequentially traverse the collection.

Vector:vector (vector) is a concrete implementation of the list interface, which supports sequential access of the listing elements, and the size can be increased with the addition of elements and is thread-safe. The performance of the access is poor, as is the requirement for thread safety. Since Java1.2, it has not been recommended. JAVA1.2 recommends the use of ArrayList and LinkedList. They are not thread-safe, so performance is good. where ArrayList random access speed, and LinkedList in the middle of the table inserted faster.

For the above two types, it is the container of the collection class, which realizes its concrete traversal.

Collection,set,list The difference: There is no specified order between the Collection elements, allowing duplicate elements and multiple null element objects.       There is no specified order between the SET elements, no duplicate elements are allowed, and at most one null element object is allowed. The list elements have a specified order, allowing for duplicate elements and multiple null element objects.

The list interface is a simple extension of collection, and its concrete implementation classes are often ArrayList and LinkedList. You can put anything in a list container and take it out when you need it. ArrayList can be seen in its name as an array-like form of storage, so its random access speed is very fast, and LinkedList's internal implementation is a linked list, it is suitable for in the middle of the list of links need to frequently insert and delete operations. The application can be freely selected as needed. The iterator can only walk forward through the container, while Listiterator inherits the iterator idea and provides a way to iterate through the list.

The set interface is also an extension of collection, and unlike list, the object element in set cannot be duplicated, meaning you cannot put the same thing two times into the same set container. Its commonly used concrete implementation has hashset and TreeSet class. HashSet can quickly locate an element, but the object you put into HashSet needs to implement the Hashcode () method, which uses the algorithm of the previously mentioned hash code. And TreeSet will put the elements in order to store, which requires you to put the object is sortable, which is used in the collection framework provided by the other two practical classes comparable and comparator. A class is sortable, and it should implement the comparable interface. Sometimes more than one class has the same sorting algorithm, it is not necessary to define the same sorting algorithm at each separate time, as long as the comparator interface is implemented. There are two useful common classes in the collection framework: collections and Arrays. Collections provides some very useful methods for a collection container such as sorting, copying, finding, and populating, and arrays is doing a similar operation on an array.

A map is a container that associates a key object with a value object, and a value object can be a map, and so on, so that a multilevel map can be formed. For a key object, like set, a key object in a map container does not allow repetition, which is to keep the consistency of the search results, and if there are two key objects, then you have a problem with the value object that the key object has, and you may not get the value object you want, and the result will be confusing. So the uniqueness of the key is very important, and it conforms to the nature of the set. Of course, in the process of use, the value object corresponding to a key may change, then the last modified value object corresponds to the key. There is no unique requirement for value objects. You can map any number of keys to a value object without any problems (though it may be inconvenient for you to use it, you don't know what you get is the value object for that key). There are two more common implementations of map: HashMap and TreeMap. HashMap also uses the hash code algorithm, in order to quickly find a key, TreeMap is the key in order to store, so it has some extension methods, such as Firstkey (), Lastkey () and so on, you can also specify a range from the TreeMap to get its child map. The association of Keys and values is simple, with the pub (Object Key,object value) method to associate a key with a value object. Use Get (object key) to get the value object corresponding to this key object.

1. Classification of Containers 1.1. Collection: A set of independent elements, that is, each of its positions holds only one element. 1) List: elements are placed in the order of the elements and are not rearranged. 2) Set: Do not receive the repeating element of love, it will use one of its own internal arrangement mechanism 1.2. Map: A group of pairs of Key-value objects, that is held by some key-value pairs. There can be no duplicate key in the map, it has its own internal arrangement mechanism. 2. The element type in the container is object. When an element is obtained from a container, it must be converted to its original type. list, map, set list access and value by subscript to access the map key value corresponding to access the set access element is unordered

Explanation of the collection framework in Java

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.