Java: how to make a private field Map immutable within a class?
问题 public class Hi { private final Map<String, String> map; public Map<String, String> getMap() { return map; } } I have this Hi class, and I want map to be immutable. I also need a getter. Currently, another class can modify the map from the getter. I would like to return a copy of the map to solve this problem, but Map is an interface, so does that mean I have to make the getter call: return new HashMap<String,String>(map); Is there another way to do it without forcing the map to be a hashmap?