Talking about HashMap(1)

Let's briefly introduce the data type of HashMap . First , Java defines an interface java.util.Map for the mapping in the data structure, and then HashMap is one of the implementation classes of this interface. Its internal implementation is an implementation class in java1.7 and before. A data structure called a linked list hash , that is, an array plus a linked list data structure.

 

When the Put method is called to store the value , the hashcode method will be called first to calculate the hash value of the key , and then the key and value will be put into the linked list under the array corresponding to the hash value . ( HashMap allows the key of a record to be null , The value of multiple records is allowed to be null, because when calculating the hash value , it will first determine whether the key is Null, and if it is null , the data will be put in the first place of the map )

When the get method is called to obtain the value, the hashcode will be calculated according to the key value to find the corresponding array , and then the corresponding value value will be queried according to the key value in the array.

Simply put, the hash algorithm can divide the data into a row of hash buckets, and put the data with the same hash value into the same one.

Hash bucket , if two objects have the same hash value, they must be the same , but the two objects are not necessarily the same. For example , "Ea" and "FB" have the same hash value , but different objects

When storing a custom object, you need to override the hashCode and equals methods

Why rewrite the hashCode method : Because by default , the hashCode method maps the storage address of the object . When two new custom objects are created, the storage addresses of different objects must be different , so the resulting hashCode values ​​must also be different.

Why rewrite the equals method : Because to judge whether two objects are logically equal, it is necessary to judge whether the instances of the two classes are equal according to the member variables of the class, and the equals method in inheriting Object can only judge whether the two reference variables are not. is the same object

Why can HashMap guarantee the uniqueness of Key ?

Because HashMap will first find the linked list corresponding to which array element the data is in through the hash value of the key when fetching the data , and if it is the same, it will traverse the linked list to find the value corresponding to the key value .

Why does HashMap need to use a hashing algorithm ?

Because of efficiency . If you check every time you add an element, then when there are many elements, the number of comparisons of elements added to the collection is very high. That is, if the collection now has 1000 elements, then when the 1001st element is added to the collection, it will call the equals method 1000 times . This obviously reduces efficiency considerably. Calculating the hash value is equivalent to calculating the index . Accessing the value through the index can greatly speed up the efficiency of the operation because the actual number of calls to the equals method is greatly reduced, and it only takes one or two times.

 

 

Expansion :

Hashmap,HashTable,LinkedHashMap,TreeMap

(1) HashMap:它根据键的hashCode值存储数据,大多数情况下可以直接定位到它的值,因而具有很快的访问速度,但遍历顺序却是不确定的。 HashMap最多只允许一条记录的键为null,允许多条记录的值为nullHashMap非线程安全,即任一时刻可以有多个线程同时写HashMap,可能会导致数据的不一致。如果需要满足线程安全,可以用 CollectionssynchronizedMap方法使HashMap具有线程安全的能力,或者使用ConcurrentHashMap

(2) Hashtable : Hashtable is a legacy class. The common functions of many mappings are similar to HashMap . The difference is that it inherits from the Dictionary class and is thread-safe. Only one thread can write Hashtable at any time, and the concurrency is not as good as ConcurrentHashMap , because ConcurrentHashMap introduces segment locks. Hashtable is not recommended to be used in new code. It can be replaced by HashMap when thread safety is not required, and ConcurrentHashMap can be used when thread safety is required .

(3) LinkedHashMap : LinkedHashMap is a subclass of HashMap , which saves the insertion order of records. When using Iterator to traverse LinkedHashMap , the first obtained record must be inserted first, or it can be constructed with parameters and sorted according to the access order.

(4) TreeMap : TreeMap implements the SortedMap interface, which can sort the records it saves according to the key. The default is to sort in ascending order of the key value. You can also specify a sort comparator. When iterator is used to traverse the TreeMap , the obtained records are sorted. of. If using sorted maps, TreeMap is recommended . When using TreeMap , the key must implement the Comparable interface or pass in a custom Comparator when constructing the TreeMap , otherwise an exception of type java.lang.ClassCastException will be thrown at runtime .

For the above four types of Map classes, the key in the map is required to be an immutable object. Immutable objects are objects whose hash value cannot be changed after the object is created. If the hash value of the object changes, the Map object may not be able to locate the mapped location.

 

-----------------------------------------------------------------------------------------------------------------------------

Java 1.8 array + linked list + red-black tree

When the length of the linked list is greater than the default value of 8 , it will be converted to a red-black tree by default to increase the performance of HashMap

-----------------------------------------------------------------------------------------------------------------------------

 

ConcurrentHashMap (king Kerat )  segment lock

Use segmented load synchronization lock for data . By default, 16 threads can concurrently operate data at the same time . Compared with Hashtable , the synchronization concurrency is higher and the efficiency is faster .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324597995&siteId=291194637