I want to:
- Add an entry to a ConcurrentHashMap, if there is no entry for the key, or
- Replace the value for the key, only if the current value is smaller.
I came up with the following code, but sine it has while (true), it looks scary to me :)
I wanted to check it with you guys. Do you think it is working?
// Input: map, k, t
while (true) {
if (map.containsKey(k)) {
current = map.get(k);
if (current != null && current < t) {
if (map.replace(k, current, t))
break;
} else if (current != null && current >= t) {
break;
}
} else {
pre = map.putIfAbsent(k, t);
if (pre == null)
break;
}
}