Java data structure (set | container) and java Data Structure
Make a small personal summary of the data structure in java, although it is not yet thorough research into the jdk source code. First, why does the collection need to appear in java? What are the requirements. I think objects can be used to describe anything for Object-Oriented purposes. In order to facilitate object operations and storage, a set appears. A set should be the most common method for storing objects, it is more flexible and variable length than array. At the same time, the set can only store objects and cannot make basic data types.
The Collection framework consists of Collection and Map. Each Collection type is based on a specific data structure. The Collection interface inherits the Iterable interface, so you can use iterator () of the Iterable interface () the returned value is an Iterator interface type, which is the Iterator in the Collection set. The Iterator is a Collection element removal method that can directly access the elements in the set, it is defined in the set to facilitate the extraction of elements in the set. It can be used to retrieve and delete elements (next (), remove () in the set ()) when using the iterator to operate data, you cannot use the set method to operate the set, because the iterator has already defined a set for subsequent iterator operations on the set, it is not a set defined previously, and this conflicts. Its Sub-interface ListIterator can add, delete, modify, and query elements in the Set (in fact, it is the object reference stored in the set of operations, not the real object itself, because the collection stores the object reference address ).
The Collection interface is further divided into List and Set interfaces. The corresponding List interfaces are commonly used in implementation classes: ArrayList and sorted List. The underlying data structure of the ArrayList class (that is, the data storage method) is an array interface, it is ordered with a badge index and can be repeated, while the underlying layer of the sort list class is a linked table data structure, the equals method used to determine whether the elements are the same is faster than the ArrayList method to add or delete data to the set data because only the first and last links need to be adjusted, and the ArrayList needs to be re-adjusted for the index, therefore, the addition and deletion operations on large data volumes are still more efficient than the merge list, and the query operation ArrayList is faster. They are all out-of-the-box and out-of-sync (ArrayList replaces Vector ). The Set interface is commonly used in two classes: HashSet and TreeSet. The underlying layer of the HashSet interface is a hash table interface. It is unordered, non-repetitive, and the thread is not synchronized, to determine whether the elements are the same, the hashCode method returns 0. If the value is 0, the equals method is determined to check whether the return value is true. The underlying layer of TreeSet is a binary tree structure. It is an ordered set that cannot be duplicated and threads are not synchronized. The way to ensure data uniqueness is to check whether the compareTo method returns 0. There are two methods for the TreeSet sorting set: 1. Make the stored elements have a comparison, that is, implement the Comparable interface to overwrite the compareTo method. 2. The custom Comparator implements the Comparator interface to customize the compareTo method, and then uses the Comparator as the parameter to access the TreeSet constructor. The comparison is achieved when the TreeSet is initialized.
The Map interface is an interface at the same level as the Collection interface, and the Map interface stores a key ing relationship in the key-value form. Therefore, Map is the first choice for data that needs to store ing relationships, commonly used Map types include HashMap and TreeMap. The underlying data structure of HashMap is also a hash table, which can be stored using null, And the threads are not synchronized. This replaces the thread of HashTable for non-synchronization, null values cannot be stored. TreeMap bottom layer is also a binary tree data structure. Similarly, Map uses entrySet () to retrieve data () the method returns the Set and then uses the iterator to retrieve or enhance the for loop (the Set that implements the Iterable interface can all use foreach for loop). The ing relation returned by entrySet is a Map. entry type because Entry is actually defined in Map. Why? Because only a Map set and a key-value pair have a ing relationship between key-value pairs, the relationship belongs to the internal thing in the set and directly accesses the elements in the set. In actual application, I think more is the nesting of map.
Tool class (Collections, Arrays) Collections collection tool class is a special tool class that combines operations and defines static methods. You can perform various sorting binary searches on the List set (similar to string operations) the thread security can also be converted to thread security, the same Arrays is defined in the tool class for Array Operations and static methods are used for various Array Operations (toString prints array elements, asList converts to list, etc)