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>
B is the same as E. I guess there is an error in A. Should be put instead of out.
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
ConcurrentMap is an interface, you can’t use “new ConcurrentMap();” If you ignore it, then B and E are correct
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.
+1
Great
BE