您的位置:首页 > 其它

hashMap和hashTable的区别

2015-09-02 10:29 323 查看
public static void main(String[] args) {
HashMap<String, String> map = new HashMap<String, String>();
map.put(null, "111");
map.put("22", null);
/**
* 如果key在map中存在,map.containsKey(null)则返回true,否则返回false
* 如果key在map中存在,map.containsValue(null)则返回true,否则返回false
*/
// System.out.println(map.containsKey(null)+"---"+map.containsKey("22")+"---"+map.containsKey("333"));
// System.out.println(map.containsValue(null)+"---"+map.containsValue("111")+"---"+map.containsValue("333"));
/**
* 运行结果
* true---true---false
* true---true---false
*/
Hashtable<String, String> table = new Hashtable<String, String>();
// table.put(null, "111");
// table.put("222",null);
/**
* 运行结果
* Exception in thread "main" java.lang.NullPointerException
at java.util.Hashtable.put(Hashtable.java:399)
at com.cn.test.Test.main(Test.java:28)
*/
System.out.println("==="+table.get("111"));
/**
* 运行结果
* ===null
*/
}

区别如下:

 HashMap            Hashtable            
线程是否同步
key,value是否科委null
HashMap 的put方法如下:

/**
* Associates the specified value with the specified key in this map.
* If the map previously contained a mapping for the key, the old
* value is replaced.
*
* @param key key with which the specified value is to be associated
* @param value value to be associated with the specified key
* @return the previous value associated with <tt>key</tt>, or
* <tt>null</tt> if there was no mapping for <tt>key</tt>.
* (A <tt>null</tt> return can also indicate that the map
* previously associated <tt>null</tt> with <tt>key</tt>.)
*/
public V put(K key, V value) {
if (key == null)
return putForNullKey(value);
int hash = hash(key.hashCode());
int i = indexFor(hash, table.length);
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}

modCount++;
addEntry(hash, key, value, i);
return null;
}Hashtable  的put方法如下:
/**
* Maps the specified <code>key</code> to the specified
* <code>value</code> in this hashtable. Neither the key nor the
* value can be <code>null</code>. <p>
*
* The value can be retrieved by calling the <code>get</code> method
* with a key that is equal to the original key.
*
* @param key the hashtable key
* @param value the value
* @return the previous value of the specified key in this hashtable,
* or <code>null</code> if it did not have one
* @exception NullPointerException if the key or value is
* <code>null</code>
* @see Object#equals(Object)
* @see #get(Object)
*/
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)) {
V old = e.value;
e.value = value;
return old;
}
}

modCount++;
if (count >= threshold) {
// Rehash the table if the threshold is exceeded
rehash();

tab = table;
index = (hash & 0x7FFFFFFF) % tab.length;
}

// Creates the new entry.
Entry<K,V> e = tab[index];
tab[index] = new Entry<K,V>(hash, key, value, e);
count++;
return null;
}

看上面源码注释,即清晰明了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: