这个问题是由奇怪的HashMap.put()行为引起的。
我认为我理解了为什么Map<K,V>.put
需要一个K
,而Map<K,V>.get
需要一个Object
,因为不这样做会破坏太多现有的代码。
现在我们进入了一个非常容易出错的情况:
java.util.HashMap<Long, String> m = new java.util.HashMap<Long, String>();
m.put(5L,"Five"); // compiler barfs on m.put(5, "Five")
m.contains(5); // no complains from compiler, but returns false
如果长整型 Long
的值在 int
范围内且值相等,那么返回 true
不就行了吗?