To be honest, the degree of understanding of Java collections is considered qualified, even if it is a big cow among big cows, a big god among big gods, what is the advantage of knowing the principle of collection, can it be said that if you want to rewrite Java collections, write your own performance? Is it better?

It’s no use but! If you don’t answer, then you can only go back and wait for the notice… So do you want to give the interviewer some work? Maybe we only need to know that HashMap is an asynchronous implementation of the Map interface based on the hash table. This implementation provides all optional mapping operations and allows the use of null values ​​and null keys. This class does not guarantee the order of the mapping, especially it does not guarantee that the order will last forever. HashMap is actually a “linked list hash” data structure, that is, a combination of array and linked list. Why do you need to ask the principle? Another way to ask is how HashMap finds and stores values ​​efficiently.. HashMap uses Map.Entry internally to store the key and value, uses the put(key, value) method to store the value, and uses get( key) method to find the value. Share a 24W Java interview manual, which includes network protocol, Java basics, advanced, string, collection, concurrency, JVM, data structure, algorithm, MySQL, Redis, Mongo, Spring, SpringBoot, MyBatis, SpringCloud, Linux And various middleware (Dubbo, Nginx, Zookeeper, MQ, Kafka, ElasticSearch), etc… Link: Fatty Tiger A: It took a month to compile a 240,000-word Java interview manual. For example, often in interviews You will encounter HashMap related questions: 1. “Have you used HashMap? What is HashMap? Why do you use it?” Almost everyone will answer “Yes” and then answer some of the characteristics of HashMap, such as HashMap can accept null Key-value and value, while Hashtable cannot; HashMap is non-synchronized; HashMap is fast; and HashMap stores key-value pairs and so on. This shows that you have used HashMap and are quite familiar with it. But the interviewer took a sharp turn and started asking some tricky questions from now on, about more basic details about HashMap. The interviewer may ask the following questions: 2. “Do you know the working principle of HashMap? Do you know the working principle of the get() method of HashMap?” You may answer “I did not check the standard Java API in detail, you You can look at the Java source code or Open JDK.” “I can use Google to find the answer.” But some interviewers may be able to give the answer, “HashMap is based on the principle of hashing, we use put (key, value) to store objects to HashMap , Use get(key) to get the object from the HashMap. When we pass the key and value to the put() method, we first call the hashCode() method on the key, and the returned hashCode is used to find the bucket location to store the Entry object.” The key point here is to point out that HashMap stores key objects and value objects in the bucket as Map.Entry. This is helpful to understand the logic of obtaining objects. If you are not aware of this, or mistakenly believe that only the value is stored in the bucket, you will not answer the logic of how to get the object from the HashMap. This answer is quite correct, and it also shows that the interviewer really knows the working principle of hashing and HashMap. But this is only the beginning of the story. When the interviewer added some actual scenes that Java programmers encounter every day, wrong answers frequently appeared. The next question may be about collision detection in HashMap and collision resolution: 3. “What happens when the hashcodes of two objects are the same?” From here, the real confusion begins. Some interviewers will Answer Because the hashcode is the same, the two objects are equal, the HashMap will throw an exception, or they will not be stored. Then the interviewer may remind them that there are equals() and hashCode() methods, and tell them that even if the two objects have the same hashcode, they may not be equal. Some interviewers may give up on this, while others can continue to advance. They answered “Because the hashcode is the same, so their bucket locations are the same, and’collision’ will occur. Because HashMap uses a linked list to store objects, this Entry (contains key-value pairs) The Map.Entry object) will be stored in the linked list.” This answer is very reasonable. Although there are many ways to deal with collisions, this method is the simplest, and it is exactly the way to deal with HashMap. But the story is not over yet, the interviewer will continue to ask: 4. “If the hashcodes of the two keys are the same, how do you get the value object?” The interviewer will answer: When we call the get() method, HashMap will use the hashcode of the key object Find the bucket location, and then get the value object. The interviewer reminded him that if there are two value objects stored in the same bucket, he gives the answer: it will traverse the linked list until the value object is found. The interviewer will ask because you don’t have a value object to compare, how are you sure to find a value object? Unless interviewees until HashMap stores key-value pairs in the linked list, they will not be able to answer this question. Some interviewers who remember this important knowledge point will say that after finding the bucket location, they will call the keys.equals() method to find the correct node in the linked list, and finally find the value object they are looking for. The perfect answer! In many cases, interviewers will make mistakes in this link because they confuse the hashCode() and equals() methods. Because the hashCode() appeared repeatedly before this, and the equals() method only appeared when the value object was obtained. Some good developers will point out that the use of immutable, declared final objects, and the use of appropriate equals() and hashCode() methods will reduce collisions and improve efficiency. Immutability makes it possible to cache the hashcode of different keys, which will increase the speed of the entire object acquisition. It is a very good choice to use wrapper classes such as String and Interger as the key. If you think it’s over here, you will be surprised when you hear the following question. 5. “What if the size of HashMap exceeds the capacity defined by load factor?” Unless you really know how HashMap works, you won’t be able to answer this question. The default load factor size is 0.75, that is to say, when a map fills up 75% of the bucket, like other collections (such as ArrayList, etc.), a bucket array twice the size of the original HashMap will be created to recreate Adjust the size of the map and put the original objects into the new bucket array. This process is called rehashing, because it calls the hash method to find the new bucket location. If you can answer this question, the following questions come: 6. “Do you know what is wrong with resizing the HashMap?” You may not be able to answer it. At this time, the interviewer will remind you that when you are multi-threaded, it may Generate a race condition. When the HashMap is resized, there is indeed a race condition, because if both threads find that the HashMap needs to be resized, they will try to resize at the same time. In the process of resizing, the order of the elements stored in the linked list will be reversed, because when moving to a new bucket position, HashMap will not put the elements at the end of the linked list, but at the head. This is To avoid tail traversing. If conditional competition occurs, then there is an endless loop. At this time, you can ask the interviewer, why is it so strange to use HashMap in a multi-threaded environment? ) Supplement: More questions about HashMap: 1. Why are wrapper classes such as String and Interger suitable as keys? Wrapper classes such as String and Interger are suitable as the keys of HashMap, and String is the most commonly used. Because String is immutable and final, and the equals() and hashCode() methods have been rewritten. Other wrapper classes also have this feature. Immutability is necessary because in order to calculate the hashCode(), the key value must be prevented from changing. If the key value returns a different hashcode when it is put in and when it is obtained, then the object you want cannot be found in the HashMap. Immutability has other advantages such as thread safety. If you can guarantee that the hashCode is constant just by declaring a field as final, then please do so. Because the equals() and hashCode() methods are used when obtaining the object, it is very important that the key object rewrite these two methods correctly. If two unequal objects return different hashcodes, the chance of collision will be smaller, which can improve the performance of HashMap. 2. Can we use custom objects as keys? This is an extension of the previous question. Of course, you may use any object as a key, as long as it complies with the definition rules of equals() and hashCode(), and the object will not change after it is inserted into the Map. If this custom object is immutable, then it has already met the condition of being a key, because it cannot be changed after it is created. 3. Can we use CocurrentHashMap instead of Hashtable? This is another very popular interview question, because more and more people use ConcurrentHashMap. We know that Hashtable is synchronized, but ConcurrentHashMap has better synchronization performance because it only locks a part of the map based on the synchronization level. ConcurrentHashMap can certainly replace HashTable, but HashTable provides stronger thread safety. I personally like this question very much, because the depth and breadth of this question do not directly involve different concepts. Let’s take a look at the knowledge points of these problems: the concept of hashing, the application of equals() and hashCode() to solve collisions in HashMap, and their importance in HashMap. The benefits of immutable objects. HashMap multi-threaded conditional competition Re-adjust the size of HashMap. The working principle of HashMap HashMap is based on the principle of hashing. We store and obtain objects through put() and get() methods. When we pass the key-value pair to the put() method, it calls the hashCode() method of the key object to calculate the hashcode, and then finds the bucket location to store the value object. When obtaining an object, find the correct key-value pair through the equals() method of the key object, and then return the value object. HashMap uses a linked list to solve the collision problem. When a collision occurs, the object will be stored in the next node of the linked list. HashMap stores key-value pair objects in each linked list node. What happens when the hashcodes of two different key objects are the same? They will be stored in a linked list in the same bucket location. The equals() method of the key object is used to find key-value pairs.


By stockin

0 0 vote
Article Rating
Notify of
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
7 months ago

The source code of HashMap, as long as you can understand it, there will be no such problem. I like to ask others about HashMap in interviews. Why? First of all, HashMap is a collection of commonly used data structures: arrays; linked lists; red-black trees (trees); and the main reason for the fast query performance of HashMap is the idea of ​​indexing by array subscripts and using space for time; you know this idea How many places does it apply? All places that speed up the query are the idea of ​​using indexes and space for time. Redis, Mysql’s B+tree, ES, etc. There are also many values ​​that have passed many tests: the threshold of expansion is 0.75 The threshold of the linked list to the red-black tree: 8 The threshold of the red-black tree degenerating into the linked list: 6 Of course, this part is not worth studying like the algorithm above. Of course, in fact, all of the above are not important to you. But at least you have to understand, when to use HashMap? Is HashMap just to provide you with a data structure for accessing values ​​through Key and Value? When the collection of HashMap is extremely small, the resource consumption is actually higher than that of arrays, and the performance is not very advantageous compared to arrays. When you need to use HashMap, if you know the principle, you will know the reason. Also, why does HashMap overwrite the existing value when it is put? Why does HashMap not allow duplicate keys? If you know the principle, you will know why. Of course, you may also say that you don’t want to know why these problems are mentioned above, you just need to know how to use and when you need to use it. Then I can’t say anything, because the height of your programmer is limited to this. HashMap is one of the small tools in this tool. If you are only a plumber, you definitely don’t need to know the lever principle of the wrench and where is the fulcrum. However, if you know the lever principle of a wrench, you are likely to give you pipe wrenches, jacks, and various similar tools, which you can basically master and use. However, in fact it is. Students who are good at “mastering” this principle will indeed have a clearer mind when thinking about problems, drawing inferences from one another, and checking loopholes.

7 months ago

To be honest, hashmap is almost one of the several data structure/collection classes that programmers need to master the principle most. Another problem that can be compared with it is “the difference between arrays and linked lists.” These two questions are also necessary questions when answering the main interview. Unfortunately, in the interview experience of the interviewee, less than half of the students can answer these questions well… The questions here are not the rocket-making questions that know the marketing account, but the most common usage, such as: “If To make a game, you need to know which users are online in the background… In this case, which data structure, array, linked list, or hashmap is suitable?”-In this case, at least you have to understand their advantages and disadvantages and complicated operation time Right? If the User object is directly used as the key of the hashmap (though this is generally not done), what are the precautions? What happens when the hash value is the same? “If there are multiple threads processing online and offline operations at the same time, can the native hashmap implementation provided by Java ensure thread safety?”-How did this pit come about? Will it deadlock, lose data, or hang into a loop? The answerer like that can’t take the test. That’s the language’s own pot, but at least you have to know that there may be a hole here, right? “Then how to get a thread-safe hashmap implementation?”-It is best if you understand ConcurrentHashmap. If you can understand the idea of ​​”segment lock”, you are qualified. If you don’t understand it, you can explain synchronized. “Then what structure is the database index implemented, can hashmap be used?”-If you understand the principle, this question should be explained well. Of course, some small databases can indeed choose hashmap as an index, but what is the additional price it pays, and why do some libraries use B+ trees? (Meow, when you talk about B+ trees, you get angry. Baidu Encyclopedia, which was cheating, actually said that B-trees, B-trees and B+ trees are three different things, and they explained the difference for a long time. I don’t know the old words. How many people have been pitted…) Wait a minute, don’t rush to say that this knowledge is meaningless, first look at this question-is the database index column suitable for UUID? Under what circumstances will a joint index fail during query? The procedure is the most reasonable. If you understand these principles, then you can live in harmony with it. If you don’t want to learn these principles, but just memorize scattered knowledge points of unknown principles-it is equivalent to forcing one’s brain as a hashmap, how much can one remember? (At least you have to get a Bloom Filter (squinting smile) Although it is said that there is a technical dead end, but if you really plan to take a few more steps on this road, then knowing it and knowing it is the right way. Finally, I will give an additional question. Well, anyway, the question bank has long been transparent, and you can’t grab people from the big companies (shangshou): “If you want to traverse this ConcurrentHashmap that stores online users in a multi-threaded environment, and get all online user information, what should be the best operation? Great?”

7 months ago

It’s no use, the interview asking HashMap can be said to be meaningless in today’s interviews. If the interviewer knows the source code of HashMap well and can sweat your question, then the interview question is meaningful. If the interview asking HashMap allows the interviewer to bite the bullet and read the source code and understand the ingenious design in HashMap, then this is also meaningful. The key is that in the current interview, the interviewer has not read the source code, and the interviewer has not read the source code. The interviewer’s knowledge of HashMap comes from the blog, and the interviewer’s knowledge of HashMap comes from the interview. This is not a test of HashMap at all, but a test of luck. It depends on whether the interviewer’s interview is copied from the interviewer’s blog. The current interviews are called stereotypes for no reason. For a person with a strong memory but no development experience, if the interview does not test the algorithm, the TA is fully capable of winning offers from all major companies. When I tried it a few years ago, I didn’t even open the source code of HashMap, but it didn’t prevent me from answering the questions of HashMap in the interview, because I knew that the interviewer hadn’t read it either. So what are good interview questions? A good interview question should meet at least one condition: if you can answer this question correctly, it means you have the ability I expect, not if you can answer this question correctly, it means you have memorized the answer. To give a few simple examples, for example, when I test the interviewer’s B+ tree, I don’t want the interviewer to memorize the concept of the B+ tree, but I hope the interviewer can know the actual role of the B+ tree in the database. There will be such questions: 1. I will first draw a B+ tree, and then let the interviewer draw how the tree will change after insertion and deletion. 2. I told the interviewer that the current B+ tree is an index, and then I gave a few queries about this index, and then asked why the index would fail during the query. If the interviewer can answer correctly, it means that the TA not only understands the principle of the B+ tree, but also thinks about whether the index will fail when doing database queries. And if I just ask about the concept of B+ tree, how to insert and delete B+ tree, then the interviewer can fully understand the answer on the back. I can only know if TA has memorized this knowledge point. As for the level of understanding of it, I Know nothing. For another example, many interviews like to test the isolation level of the database. The major faces summarize the isolation level of the database quite well, and many people just memorize the table in the face, and even the application scenarios of each level I don’t know why there is an isolation level. If it were me, I would cite a practical case and then ask what isolation level should be used. If the interviewer can answer correctly, it proves that the TA can make the right choice in the future work. ps: I’m currently writing study notes for database isolation level: The interview questions are now developing in a deformed direction. The test items are wide and comprehensive, and even the interviewer can’t answer it without looking at the answer. Going back to HashMap, I think that during the interview, you ask the interviewer to breakpoints and track the source code of HashMap expansion. It is better than asking these stereotyped questions every day, but the current situation is also due to the general environment. If you can’t change it, you can adapt to it, but To insist on attaching meaning to this kind of rubbish interview question, I don’t think it is necessary.

7 months ago

No use! What’s the use? More than the principle of hashmap is useless, you don’t even need to know the key value of hashmap, you really believe me. The key of the most basic map is not allowed to be repeated. I am afraid that it is not clear. You think you are clear. Anyway, I thought I was clear at the beginning. Then I actually told me that I was not clear. This is what I asked a fresh graduate who came to our company for half a year after graduation. I just asked him. Do you know that the key of the map cannot be repeated? He said he knew. I said that you know why you still write bugs in the code, and he said how could it be impossible. The demand is like this. A batch of data comes. I don’t want to insert it one by one. I want it to accumulate to 1000 or 5 seconds before inserting it in batches. This method is multi-threaded and there are a total of 8 threads that can call it. At first, I told him to remember to lock when you write. He said no. I don’t need to use list to connect, but I use ConcurrentHashMap to connect. I said whatever you want, but after two days I found that the data was missing, so I took a look at the code and found that he used the map key to store the timestamp. Then I told him that if you store the timestamp in the key, if multiple threads call this method at the same time, the value with the same key value will be replaced and there will be less data. He suddenly realized, I said, don’t you know that the key of the map cannot be repeated? Knowing what’s useful, doesn’t it still make a mistake when I use it? Then I asked him to lock and use the list to connect, and he said that I know that a map key can be repeated. What did I say? He said it was IdentityHashMap. I said you should not be too busy to make changes. You should first look at how this map is used and what are the shortcomings before you change it. After a while, he said that this map is not thread-safe, but you can use Collections.synchronizedMap(new IdentityHashMap(…)) to make it thread-safe. I said you go to see how Collections.synchronizedMap makes map thread safe. As a result, after a long time, he told me that it was used but why the data was inserted a lot, originally 20W, I inserted it in order to become 80W. I said, why don’t you go and see how the collections.synchronizedMap is locked? In the end, I told it that you should use the list to connect, and use the same lock and unLock this list, and it will be OK to add the lock during .add and insert. Sometimes we really solve the problem and go to Baidu to see the post that can solve our method and start to try, this works, that does not work. Why on earth is it not working, and why is this working. I don’t study it, and I will go to Baidu next time I encounter this problem. I feel that the problem is solved and everything is fine, what you think is what you think.

7 months ago

It is certainly useful to know the principle. But the problem is that many companies have a big business scale. When recruiting a CRUD engineer for an interview, you have to ask how to use ConcurrentHashMap in a high-concurrency scenario. After joining the company, I found that ConcurrentHashMap was never used in the code base. It is better to ask various questions. API details of the library. Candidates who memorize these details are more efficient at writing CRUD. On the other hand, this type of question is too modular and tied to a specific programming language (some even with a specific version). When asked a lot, there are templated answers on the Internet. Just look at some of the answers below this question. If you really want to examine whether candidates can flexibly use the tools provided by programming languages to solve performance problems in large-scale business scenarios, programming languages cannot be limited. Instead, candidates should focus on the process of analyzing scenarios, considering constraints, and constructing ideas. However, the interviewer may also know a programming language, and it is not easy to ask questions if the programming language is not limited.

7 months ago

One reason I hate Java is this kind of Java interview questions that I see on the Internet. It was originally a hash table for investigating the principle, but it turned into a hashmap for investigating memory. Not only hash tables, many of them are general knowledge and concepts in computers. For many Java programmers, it has become narrowed down to Java libraries and frameworks. For example: You may have a little knowledge of network basics, but you can talk about things in libraries and frameworks such as netty and spring.

7 months ago

The interviewer’s question about the principle of HashMap may be just to investigate whether you have the spirit of specializing in learning, because HashMap is the most used one. If you don’t even know the principle of HashMap, the interviewer can probably define that you are only interested in everything. Will use it, but I don’t know the principle. There are so many people who use it now, why did you choose you? So this question may be a condition for screening. Of course, the main thing is to know the principle in order to better use and solve the problem, this is the most important.

7 months ago

The interviewer asks you about the principle of hashmap, which is more about your mastery of the underlying principles, which involves algorithms and data structures. Many people will say: I know how to use it, why do I have to understand the underlying knowledge of the application? In fact, it is not: when dealing with complex tasks or solving complex bugs, it is very important to have deep knowledge of the underlying system. Such as data structure, network protocol, operating system related knowledge, etc. Many problems with programs stem from misunderstandings about how computers work, even programs developed in high-level languages. In addition, some architectures or frameworks that are more biased towards the application layer must be based on lower-level systems. Take HashMap as an example. In essence, it is a collection of commonly used data structures: arrays; linked lists; red-black trees (trees). Hashmap has a big advantage: the query speed is fast, the reason is that the space is exchanged for time: the index of the array subscript. This underlying idea is very important. Basically, in all areas of programming to speed up query speed, this idea is used: redis, Mysql, and Es are nothing more than the same. After understanding the underlying principles, we can see through the dazzling technology and not be burdened by endless new technologies. Another example is the rise of Docker technology, which has changed the way of CI/CD and promoted the development of cloud native technology. So what is Docker? The bottom layer is nothing more than: CGroups restricts resources, Namespace modifies process views, and rootfs provides a file system for the isolated execution environment for container processes. Only by understanding the underlying principles of Docker can you better control Docker in actual work. It is very important for programmers to consolidate the basic knowledge of computer system, including algorithms, data structures, operating systems, computer networks, computer composition principles, databases, and so on. Only when you have a deep grasp of these basic knowledge, and when you encounter high-performance, high-availability, and magical bugs, you will have enough countermeasures. Otherwise, you will really have to be anxious. Especially algorithms, for programmers, the most important thing.

7 months ago

CRUD is the most frequent requirement in software, and C query is the most important of them. There are two main algorithms for efficient search: ordered search (binary) and hash search. HashMap uses the principle of hash search. It can be seen that by understanding HashMap, you will understand half of the world of efficient search. In fact, HashMap also uses a red-black tree, which is a variant of ordered search. This means that if you have mastered the principle and implementation of HashMap, you have almost mastered the essence of efficient search. Kill two birds with one stone, why not do it? Without an understanding of principles and implementation, you cannot optimize the system. Hash is not only used for searching, but also used in many fields such as cryptography, distributed (the foundation of modern Internet application architecture). If you don’t know the principle, it looks like a bubble floating on the water, it looks like you can swim, but it’s actually very fragile. Some people say that I don’t need to know the principles of biology to eat, so why do I need to know the principles to write a program? This kind of analogy thinking doesn’t work in software development. Because eating is a physiological instinct, but making software requires brainpower. If humans have built-in instinct to write programs, then maybe they don’t need to know the principle. Unfortunately, humans are just bare metal, without any advanced software installed. If you don’t know the principle, when you encounter a slightly complicated problem, or something goes wrong online, you will be at a loss and don’t know what to do. Why do you want to know the principle? It’s because you haven’t encountered more complicated problems, because most of the problems for beginners can be solved by just searching the Internet and copying and pasting. Half of them stepped in, but the other foot hasn’t stepped in yet! Another analogy: You know that 2+2=4, but if you give you 24+8, will you forget it? If you understand the four arithmetic rules (principles), you will have the ability to learn by analogy and draw inferences. This is very important for the development industry that requires a lot of learning and continuous in-depth development. Of course, if you only want to do other careers for two years, then I wish you a smooth ride through these two years! Boy! Don’t just learn when you need it: Don’t just wait to eat when you are hungry.

Would love your thoughts, please comment.x