JDK 8 on mac OS, looking at following code from HashMap.java:
public Set<K> keySet() {
Set<K> ks = keySet;
if (ks == null) {
ks = new KeySet();
keySet = ks;
}
return ks;
}
Any changes to the returned ks will reflect in keySet as they always point to the same underlying set, if this is true, can it be written as:
public Set<K> keySet() {
if (keySet == null) {
keySet = new KeySet();
}
return keySet;
}
Are the two code snippets behave equivalent?
If so, why HashMap uses the first variation rather than the second variation?