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方法) ,需要遍历 ...
随机推荐
- Django TemplateSyntaxError Could not parse the remainder: '()'
返回的数据是列表集合,如 n [5]: a = set() In [6]: a.add((1, 3)) In [7]: a Out[7]: {(1, 3)} 在模板中使用方式如下: {% for ar ...
- HTML之:让网页中的<a>标签属性统一设置-如‘新窗口打开’
在开发过程中,我们往往想在页面中,给<a>设置一个统一的默认格式,例如我们想让链接:“在新窗口打开”,我们就可以使用<base>标签 在网页中添加这段代码: <head& ...
- Beta Daily Scrum 第三天
[目录] 1.任务进度 2.困难及解决 3.燃尽图 4.代码check-in 5.总结 1. 任务进度 学号 今日完成 明日完成 612 初步完成成就界面的统计图表 继续编写成就界面的图表 615 白 ...
- POJ3114 Countries in War (强连通分量 + 缩点 + 最短路径 + 好题)
题目链接 题意是说在几个邮局之间传送一份信件,如果出发点和终止点在同一个国家传递,则时间为0,否则让你求花费最少时间,如果不能传到,则输出Nao e possivel entregar a carta ...
- 半透明状态栏(适用于搜索等)CSS样式
.search-{ -webkit-box-flex: ; -moz-box-flex:; text-align: left; font-size:14px; line-height:22px; he ...
- Win8.1微软官方最终正式版ISO镜像文件
Win8.1微软官方最终正式版ISO镜像文件 经过预览版,测试版.开发版本等几个乱七八糟的版本后,2013年10月17日,微软终于如约的发布了Win8.1最终正式版. Win8.1和win8的区别 1 ...
- nltk.download()出错解决
http://blog.csdn.net/joey_su/article/details/17289621 官方下载地址 http://www.nltk.org/nltk_data/ 把python自 ...
- XtraFinder在OSX10.11的使用
重启系统,按住command键加上R键 进入恢复模式还是什么模式里,然后启动terminal 然后键入 csrutil enable --without debug 重启电脑,可正常使用 居然上传不了 ...
- VisualStudio.gitignore git 忽略
https://github.com/kaedei/gitignore/blob/master/VisualStudio.gitignore
- NopCommerce源码分析ContainerBuilder builder.Update(container)
/// <summary> /// Register dependencies /// </summary> /// <param name="config&q ...