zoukankan      html  css  js  c++  java
  • hashmap与hashtable的区别,以及实现hashmap的同步操

    Hashtable和HashMap的区别
    1.Hashtable是Dictionary的子类,HashMap是Map接口的一个实现类;

    2.Hashtable中的方法是同步的,而HashMap中的方法在缺省情况下是非同步的。即是说,在多线程应用程序中,不用专门的操作就安全地可以使用Hashtable了;而对于HashMap,则需要额外的同步机制。但HashMap的同步问题可通过Collections的一个静态方法得到解决:
    Map Collections.synchronizedMap(Map m)
    这个方法返回一个同步的Map,这个Map封装了底层的HashMap的所有方法,使得底层的HashMap即使是在多线程的环境中也是安全的。

    3.在HashMap中,null可以作为键,这样的键只有一个;可以有一个或多个键所对应的值为null。当get()方法返回null值时,即可以表示HashMap中没有该键,也可以表示该键所对应的值为null。因此,在HashMap中不能由get()方法来判断HashMap中是否存在某个键,而应该用containsKey()方法来判断。

    4.其底层的实现机制不同,hashmap的访问速度要快于hashtable,因为它不需要进行同步检验,建议在非多线程环境中使用hashmap代替hashtable .

    HashMap与Hashtable的区别
    HashTable的应用非常广泛,HashMap是新框架中用来代替HashTable的类,也就是说建议使用HashMap,不要使用HashTable。可能你觉得HashTable很好用,为什么不用呢?这里简单分析他们的区别。
    1.HashTable的方法是同步的,HashMap未经同步,所以在多线程场合要手动同步HashMap这个区别就像Vector和ArrayList一样。

    2.HashTable不允许null值(key和value都不可以),HashMap允许null值(key和value都可以)。

    3.HashTable有一个contains(Object value),功能和containsValue(Object value)功能一样。

    4.HashTable使用Enumeration,HashMap使用Iterator。

    以上只是表面的不同,它们的实现也有很大的不同。

    5.HashTable中hash数组默认大小是11,增加的方式是 old*2+1。HashMap中hash数组的默认大小是16,而且一定是2的指数。

    6.哈希值的使用不同,HashTable直接使用对象的hashCode,代码是这样的:
    1. int hash = key.hashCode();
    2.  int index = (hash & 0x7FFFFFFF) % tab.length;
    3.  而HashMap重新计算hash值,而且用与代替求模:
    4.  int hash = hash(k);
    5.  int i = indexFor(hash, table.length);

    6. static int hash(Object x) {
    7.    int h = x.hashCode();

    8.   h += ~(h < < 9);
    9.    h ^= (h >>> 14);
    10.    h += (h < < 4);
    11.    h ^= (h >>> 10);
    12.    return h;
    13.  }
    14.  static int indexFor(int h, int length) {
    15.    return h & (length-1);
    16.  }

    以上只是一些比较突出的区别,当然他们的实现上还是有很多不同的,比如
     HashMap对null的操作

    例子

    1. package testMapAndTable;

    2. import java.util.Collections;
    3. import java.util.HashMap;
    4. import java.util.Hashtable;
    5. import java.util.Map;

    6. public class TableMap {

    7.     public static void main(String... strings) {
    8.         Map<Integer, Integer> map = Collections
    9.                 .synchronizedMap(new HashMap<Integer, Integer>());// 为hashmap加上同步

    10.         Hashtable<Integer, Integer> table = new Hashtable<Integer, Integer>();
    11.         long before = System.currentTimeMillis();
    12.         add(map);
    13.         System.out.println("add map time="
    14.                 + (System.currentTimeMillis() - before));
    15.         before = System.currentTimeMillis();
    16.         add(table);
    17.         System.out.println("add table time="
    18.                 + (System.currentTimeMillis() - before));

    19.         before = System.currentTimeMillis();
    20.         get(map);
    21.         System.out.println("get map time="
    22.                 + (System.currentTimeMillis() - before));

    23.         before = System.currentTimeMillis();
    24.         get(table);
    25.         System.out.println("get table time="
    26.                 + (System.currentTimeMillis() - before));
    27.     }

    28.     public static void add(Map<Integer, Integer> map) {
    29.         for (int i = 0; i < 300000; i++) {
    30.             map.put(i, i);
    31.         }
    32.     }

    33.     public static void get(Map<Integer, Integer> map) {
    34.         for (int i = 0; i < 300000; i++) {
    35.             map.get(i);
    36.         }
    37.     }
    38. }
    阅读(801) | 评论(0) | 转发(1) |
    给主人留下些什么吧!~~
    评论热议
  • 相关阅读:
    如何让你的Sublime和Codeblocks支持C++11
    Python print不换行输出的替代方法
    阶梯博弈
    hdu4633_Polya定理
    Ural_1169_Pairs
    ACM竞赛中的魔方问题专题(不定时更新)
    LintCode 35. 翻转链表
    windows中mysql5.7中配置中文字符集和默认datadir
    CentOS7使用打开关闭防火墙与端口
    关于阿里巴巴开发手册"不得使用外键与级联,一切外键概念必须在应用层解决"的疑惑
  • 原文地址:https://www.cnblogs.com/black/p/5171891.html
Copyright © 2011-2022 走看看