Design a thread-safe hash map from integer keys to integer values. Multiple threads share one ConcurrentHashMap instance and may call its methods at the same time.
Implement the following operations:
put(key, value) inserts a key-value pair or replaces the existing value.get(key) returns the current value, or -1 when the key is absent.remove(key) removes the key when it exists.increment(key, delta) atomically adds delta to the current value and returns the new value. A missing key has an initial value of 0.size() returns the number of keys in a consistent snapshot of the map.
The constructor receives stripeCount, the number of independently locked stripes. Keys assigned to different stripes should be able to proceed concurrently.
Every public operation must be thread-safe and linearizable. In particular, two concurrent calls to increment must never overwrite each other's updates.
The judge preloads the standard concurrency and collection APIs for every supported language. You do not need to add import, include, using, or package statements.
Example 1:
Input:
Output:
Explanation: Updating key 1 does not change the number of keys. Removing key 2 makes subsequent reads return -1.
Example 2:
Input:
Output:
Explanation: Each increment is one atomic read-modify-write operation, so no update is lost.
Constraints
1 <= stripeCount <= 32-1_000_000 <= key <= 1_000_0000 <= value <= 1_000_0001 <= delta <= 1000- The result of an increment fits in a signed 32-bit integer.
- The judge may call every operation concurrently.
- Values passed to
put are non-negative, so -1 can represent a missing key.