Which fragment puts a key/value pair in partList without the responsibility of overwriting an existing key?

Given:

ConcurrentMap <String, String> PartList = new ConcurrentMap<>();

Which fragment puts a key/value pair in partList without the responsibility of overwriting an existing key?

Given:

ConcurrentMap <String, String> PartList = new ConcurrentMap<>();

Which fragment puts a key/value pair in partList without the responsibility of overwriting an existing key?

A.
partList.out(key,”Blue Shirt”);

B.
partList.putIfAbsent(key,”Blue Shirt”);

C.
partList.putIfNotLocked (key,”Blue Shirt”);

D.
partList.putAtomic(key,”Blue Shirt”)

E.
if (!partList.containsKey(key)) partList.put (key,”Blue Shirt”);

Explanation:
putIfAbsent(K key, V value)
If the specified key is not already associated with a value, associate it with the given value.

Reference:java.util.concurrent,Interface ConcurrentMap<K,V>



Leave a Reply 7

Your email address will not be published. Required fields are marked *


Marian

Marian

B is the same as E. I guess there is an error in A. Should be put instead of out.

Jav

Jav

A overwrites if there is an existent key
B does not overwrites if there is an existent key
E also does not overwrites if there is an existent key
C and D are bullshit

mato

mato

ConcurrentMap is an interface, you can’t use “new ConcurrentMap();” If you ignore it, then B and E are correct

Tim

Tim

B and E the same, except that for B the action is performed atomically. So to “put a key/value pair in partList without the responsibility of overwriting an existing key”, B would be a better choice.