on-internet
← Community

How to do ConcurrentHashMap.computeIfAbsent without assigning the entry to hashmap?

Stack Overflow ·

Is there a way to simulate ConcurrentHashmap.computeIfAbsent but not assign the entry to the hashmap. I just need the instance that the method generated when the entry in the hashmap is not yet there. These are running in Thread. So I need it to be thread-safe. I have tried with sychronized block, but this will hold the entire hash. I want it to be like the computeIfAbsent. synchronized(hashMap) { if (hashMap.contains(key)) { rs = hashMap.get(key); } else { rs = createNewInstance(); } } /* this would be perfect, but I don't what the new instance to be in the hashMap */ rs = hashMap.computeIfAb

Comments

Sign in or sign up to comment and upvote.

No comments yet.