The main difference between HashedMap and HashMap lies in their features and design in implementing the Map interface. HashedMap was introduced in the JDK4 version. It makes certain improvements based on HashMap, adds additional features, and provides many subclassing methods. One of the core differences is thread safety. HashMap itself is not thread safe, which means that concurrency issues may arise when used in a multithreaded environment.
1. High concurrency scenarios: ConcurrentHashMap is preferred, and its segmented lock/CAS+synchronized design achieves the best balance between security and performance. Simple low-concurrency scenario: Collections.synchronizedMap () is available, but you need to strictly follow the traversal and locking specification. Legacy system maintenance: If Hashtable already exists, it is recommended to gradually migrate to ConcurrentHashMap. Absolutely avoid: Sync manually or use Hashtable directly.
2. There are three main ways to achieve Map thread safety in Java: Use HashtableHashtable to achieve thread safety by adding the synchronized keyword to all public methods (such as put, get, containsKey, etc.). The principle is to lock the entire hash table to ensure that only one thread can access the table at a time. Features: Simple and direct, but coarse lock granularity (full table lock).

3. Core thread safety mechanism: The segmented lock segmented structure ConcurrentHashMap divides the entire Map into multiple segments (segments), each of which is locked independently. For example, the number of segments is determined by bit operation during initialization (for example, the initial capacity of 16 corresponds to 16 segments), and each segment maintains a separate hash table.
4. In Java, you can create instances by using the default construction of ConcurrentHashMap or specifying the initial capacity, and use the atomic operation methods it provides (such as putIfAbsent, computeIfAbsent) to achieve thread-safe Map, while paying attention to its weak consistent iterator characteristics.
5. In Java, the core of using ConcurrentHashMap to ensure thread safety lies in using its built-in concurrency control mechanism (segmented lock/CAS+ synchronous lock) and atomic operation methods to avoid the complexity of manual locking.

6. ConcurrentHashMap achieves thread safety through segmented locking (JDK 7) and CAS+synchronized (JDK 8+) mechanisms, combines volatile to ensure visibility, and reduces lock competition through fine-grained synchronization, thereby improving concurrency performance.



1. The reason why HashMap wants to implement the writeObject and readObject methods itself is to ensure data consistency during serialization and deserialization. Since the Entry storage location of HashMap depends on the Hash value of the Key, and the Hash value may be different in different JVM implementations or running environments, HashMap requires a custom serialization and deserialization process.
Implementation principle of HashMap: First, there is an array where each element is a linked list (which may be inaccurate). When adding an element (key-value), the hash value of the element key is first calculated to determine the insertion position in the array. However, elements with the same hash value may have already been placed in the same position in the array.

The underlying principle of HashMap mainly involves the hash table structure and the implementation mechanism of put () and get () methods. The details are as follows: Hashtable structure data structure characteristics: HashMap combines the advantages of arrays and linked lists, and adopts array + linked lists + red-black tree structure to achieve efficient data operations. Array: Used to store elements and quickly locate element positions through hash values. The initial length is 16 (implemented by 14 in the source code).
The underlying principle of hashmap is that HashMap is based on the hashing principle and stores and obtains objects through put and get methods. When a key-value pair is passed to the put method, it calls the key object's hashCode method to calculate the hashcode, and then finds the bucket location to store the value object. When getting an object, find the correct key-value pair through the equals method of the key object, and then return the value object.
发表评论