A brief analysis of Java Collection (collection)

Source: Internet
Author: User

Class set: Called a container (container) in C + + and called a class set in Java

Collection (class Set): The most basic collection interface, a Collection represents a set of object collections. Collection (class Set) Special shape:1, elements (Elements) storage:
    1. can store the same elements;
    2. Can not store the same elements;
2. Element (Elements) Order:
    1. Orderly
    2. Disorder
Collection (class set) Advantages:
    1. The method of processing object group is standardized;
    2. High performance; The implementation of basic class sets (dynamic arrays, linked tables, trees, and hash lists) is efficient. In general, it is seldom necessary to manually write code for these "Data engines".
    3. Allows different types of class sets to work in the same way and in a highly interoperable manner;
    4. Easy to expand and/or modify;
    5. has a mechanism to fuse a standard array into a class set framework;
    6. The algorithm is simple to operate, and the class set algorithm is defined as a static method in the collections class (for example: Max, Min, reverse ...). ), so they can be exploited by all class sets.
    7. Class set Element (Elements) access is simple. Iterator provides a versatile, standardized approach that can be accessed by the elements of any class set, by means of a iterator-defined method.
Collection Framework: A unified schema for representing and manipulating collections, containing the interfaces and classes that implement the collection.
    • Collection: The root interface in the collection hierarchy, the JDK does not provide a direct implementation class for this interface.
    • List: is an ordered set of elements that can contain duplicates . Provides a way to access by index.
ArrayList: A common understanding of an array that can automatically grow capacity. LinkedList: Can be through the valley of the understanding of the entry object to store the two-way circular linked list. 
    • Set: cannot contain duplicate elements . SortedSet is a set that arranges elements in ascending order.
HashSet: Can be generally understood as random to the collection of objects, the set of objects are unordered; Linkedhashset: can be easily understood as random to the collection of objects, the objects in the collection are sorted in order of the sequence of the list; Tree Set: Can be popularly understood as random to the collection of objects, the collection of objects are based on the specified comparator to sort;
    • Map: Contains the key-value pair. The map cannot contain duplicate keys. SortedMap is a map that sorts keys in ascending order.
HashMap: A set of objects (value) that can be commonly understood as a different tag (key); TreeMap: It can be popularly understood as a collection of objects (value) that hold different tokens (keys) and sorted by the specified comparer by tag (key). Note: 1. HashSet is implemented through HashMap, TreeSet is achieved through TREEMAP, except that set uses only the map key.
2. A common feature of the key and set of map is the uniqueness of the collection. TreeMap is more a sort of feature.
3. Hashcode and equal () are used by HashMap because there is no need to sort, so just focus on positioning and uniqueness.
A. hashcode is used to calculate the hash value, and the hash value is used to determine the hash table index.
B. One index in the hash table is a linked list, so it is also possible to loop through the equal method to compare each object on the chain to really locate the entry of the key value.
C. When put, if the hash table is not located in the list, add a entry before the list, if it is located, replace the value in entry and return the old value
4. Because the treemap needs to be sorted, a comparator is required for the size comparison of the key values. Of course, it is also used to locate comparator.
A. Comparator can be specified when creating TreeMap
B. If the creation is not determined, then the Key.compareto () method is used, which requires key to implement the comparable interface.
C. TreeMap is implemented using the tree data structure, so you can use the Compare interface to complete the positioning.

A brief analysis of Java Collection (collections)

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.