To expand slightly on Michael's answer, I expect it is there to ensure that the keySet()
method never returns null
, possibly in addition to providing performance benefits noted.
Given this code:
public Set<K> keySet() {
return (keySet == null ? (keySet = new KeySet()) : keySet;
}
It would be at least theoretically possible in multi-threaded code that the keySet
field could be set to null
between the first read (keySet == null
) and the second read, where it is returned. I haven't looked at the rest of the code, but I assume there are other places where keySet
is potentially assigned null
. Whether this is as a result of an issue seen in the wild, or a defensive measure would be a question for the authors.
The actual code:
public Set<K> keySet() {
Set<K> ks;
return (ks = keySet) == null ? (keySet = new KeySet()) : ks;
}
...doesn't have this problem as the field is only read once.