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 的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;
}
看上面源码注释,即清晰明了。
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 | 是 | 否 |
/**
* 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;
}
看上面源码注释,即清晰明了。
相关文章推荐
- android源码出现的@字符代表什么意思
- Android中的Flux架构
- 强大Android图片加载的框架:Fresco
- Android搜索芽发展clientVersion1.0结束(过程和结果显示)
- linux权限问题
- vsphere client 参数
- MySQL选用可重复读之前一定要想到的事情
- Delphi 接口机制真相
- [转] Ext Grid (ExtJs)上的单击以及双击事件
- JS实现可点击展开与关闭的左侧广告代码
- mysql 创建新用户 赋予权限
- NYOJ 39 水仙花数
- CakePHP使用方法
- 鲁大师2015如何设置系统待机时间|鲁大师设置待机时间方法
- 个人生活作息规律
- ListView优化(三) 【来自知乎上的汇总,很有见地!!】
- js 中去掉字符串两边的空的方法(即java中的trim()方法)
- LOJ 1370 Bi-shoe and Phi-shoe(欧拉函数的简单应用)
- Android NDK开发学习(一)
- NYOJ 34 韩信点兵