Ask a big factory for an offer: Map interview questions

Preface

The text has been included in my GitHub : https://github.com/ZhongFuCheng3y/3y , there are more than 300 original articles, and the interview series is being serialized recently !

I, Sanwai, recently started writing an interview series. I gave this interview series a name called " Ask a big factory for an offer "

So this article is called " Ask a big company for an offer: Map interview questions "

Let's start next.

Interview site

Sanwai : "My name is Sanwai . I currently maintain a public account called Java3y . In the past few years, I have written 300+ original technical articles, nearly 1,000 pages of original e-books and mind maps with multiple knowledge points. My vision is: As long as you pay attention to me and the third consecutive classmates can get Dachang offer . My..."

Three crooked: "Map is an interface in Java, and the common implementation classes are HashMap, LinkedHashMap, TreeMap and ConcurrentHashMap"

Three crooks: "The first thing to be clear is: in Java, the structure of 数组+链表the hash table is the way. The underlying data structure of HashMap is 数组+链表/红黑树, the underlying data structure of LinkedHashMap is , the underlying data structure of 数组+链表+双向链表TreeMap is red-black tree, and the underlying data structure of ConcurrentHashMap is also 数组+链表/红黑树"

Interviewer: "Let’s start with HashMap first. Can you tell me what happens when you have newa HashMap?"

Three crooked: "HashMap has several construction methods, but the most important thing is to specify the initial value size and the size of the load factor. If we do not specify, the default HashMap size is 16, and the size of the load factor is 0.75"

Three crooked: "The size of the HashMap can only be a power of 2. If you pass a 10 in, in fact the final size of the HashMap is 16, you pass a 7 in, and the final size of the HashMap is 8. tableSizeForYou can see the specific implementation To. When we put the element into the HashMap, we need to calculate the location (hash) of the element. In the HashMap, the bit operation is used instead of the modulus, which can calculate the location of the element more efficiently . Why the size of the HashMap It can only be a power of 2, because only when the magnitude is a power of 2, can we reasonably use bit operations instead of modulo ."

Three crooks: "The size of the load factor determines the expansion of the hash table and the hash conflict . For example, my default HashMap size is 16 and the load factor is 0.75, which means that the array can only hold 12 elements at most. If there are 12 elements, the hash table needs to be expanded. How to calculate it to be 12? Very simple, that is 16*0.75. Every time an putelement enters, it will check whether the size of the HashMap exceeds this threshold. If so, it needs to be expanded."

Three crooked: "In view of the above statement (the size of HashMap can only be a power of 2), so when expanding the capacity, the default is to expand the original by 2 times."

Three skew: "Obviously the expansion operation must be time-consuming, then can I increase the load factor a bit, for example, if I want to adjust it to 1, then my HashMap will only expand when there are 16 elements. Obviously it is Yes, but not recommended. The load factor has been increased, which means that the probability of hash collisions will increase, and the probability of hash collisions will increase, which will also be time-consuming (because the search speed is slower)"

Three crooked: "The realization is based on the hashmethod. What can be found is that it first calculates the normal hash value, and then performs an exclusive OR operation with the high 16 bits to generate the final hash value. The advantage of this can increase the randomness. Sex , reducing the possibility of collision."

Three crooks: "At putthe time, first perform a hash operation on the key to calculate the index where the key is located. If there is no collision, put it directly into the array. If there is a collision, you need to determine whether the current data structure is a linked list or a red-black tree. Insert in different situations. Assuming that the key is the same, replace with the original value. Finally, judge whether the hash table is full (the current hash table size *load factor), if it is full, expand the capacity.

Three crooks: "At getthe time, I still perform a hash operation on the key, calculate the index where the key is located, and then determine whether there is a hash conflict, if there is no direct return, if there is, then determine whether the current data structure is a linked list or a red-black tree, respectively Take it out of a different data structure."

Three crooks: "First, the hash value is compared, and then the ==operator and is equals()used to determine whether the element is the same. To put it bluntly: if only the hash value is the same, it means that the element has a hash conflict. If the hash value and equals() || ==are the same, then Explain that the element is the same."

Three deviations: "When the size of the array is greater than 64 and the size of the linked list is greater than 8 , the linked list will be changed to a red-black tree. When the size of the red-black tree is 6 , it will degenerate into a linked list. Here, the red-black tree will degenerate into a linked list. The operation is mainly based on performance considerations during query and insertion . Linked list query time complexity is O(N), insertion time complexity O(1), red-black tree query and insertion time complexity O(logN)"

Three crooked: "In fact, LinkedHashMap is not used much in daily development. As mentioned earlier, the underlying structure of LinkedHashMap is 数组+链表+双向链表". In fact, it inherits HashMap and maintains a doubly linked list on the basis of HashMap . With this doubly linked list, our insertions can be "ordered". The order here does not refer to order of size, but order of insertion .

Three crooked: "LinkedHashMap actually uses a doubly linked list to traverse when traversing, so the size of LinkedHashMap will not affect the performance of traversal"

Three crooks: "TreeMap is not used much in real development. The underlying data structure of TreeMap is a red-black tree. The key of TreeMap cannot be null (if it is null, how to sort it?), TreeMap is ordered through Comparator For comparison, if the comparator is null, then the natural order is used "

Three crooks: "HashMap is not thread-safe. In a multi-threaded environment, HashMap may have data loss and unable to obtain the latest data. For example, thread A putenters but thread B getcannot come out. We want thread safety. You can use ConcurrentHashMap"

Three crooks: "ConcurrentHashMap is a thread-safe Map implementation class, which is under the jucpackage. In addition to ConcurrentHashMap, there is another thread-safe Map implementation class called Hashtable. Of course, Collections can also be used to package a thread-safe Map. But Both Hashtable and Collections packaging are relatively inefficient (because the synchronize is directly set in the outer layer), so we generally have thread safety considerations, and we use ConcurrentHashMap"

Three crooks: "The underlying data structure of ConcurrentHashMap is 数组+链表/红黑树that it can support high concurrent access and update, and is thread-safe. ConcurrentHashMap achieves synchronization by locking parts and using the CAS algorithm . There getis no lock at the time, and Node uses it. To volatilemodify. When expanding, each thread will be assigned a corresponding interval , and in order to prevent putValdata inconsistency, the thread's responsible interval will be locked"

Three crooked: "No, I won't"

Three crooked: "I have also seen JDK7 HashMap and ConcurrentHashMap when I was studying. In fact, there are still many differences. For example, the HashMap of JDK 7 uses the head interpolation method when expanding, but it becomes the tail interpolation method in JDK8. HashMap in JDK7 has not yet introduced red-black trees... ConcurrentHashMap is still implemented in JDK7 using segmented locks, but JDK 8 is different. But most of the details of JDK 7 have been forgotten."

Three crooked: "I haven't used the JDK 7 API, I thought I should use JDK8 at least now, right? So I didn't read it carefully. How about I tell you about multi-threading? "

Three crooked: "Oh"

Digression

For this interview, you may want to know more about the details of Map, such as Map基础知识/HashMap/LinkedHashMap/TreeMap/ConcurrentHashMapthe source code, you can look through my previous articles on CSDN !

Welcome to like, this is my motivation to update!

Guess you like

Origin blog.csdn.net/Java_3y/article/details/108253021