Can someone perhaps explain the following to me? My confusion is mainly around the else statement, in that it initialises an empty HashSet and adds this HashSet to the Map, but calling values.add(sToAdd) after the fact seems to updated the entries map anyway?
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class Entries {
private Map<Integer, Set<String>> entries = null;
Entries(){
this.entries = Collections.synchronizedMap(new TreeMap<Integer, Set<String>>());
}
final void add(String sToAdd){
Set<String> values;
if (this.entries.containsKey(1)) {
values = this.entries.get(1);
} else {
values = new HashSet<String>();
this.entries.put(1, values);
}
values.add(sToAdd);
}
public static void main(String[] args) {
Entries e = new Entries();
e.add("First entry");
System.out.println(e.entries.toString());
}
}