HashMap, treemap, LinkedHashMap learning
Map is mainly used to store key value pairs and obtain values according to keys. Therefore, duplicate keys (repeated and overwritten) are not allowed, but duplicate values are allowed. HashMap is the most commonly used map. It stores data according to the hashcode value of the key. Its value can be obtained directly according to the key. It has fast access speed. During traversal, the order of obtaining data is completely random. HashMap allows at most one record key to be null; Multiple records are allowed to have null values; HashMap does not support thread synchronization, that is, multiple threads can write HashMap at any time; Data inconsistency may result. If synchronization is required, you can use the synchronized map method of collections to enable HashMap to be synchronized, or use concurrenthashmap. Hashtable is similar to HashMap. It inherits from the dictionary class. The difference is that it does not allow the recorded key or value to be empty; It supports thread synchronization, that is, only one thread can write the hashtable at any time, which also leads to the slow writing of the hashtable. LinkedHashMap saves the insertion order of records. When traversing LinkedHashMap with iterator, the records obtained first must be inserted first You can also sort by application times with parameters during construction. It will be slower than HashMap when traversing, but there is an exception. When HashMap has a large capacity and less actual data, it may be slower than LinkedHashMap, because the traversal speed of LinkedHashMap is only related to the actual data and has nothing to do with the capacity, while the traversal speed of HashMap is related to its capacity. Treemap implements the sortmap interface, which can sort the records it saves according to the key. By default, the key values are sorted in ascending order. You can also specify the sorting comparator. When traversing treemap with iterator, the records obtained are sorted.
Generally, we use HashMap most. The key value pairs stored in HashMap are random when taken out. It stores data according to the hashcode value of the key, and its value can be obtained directly according to the key, which has a very fast access speed. HashMap is the best choice for inserting, deleting and locating elements in map. The sorted key value pairs are retrieved from the treemap. But treemap is better if you want to traverse keys in natural or custom order. LinkedHashMap is a subclass of HashMap. If the order of output is the same as that of input, LinkedHashMap can be used. It can also be arranged according to the reading order, such as application in connection pool.
1. HashSet is implemented through HashMap and TreeSet is implemented through treemap, but set only uses key2 of map The key and set of map have a common feature, that is, the uniqueness of the set Treemap has an additional sorting function. 3 Hashcode and equal () are used by HashMap. Because there is no need to sort, you only need to focus on positioning and uniqueness a. Hashcode is used to calculate the hash value, which is used to determine the hash table index b. An index in the hash table stores a linked list, so you need to use the equal method to cycle and compare each object in the chain before you can really locate the entry corresponding to the key value c. When put, if it is not located in the hash table, add an entry in front of the linked list. If it is located, replace the value in the entry and return the old value4 Because the treemap needs sorting, a comparator is needed for size comparison Of course, it is also positioned with comparator a. The comparator can specify B when creating a treemap. If it is not determined when creating a treemap, it will use key Compareto() method, which requires that the key must implement the comparable interface c. Treemap is implemented using the tree data structure, so the location can be completed by using the compare interface
Note: 1. The collection does not have a get () method to get an element. Elements can only be traversed through iterator(). 2, as like as two peas, Set and Collection have the same interface. 3. List, you can get one element at a time through the get () method. Use numbers to select one of a bunch of objects, get (0). (add / get) 4. Generally use ArrayList. Use LinkedList to construct stack, queue and queue. 5. Map uses put (k, V) / get (k), and containskey() / containsvalue() to check whether it contains a key / value. HashMap will use the hashcode of the object to quickly find the key* Hashing hash code is to transform the information of the object into a unique int value, which is stored in an array. We all know that array is the fastest of all storage structures. Therefore, you can speed up the search. When a collision occurs, let the array point to multiple values. That is, a table is generated at each position of the array. 6. The key sequence and value sequence can be extracted separately from the elements in the map. Use keyset() to extract the key sequence and generate a set from all the keys in the map. Use values () to extract the value sequence and generate a collection of all values in the map. Why does one generate a set and the other a collection? That's because key is always unique and value is allowed to be repeated.