Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[collections-dev] Question about ConcurrentHashMap

Hi,

I am using EC ConcurrentHashMap. I need a method that does below two operations atomically:
1. returns value of a key
2. if the key is not in the dictionary (i.e., step 1 returns null) it should put a KV pair in the map.

I am trying getIfAbsentPut but it does not return the original value in the map. I want this test to pass:

ConcurrentHashMap<String, String> map = new ConcurrentHashMap<>();
map.put("foo", "bar");
var x = map.getIfAbsentPut("spam", "eggs");
assertTrue(x == null);

is there any method that can do this for me?

S.

PS: i am looking for a behavior somewhat equivalent to AtomicInger getAndIncrement. the get returns the original value not the value after the operation completes.

Back to the top