I'm returning an unmodifiable map from a method. How do I make sure that any code that tries to modify the returned map will get a compile time error, as opposed to a run time error?
My class:
public class Foo
{
private final Map<String, Bar> aMap;
public Foo()
{
...
}
public Map<String, Bar> getMap(){
return Collections.unmodifiableMap(aMap);
}
}
I want this to create a compile time error:
Foo foo = new Foo();
Map<String, Bar> m = foo.getMap();
m.put('key',null); // user should be notified that the map is unmodifiable here, not at run time
Can I change the return type? Can I add an appropriate annotation?