1.先看hashtable的源代码

  1. public synchronized V put(K key, V value) {
  2. // Make sure the value is not null
  3. if (value == null) {
  4. throw new NullPointerException();
  5. }
  6.  
  7. // Makes sure the key is not already in the hashtable.
  8. Entry<?,?> tab[] = table;
  9. int hash = key.hashCode();
  10. int index = (hash & 0x7FFFFFFF) % tab.length;
  11. @SuppressWarnings("unchecked")
  12. Entry<K,V> entry = (Entry<K,V>)tab[index];
  13. for(; entry != null ; entry = entry.next) {
  14. if ((entry.hash == hash) && entry.key.equals(key)) {
  15. V old = entry.value;
  16. entry.value = value;
  17. return old;
  18. }
  19. }
  20.  
  21. addEntry(hash, key, value, index);
  22. return null;
  23. }

下面是hashMap的put方法

  1. public V put(K key, V value) {
  2. return putVal(hash(key), key, value, false, true);
  3. }
  4.  
  5. //hash方法
  6. static final int hash(Object key) {
  7. int h;
  8. return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
  9. }
  10. //putVal方法
  11. final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
  12. boolean evict) {
  13. Node<K,V>[] tab; Node<K,V> p; int n, i;
  14. if ((tab = table) == null || (n = tab.length) == 0)
  15. n = (tab = resize()).length;
  16. if ((p = tab[i = (n - 1) & hash]) == null)
  17. tab[i] = newNode(hash, key, value, null);
  18. else {
  19. Node<K,V> e; K k;
  20. if (p.hash == hash &&
  21. ((k = p.key) == key || (key != null && key.equals(k))))
  22. e = p;
  23. else if (p instanceof TreeNode)
  24. e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
  25. else {
  26. for (int binCount = 0; ; ++binCount) {
  27. if ((e = p.next) == null) {
  28. p.next = newNode(hash, key, value, null);
  29. if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
  30. treeifyBin(tab, hash);
  31. break;
  32. }
  33. if (e.hash == hash &&
  34. ((k = e.key) == key || (key != null && key.equals(k))))
  35. break;
  36. p = e;
  37. }
  38. }
  39. if (e != null) { // existing mapping for key
  40. V oldValue = e.value;
  41. if (!onlyIfAbsent || oldValue == null)
  42. e.value = value;
  43. afterNodeAccess(e);
  44. return oldValue;
  45. }
  46. }
  47. ++modCount;
  48. if (++size > threshold)
  49. resize();
  50. afterNodeInsertion(evict);
  51. return null;
  52. }

通过比较可以看出

(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的更多相关文章

  1. Hash(4) hashtable,hashmap

    首先,我们要知道set是利使用map是实现的,因为只要利用map中的key唯一性就行了. 1.hashmap 和hashtable的区别是什么? 我们可以背出:  hashtable线程安全.hash ...

  2. PHP内核探索之变量(3)- hash table

    在PHP中,除了zval, 另一个比较重要的数据结构非hash table莫属,例如我们最常见的数组,在底层便是hash table.除了数组,在线程安全(TSRM).GC.资源管理.Global变量 ...

  3. Hash链表

    <?php /* +------------------------------------------------------------------------------ | dateti ...

  4. 【BZOJ-1090】字符串折叠 区间DP + Hash

    1090: [SCOI2003]字符串折叠 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1127  Solved: 737[Submit][Stat ...

  5. Hash Table 的实现步骤是什么

    什么是HashTable Hash Table 是计算机科学中很重要的一种数据结构,其时间复杂度为O(1),主要是通过把关键字Key 映射到数组中的一个位置来访问记录,所以速度相当快.映射函数称为 H ...

  6. 集合Hashtable Dictionary Hashset

    #region Dictionary<K,V> Dictionary<string, Person> dict = new Dictionary<string, Pers ...

  7. HashTable Dictionary HashMap

    HashTable和HashMap 脑海中一直存在两个Hash,一个是HashMap另一个是HashTable,今天来总结一下两者的区别 相同点:表示根据键的哈希代码进行组织的键/值对的集合. 区别: ...

  8. Hashtable与HashMap区别(2)

    提到hashtable,先要澄清两个问题hashCode与equals().Hashtable有容量和加载因子,容量相当于桶,因子相当于桶里的对象.而hashCode我们可以把它理解为桶的序号,所以H ...

  9. C# Hashtable中存入数组、List

    哈希表中存入数组示例代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; ...

  10. JavaScript 散列表(HashTable)

    TypeScript方式实现源码 // 特性: // 散列算法的作用是尽可能快地在数据结构中找到一个值. 在之前的章节中, 你已经知道如果 // 要在数据结构中获得一个值(使用get方法) ,需要遍历 ...

随机推荐

  1. 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 ...

  2. HTML之:让网页中的<a>标签属性统一设置-如‘新窗口打开’

    在开发过程中,我们往往想在页面中,给<a>设置一个统一的默认格式,例如我们想让链接:“在新窗口打开”,我们就可以使用<base>标签 在网页中添加这段代码: <head& ...

  3. Beta Daily Scrum 第三天

    [目录] 1.任务进度 2.困难及解决 3.燃尽图 4.代码check-in 5.总结 1. 任务进度 学号 今日完成 明日完成 612 初步完成成就界面的统计图表 继续编写成就界面的图表 615 白 ...

  4. POJ3114 Countries in War (强连通分量 + 缩点 + 最短路径 + 好题)

    题目链接 题意是说在几个邮局之间传送一份信件,如果出发点和终止点在同一个国家传递,则时间为0,否则让你求花费最少时间,如果不能传到,则输出Nao e possivel entregar a carta ...

  5. 半透明状态栏(适用于搜索等)CSS样式

    .search-{ -webkit-box-flex: ; -moz-box-flex:; text-align: left; font-size:14px; line-height:22px; he ...

  6. Win8.1微软官方最终正式版ISO镜像文件

    Win8.1微软官方最终正式版ISO镜像文件 经过预览版,测试版.开发版本等几个乱七八糟的版本后,2013年10月17日,微软终于如约的发布了Win8.1最终正式版. Win8.1和win8的区别 1 ...

  7. nltk.download()出错解决

    http://blog.csdn.net/joey_su/article/details/17289621 官方下载地址 http://www.nltk.org/nltk_data/ 把python自 ...

  8. XtraFinder在OSX10.11的使用

    重启系统,按住command键加上R键 进入恢复模式还是什么模式里,然后启动terminal 然后键入 csrutil enable --without debug 重启电脑,可正常使用 居然上传不了 ...

  9. VisualStudio.gitignore git 忽略

    https://github.com/kaedei/gitignore/blob/master/VisualStudio.gitignore

  10. NopCommerce源码分析ContainerBuilder builder.Update(container)

    /// <summary> /// Register dependencies /// </summary> /// <param name="config&q ...