I was just going through the java.util.HashMap values implementation and I found the values() implementation in this way
public Collection<V> values() {
Collection<V> vs = values;
if (vs == null) {
vs = new Values();
values = vs;
}
return vs;
}
Why is an extra vs variable needed?
Why don’t it’s just
public Collection<V> values() {
if (values == null) {
values = new Values();
}
return values;
}
Is there any particular reason ? If anyone know the reason behind it please share it?