Skip to main content

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

try putIfAbsent

Thanks

Moh

On 9/22/23 13:23, Siddharth Jain via collections-dev wrote:
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.

_______________________________________________
collections-dev mailing list
collections-dev@xxxxxxxxxxx
To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/collections-dev

Back to the top