Have you mastered the characteristics and underlying principles of HashMap that big factories and big cows have mastered?

Preface

HashMap stores key-value pairs for quick access and allows null. The key value cannot be repeated. If the key value is repeated, it will be overwritten. This article will share with you the interview questions of HashMap. It is very good and has a certain reference value. Friends who need it can refer to
the characteristics of HashMap.

  • HashMap stores key-value pairs for quick access and allows null. The key value cannot be repeated. If the key value is repeated, it will be overwritten.
  • .Asynchronous, thread insecure.
  • The bottom layer is a hash table, which does not guarantee order (such as the order of insertion)

Theme

1. What is the underlying principle of HashMap?

HashMap is based on the principle of hashing. After jdk8, the data structure of array + linked list + red-black tree is adopted. We store and get objects through put and get. When we pass the key and value to the put() method, first do a hashCode() calculation on the key to get it in

The location in the bucket array to store Entry objects. When getting the object, get the bucket location through get, then find the correct key-value pair through the equals() method of the key object, and then return the value object.

2. How is put implemented in hashMap?

  • Calculate the hashcode value of the key (exclusive or operation with the high 16 bits of Key.hashCode)
  • If the hash table is empty, call resize() to initialize the hash table
  • If there is no collision, add the element directly to the hash table
  • If a collision occurs (the hashCode value is the same), three judgments are made:

1. If the key address is the same or the content after equals is the same, replace the old value. If it is a red-black tree structure, call the tree insertion method
2. Linked list structure, loop until a node in the linked list is empty, insert the tail insert method , After inserting, judge whether the number of linked lists reaches the threshold of 8 to become a red-black tree;
3. It is also possible to traverse until the hash value and content of the inserted element are the same as that of the node, and cover it.

  • If the bucket is full greater than the threshold, resize to expand

3. How is get implemented in hashMap?

Hashing the hashCode of the key, and calculate the subscript to obtain the bucket position. If it can be found at the first position of the bucket, return it directly. Otherwise, find it in the tree or traverse the linked list. If there is a hash conflict, use the equals method to go. Traverse the linked list to find nodes.

4. What happens when the hashcodes of two objects are the same?

Hash collision will occur. If the key value is the same, replace the old value. Otherwise, it will be linked to the back of the linked list, and the linked list will be stored in a red-black tree if the length of the linked list exceeds the threshold 8. The HashCode is the same, and the value object is obtained by comparing the content through equals.

5. The difference between HashMap and HashTable

  • The same point: all store key-value key-value pairs
  • Differences:
    HashMap allows the Key-value to be null, while hashTable does not;
    hashMap does not consider synchronization, which is thread-unsafe. HashTable is thread-safe and adds a layer of synchronized decoration to the api;
    HashMap inherits from the AbstractMap class, and hashTable inherits from the Dictionary class. Iterator. The iterator of HashMap is a fail-fast iterator, while the enumerator iterator of Hashtable is not a fail-fast. So when other threads change the structure of HashMap (add or remove elements), ConcurrentModificationException will be thrown. The initial value of the capacity and the increase method are different: the default capacity of HashMap is 16; when increasing the capacity, the capacity is changed to "original capacity x2" each time. The default capacity of Hashtable is 11; when increasing the capacity, each time the capacity is changed to "original capacity x2
    • 1"; The hash value algorithm when adding key-value is different: when adding elements to HashMap, a custom hash algorithm is used. Hashtable does not have a custom hash algorithm, but directly uses the hashCode() of the key.

6. What is HashSet?

HashSet implements the Set interface, which does not allow duplicate values ​​in the collection. When we mention HashSet, the first thing is to make sure that the object rewrites the equals() and hashCode() methods before storing the object in the HashSet , So that we can compare whether the values ​​of the objects are equal to ensure that no equal objects are stored in the set. If we do not override these two methods, the default implementation of this method will be used. The public boolean add(Object o) method is used to add elements to the Set. When the element value is repeated, it will immediately return false, and if it is successfully added, it will return true.

7. The difference between HashSet and HashMap?

Insert picture description here

8. Disadvantages of traditional hashMap (why introduce red-black trees?

Before JDK 1.8, the implementation of HashMap was an array + linked list. Even if the hash function is good, it is difficult to achieve a 100% uniform distribution of elements. When a large number of elements in the HashMap are stored in the same bucket, there is a long linked list under this bucket. At this time, the HashMap is equivalent to a singly linked list. If the singly linked list has n elements, the time complexity of traversal is O(n), completely lost its advantages. In response to this situation, JDK 1.8 introduced a red-black tree (search time complexity is O(logn)) to optimize this problem

9. What type of element is generally used as Key when using HashMap?

Choose Integer, String, this immutable type, like all operations on String are creating a String object, splicing and splitting the new object, etc. These classes have already standardized the hashCode() and equals() methods. . Being an immutable class is inherently thread-safe,

10. What if the size of the HashMap exceeds the capacity defined by the load factor?

Exceeding the threshold value will be expanded. In general, the expanded array will be twice the size of the original array. Re-hashing the original elements into the new hash table.

to sum up

HashMap is a knowledge point that interviewers must ask. Its internal basic implementation principles should be mastered by every interviewer. Only when you truly master the internal implementation principles of HashMap, will you not be in a hurry in the face of interviewers’ torture .
It is the golden recruitment period of 9 and 10 gold, are you all ready? I collected the interview questions of some major manufacturers and the latest information of this year (2020). The following are some screenshots of the information (all the information has been integrated into a document, and the pdf is compressed and packaged).
If you need all the latest information about JAVA interview questions and 2020, you can click here to get the code: qf
Insert picture description here

Guess you like

Origin blog.csdn.net/SpringBoot_/article/details/108671960
Recommended