Data Structures in Java (Collection | container)

Source: Internet
Author: User
Tags comparable set set

A small personal summary of the data structure in Java, although not yet to study the extent of the JDK source code. First, why do you need a collection in Java? What demand is causing. I think for object-oriented, the object is suitable for describing anything, so in order to facilitate the manipulation of objects, storage has appeared a collection, the collection should be the most common way to store objects, relative to the array more flexible, variable length. At the same time, collections can only store objects that do not have basic data types.

The collection framework is divided into two factions collection and map and each collection type is based on a specific data structure, and the collection interface inherits the Iterable interface so you can use the iterator of the Iterable interface () Method The return value is a iterator interface type This is the iterator in the collection collection, which is the removal of a collection element to directly access the elements in the collection. It is defined inside the set to make it easier to take the elements in the collection, it can remove and delete the elements in the collection (next (), remove ()) and do not use the method of the collection itself to manipulate the collection when using iterators to manipulate the data. Because iterators have been defined for subsequent operations of the collection, the iterator operation is not the previously defined collection, which is a conflict. Its subinterface, listiterator, can be used to make additional deletions to the elements in the collection (which is actually the object reference stored in the collection of operations, not the actual object itself, because the collection stores the object's reference address).

The

Collection interface is also divided into list and set interface, the corresponding list interface is commonly used is the ArrayList and LinkedList two implementation classes. The underlying data structure of the ArrayList class is an array interface, which is ordered with a corner index and can be duplicated, and the LinkedList class is a linked list data structure, They determine whether the element is the same as the Equals method is used relative to ArrayList it is faster for adding or removing data to the collection data because only the end-to-end relationship adjustment is required, and for ArrayList, the index needs to be re-adjusted. So for the large data volume of the addition or deletion operation is linkedlist more efficient and the query operation ArrayList faster they are out of sync (ArrayList replaced the vector). The set interface is commonly used HashSet and treeset two classes, HashSet is a hash table interface It is unordered, non-repeatable, thread out of sync, And judging whether the element is the same, is whether the Hashcode method returns 0 if it is 0 also determines whether the Equals method returns True. TreeSet bottom is a binary tree structure that is an orderly and non-repeatable collection of threads that ensure data uniqueness by whether the CompareTo method returns 0,treeset sorted collections in two ways: 1, Making the stored elements inherently comparable is implementing the comparable interface overlay CompareTo method. 2, the custom comparator implements the comparator interface custom CompareTo method and then takes the comparator as the parameter to enter the TreeSet construction method is the TreeSet initialization to have the comparison.

The map interface is the interface with the collection interface, the map interface stores a key-value form of the mapping relationship, so for the data that need to store the mapping relationship preferred Map,map common types have hashmap and TreeMap, HashMap the underlying data structure is also a hash table can be used to store the null is also the thread is not synchronized, this also replaces the Hashtable thread out of sync, cannot store null values, TreeMap is also a binary tree data structure, The same map's way of fetching data is to use the EntrySet () method to return a set set and then use an iterator to take out or to enhance a For loop (a collection that implements the Iterable interface can be looped using foreach). The mapping relationship returned by EntrySet is a map.entry type because entry is actually defined in the map, why do you do it? Because only the map collection has a key-value pair that has a mapping of key-value pairs, the relationship belongs to the inner object in the collection, which has direct access to the elements in the collection. In practical applications I think more of the nesting of maps.

The tool class (collections, Arrays) Collections Collection Tool class is an operation combined with a specialized tool class which is defined by a static method, A variety of sorting binary lookups for the list collection (analogy string operations) can also turn threads unsafe into thread-safe, and the same arrays is defined in a tool class that operates an array of operations that are also static methods for arrays (ToString prints array elements, Aslist into list, etc.)

  

Data Structures in Java (Collection | container)

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.