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

Given:
<code>
ConcurrentMap<String, String> partList = new ConcurrentHashMap<> ();
</code>
Which fragment puts a key/value pair in partList without the possibility of overwriting an existing key?

Given:

ConcurrentMap<String, String> partList = new ConcurrentHashMap<> ();

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

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

B.
partList.putAbsent(key,”Blu Shirt”)

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

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

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

Explanation:
The containsKey method returns true if this map maps one or more keys to the
specified value.
So this statement adds a new key if they key is not present.
Reference: Class ConcurrentHashMap<K,V>



Leave a Reply 1

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


zzZZzzzzZZzzz

zzZZzzzzZZzzz

B if putIfAbsent(key, “Blue Shirt”)