关于Hashtable类的hashcode
有个问题不是很明白,书上说当比较Hashtable对象的key是否相同时必须覆盖equals()方法和hashCode()方法,我觉的光覆盖eauals方法就可以了啊。因为只要两个对象的内容相等,我们就能判断是相同的键值了啊。
public synchronized V put(K key, V value) { // Make sure the value is not null if (value == null) { throw new NullPointerException(); } // Makes sure the key is not already in the hashtable. Entry tab[] = table; int hash = key.hashCode(); int index = (hash & 0x7FFFFFFF) % tab.length; for (Entry<K,V> e = tab[index] ; e != null ; e = e.next) { if ((e.hash == hash) && e.key.equals(key)) { //这里写着当hash值相等并且equals相等时才会被认为是同一个key,会覆盖原有的value V old = e.value; e.value = value; return old; } }