1. Talk about how HashMap works
If you are asked a question about HashMap and how it works, it will be used as an interview opener, so pretend to be thoughtful and calm down. First, HashMap is based on hashing. We know that there are two common methods for HashMap: put() and get(). When you pass a key-value pair to put(), it calls the hashCode() method of the key object to calculate hashCode. Then find the bucket location to store the value object. When an object is retrieved, the correct key-value pair is found through the equals() method of the key object, and the value object is returned.
In general, you are sure to ask what happens if hashCode values of different key objects are equal. They must be stored in a linked list in the same location, using the equals() method of the key object to find the key-value pairs.
HashMap + linked list + red black tree image from open source China
After Java1.8, HashMap added red-black tree data structure on the basis of array and linked list. The data structure is converted to a red-black tree structure when the list length is greater than 8 in an array position.
2. What is the difference between HashMap and HashTable?
Both HashMap and HashTable implement the Map interface, and the main differences are in threads, synchronization, and speed. A HashMap is asynchronous, and a HashMap can store objects with null keys, allowing up to one key to be a NULL object and multiple values to be NULL.
HashMap is thread-safe, HashTable is thread-safe.
Because HashTable is multithreaded, HashMap is faster in the case of a single thread.
A HashMap does not guarantee that the storage order is constant.
What is the difference between a HashMap and a HashSet?
HashMap implements the Map interface, while HashSet implements the Set interface.
A HashMap stores key-value pairs, and a HashSet stores objects.
A HashSet is slower than a HashMap.
A hashcode value is computed differently, in that a HashMap uses a key object and a HashSet uses its own object element.
4. What happens when two objects have the same HashCode?
Because the HashCode is the same, their bucket positions are the same, and a ‘collision’ occurs. Because HashMap uses a linked list to store objects, this Entry(a Map.entry object with key-value pairs) is stored in the linked list.
5. If two keys have the same HashCode, how do you get the value object?
When we call the get() method, the HashMap uses the hashcode of the key object to find the bucket location and then retrieves the value object. Once the bucket location is found, the keys.equals() method is called to find the correct node in the linked list and eventually the value object.
6. What is the problem with resizing the HashMap in multi-threaded situations?
Due to thread insecurity, multiple HashMap objects compete to resize a HashMap in multithreaded conditions, and it is not clear which one to resize. As a result, adjusting the size of the HashMap in multithreading can lead to an infinite loop, and since Java1.5 ConcurrentHashMap objects have been added to address multithreading and other issues.
More exciting to wechat public number [Lao Wang said programming], focus on back-end programming combat, original articles updated every day!