HashMap不是线程安全,HashTab是线程安全的。HashMap可以存空值null,HashTab不可。

解决方案 »

  1.   

    看看这篇文章就清楚了
    http://www.fawcette.com/china/print.aspx?TotalPage=6&ID=8
      

  2.   

    1. Hashtable is thread-safe, HashMap is not. Thread-safe means it's safe for multiple thread to operate on the Map at the same time. However, in real life, how safe it is becomes a big problem. Actually rather synchronizing the bottom Map, the actual effecient way is to synchronize the topper action. The same idea applies on Vector. Even though Vector is synchronized, the following code show how unsafe it is.
    Vector vec = ......;
    //Thread 1
    int total = vec.size();
    for (int i = 0; i < total; i++)
       System.out.println(vec.elementAt(i));
    //Thread 2
    if (!vec.isEmpty())
       vec.removeElementAt(0);
    If thread 2 comes in during the middle of the for loop, an ArrayIndexOutOfBoundsException will be thrown.
      

  3.   

    So, the developper should synchronize the for loop and if statement for real Thread safe. By doing so, the synchronized vector is no long useful. that's why ArrayList is replacing Vector and HashMap is replacing HashTable.2. Hashtable does NOT support null value or null key when HashMap does.
      

  4.   

    我来给你系统讲解一下Hashmap和Hashtable的区别,并在最后附上sun写的实现两者的源码,有助于你的理解,顺便插一句:多多给分呀!!!hashmap是java2.0的容器,hashtable是java1.0和java1.1的容器。hashmap与hashtable几乎完全一样,用hashmap可以取代hashtable,两者甚至连方法名都几乎一样,hashtable能干的事hashmap都可以,hashmap可以存储null但hashtable不行,为什么会这样,详见sun的源码。还有一件事非常重要:无论在hashmap或是hashtable中,如果key存储的是你自定义的类的对象,那么在你的这个类的实现中要重写Object类的hashcode()以及equals()这两个方法,这一点非常重要,请切记!以下是两者查询时的速度比较,请参考,需要提的是:能用hashmap就用,hashtable是老旧的容器,最好不要用。
    ————————————————————————————————
    型别        测试大小        Put        Get        Iteration
    HashMap     100(记录数)     80.7       135.7      278.5(毫秒)
    HashTable   100(记录数)     90.6       143.3      329.0(毫秒)
    ————————————————————————————————以下是sun实现的hashmap和hashtabel的源码,请仔细分析,这对理解两者的底层的技术实现,以及两者的细微差别非常有用:
      

  5.   

    文字太多,不让回复,请查阅sun有关文档,还有一点要说一下:不要结束那么快,否则这20分也有我一份