hash-4.hashtable
1.先看hashtable的源代码
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;
}
下面是hashMap的put方法
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
//hash方法
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
//putVal方法
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;
}
通过比较可以看出
(1)hashtable是线程安全的,它在方法上加了synchronized关键字,会锁定整个链表,效率低;
hashMap是非线程安全的
(2)hashtable key和value都不能为null,而hashMap key和value都可以是null
hashtable的put方法,进去就直接调用了key.hashCode(),这样如果key是null,是会抛出异常的,然后做判断,如果value是null,也抛出异常
hashMap,先把key做了判断,如果key是null则其hashcode设置为0,value是null可以存入map
通过源码可以看到,hashtable的put方法是线程安全的
hash-4.hashtable的更多相关文章
- Hash(4) hashtable,hashmap
首先,我们要知道set是利使用map是实现的,因为只要利用map中的key唯一性就行了. 1.hashmap 和hashtable的区别是什么? 我们可以背出: hashtable线程安全.hash ...
- PHP内核探索之变量(3)- hash table
在PHP中,除了zval, 另一个比较重要的数据结构非hash table莫属,例如我们最常见的数组,在底层便是hash table.除了数组,在线程安全(TSRM).GC.资源管理.Global变量 ...
- Hash链表
<?php /* +------------------------------------------------------------------------------ | dateti ...
- 【BZOJ-1090】字符串折叠 区间DP + Hash
1090: [SCOI2003]字符串折叠 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1127 Solved: 737[Submit][Stat ...
- Hash Table 的实现步骤是什么
什么是HashTable Hash Table 是计算机科学中很重要的一种数据结构,其时间复杂度为O(1),主要是通过把关键字Key 映射到数组中的一个位置来访问记录,所以速度相当快.映射函数称为 H ...
- 集合Hashtable Dictionary Hashset
#region Dictionary<K,V> Dictionary<string, Person> dict = new Dictionary<string, Pers ...
- HashTable Dictionary HashMap
HashTable和HashMap 脑海中一直存在两个Hash,一个是HashMap另一个是HashTable,今天来总结一下两者的区别 相同点:表示根据键的哈希代码进行组织的键/值对的集合. 区别: ...
- Hashtable与HashMap区别(2)
提到hashtable,先要澄清两个问题hashCode与equals().Hashtable有容量和加载因子,容量相当于桶,因子相当于桶里的对象.而hashCode我们可以把它理解为桶的序号,所以H ...
- C# Hashtable中存入数组、List
哈希表中存入数组示例代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; ...
- JavaScript 散列表(HashTable)
TypeScript方式实现源码 // 特性: // 散列算法的作用是尽可能快地在数据结构中找到一个值. 在之前的章节中, 你已经知道如果 // 要在数据结构中获得一个值(使用get方法) ,需要遍历 ...
随机推荐
- iOS - Availability.h
>for 'dispatch' application inner to begin note `#include <Availability.h>` These macros ar ...
- 优化DP的奇淫技巧
DP是搞OI不可不学的算法.一些丧心病狂的出题人不满足于裸的DP,一定要加上优化才能A掉. 故下面记录一些优化DP的奇淫技巧. OJ 1326 裸的状态方程很好推. f[i]=max(f[j]+sum ...
- Nginx系列3之Nginx+tomcat
preface 公司部分应用跑得的tomcat,众所周知,tcomat高并发性能很弱,所以在处理静态请求的时候,我们就把他抛给Nginx处理,而Tomcat专门处理动态请求.所以在这里说说Nginx+ ...
- 【原】ajaxupload.js上传报错处理方法
相信大家在工作中经常用到文件上传的操作,因为我是搞前端的,所以这里主要是介绍ajax在前端中的操作.代码我省略的比较多,直接拿js那里的 $.ajaxFileUpload({ url:'www.cod ...
- Maven中的dependencyManagement 意义
1.在Maven中dependencyManagement的作用其实相当于一个对所依赖jar包进行版本管理的管理器. 2.pom.xml文件中,jar的版本判断的两种途径 1:如果dependenci ...
- 适可而止:YAGNI原则
适可而止:You Ain't Gonna Need It YAGNI原则指的是只需要将应用程序必需的功能包含进来,而不要试图添加任何其他你认为可能需要的功能. 在一个软件项目中,往往80%的时间花费在 ...
- Github for Windows使用介绍
Git已经变得非常流行,连Codeplex现在也已经主推Git.Github上更是充斥着各种高质量的开源项目,比如ruby on rails,cocos2d等等.对于习惯Windows图形界面的程序员 ...
- Unity Particle System Sorting Order
http://answers.unity3d.com/questions/577288/particle-system-rendering-behind-sprites.html
- array_filter移除空数组
$arr = array( "0"=> "564645", "1"=>"", "2"=& ...
- HTML \ XHTML \XML 的区别
虽然是很简单的知识,但如果总是在需要的时候去查找,不需要的时候就丢掉,未免心里总是觉的不踏实.因为你就像是垃圾收购站,有垃圾(知识)就往里面拖,拖不下了就丢掉一些(忘了).不去整理,也因此也不知道丢的 ...