Java – ConcurrentHashMap update for multiple entries

ConcurrentHashMap update for multiple entries… here is a solution to the problem.

ConcurrentHashMap update for multiple entries

I

have a situation where in ConcurrentHashMap I store some properties like ip address and port or penalty and code – 2 or more entries connected to each other.

Now I want to update these 2 entries automatically to avoid reading incorrect pairs.

So now I’m thinking about it and don’t know the easy solution.

I can wrap the get/put method with the ReadWriteLock block, but it doesn’t feel right:) I didn’t use ConcurrentHashMap to write extra locks.

Another option is to combine these properties into one. Now I’m leaning towards that option.

The other option is to have a version in the value object, but I need to check it every time and I don’t want to do that 🙂

Are there other solutions to this problem?

Greetings

Solution

You cannot automatically update multiple entries. You have two options:

  • Use a simple HashMap with external synchronization.

  • Use different types for Map values, which must be considered together for items.

Related Problems and Solutions