map.get(key)时,key最好是只读的对象。
因为 Object.hashCode()的JDK文档提到过:the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. 下面是例证:
List list = new ArrayList();
Map map = new HashMap();
map.put(list, "aaa");
System.out.println(map.get(list)); //aaa
list.add("bbb");
System.out.println(map.get(list)); //null, 如此不妥,因为hashcode变了。
Object.hashCode() 文档表明:hashCode()方法的本意是为hashtable算法服务。