![](/img/trans.png)
[英]Difference between Hashtable and Collections.synchronizedMap(HashMap)
[英]Difference between Hashtable and hashmap
我正在做兩個求和的問題,但我想使用哈希表而不是 hashmap。 當我將相同的數字放入哈希表和 hashmap 時。 我發現哈希表和 hashmap 中 nums 和 index 的位置不同。 並且似乎 hashmap 根據其位和哈希表存儲隨機存儲 nums。 也許差異是因為 hash function。 我不知道我是否正確。 誰可以給我解釋一下這個? 謝謝:這是 hashmap 代碼和輸出:
int[] nums = {2, 7, 11, 15,9,1,13};
int target = 9;
int[] result;
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
map.put(nums[i], i);
}
System.out.println(map);
if (map.containsKey(target)) {
System.out.println(map.get(target));
}
Output:
{1=5,2=0,7=1,9=4,11=2,13=6,15=3}
4
這是哈希表代碼和 output:
int[] nums = {2, 7, 11, 15,9,1,13};
int target = 9;
int[] result;
Hashtable<Integer, Integer> table = new Hashtable<>();
for (int i = 0; i < nums.length; i++) {
table.put(nums[i], i);}
System.out.println(table);
if (table.containsKey(target)) {
System.out.println(table.get(target));}
output:
{9=4,7=1,15=3,13=6,2=0,1=5,11=2}
4
不同之處在於兩個類中都實現了 put 方法。 以下是我對 JDK 8 的觀察。
哈希表
Hashtable 在按位 AND 的幫助下進行位掩碼,然后計算存儲索引。 FYR方法實現如下:
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;
@SuppressWarnings("unchecked")
Entry<K,V> entry = (Entry<K,V>)tab[index];
for(; entry != null ; entry = entry.next) {
if ((entry.hash == hash) && entry.key.equals(key)) {
V old = entry.value;
entry.value = value;
return old;
}
}
addEntry(hash, key, value, index);
return null;
}
注意已計算的索引
int index = (hash & 0x7FFFFFFF) % tab.length;
傳遞給 addEntry() 方法。 當存在 hash 沖突時,此方法執行雙重哈希。 FYR代碼如下:
private void addEntry(int hash, K key, V value, int index) {
modCount++;
Entry<?,?> tab[] = table;
if (count >= threshold) {
// Rehash the table if the threshold is exceeded
rehash();
tab = table;
hash = key.hashCode();
index = (hash & 0x7FFFFFFF) % tab.length;
}
// Creates the new entry.
@SuppressWarnings("unchecked")
Entry<K,V> e = (Entry<K,V>) tab[index];
tab[index] = new Entry<>(hash, key, value, e);
count++;
}
HashMap
存儲實現包含在 putVal() 方法中。 FYR代碼:
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
Node<K,V>[] tab; Node<K,V> p; int n, i;
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
Node<K,V> e; K k;
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
e = p;
else if (p instanceof TreeNode)
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
else {
for (int binCount = 0; ; ++binCount) {
if ((e = p.next) == null) {
p.next = newNode(hash, key, value, null);
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash);
break;
}
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
p = e;
}
}
if (e != null) { // existing mapping for key
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}
++modCount;
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
}
由於 Java 8 在遇到 hash 沖突時,對象被存儲在平衡二叉樹中,以通過將查找時間減少到 O(log n) 來提高性能,而不是以前的 LinkedList 存儲(Java 7 和以前的),它是 O(n)。 這與 Hashtable 處理沖突的方法明顯不同。
HashTable 和 HashMap 盡管主要基於散列存儲原理,但在實現時卻完全不同,請記住多線程/並發處理等多種場景。
Also, hashcode() method belongs to Object class & returns the object ID which you can check by putting a breakpoint in your IDE & check the value in debug mode.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.