`
xubindehao
  • 浏览: 240110 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

hashmap和hashtable

阅读更多

import java.util.HashMap;
import java.util.Hashtable;

/**
 *
 */

/**
 * @author Administrator
 *
 */
public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        HashMap hashMap = new HashMap();
        hashMap.put(null, new Integer(1));
        hashMap.put(null, new Integer(2));
        hashMap.put(null, new Integer(3));

        System.out.println(hashMap.get(null));

        System.out.println(hashMap.get("Null"));

        System.out.println(hashMap.get("NullThere"));

        System.out.println(hashMap.containsKey(null));
       
        Hashtable hashTable = new Hashtable();
        hashTable.put(null, 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();

 

 

可见HashTable 的 key和value都不能为空的

 

尽信书不如无书,我今天在看网上的一些资料的时候发现一遍篇《HashMap和Hashtable的区别》的文章,随手就在Eclipse里实验了一下,结果发现很多原来文章中的错误,现在把这个问题修改好以后贴在这里,希望对大家的学习也有帮助。

HashMap Hashtable 的区别。

错误说法:

HashTable 不允许 null (key value 都不可以 ),HashMap 允许 null (key value 都可以 )
这句话容易让人误会,到底是怎么个不允许法呢?其实在编译期不会有任何的不一样,会照样执行,只是在运行期的时候 Hashtable 中设置的话回出现空指针异常

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

不用多说,看下面的程序就可以:

HashMap map = new HashMap();

       map.put( "Null" , null );

       map.put( null , "Null" );

       map.put( null , "Empty" );

       System. out .println(map.get( null ));

       System. out .println(map.get( "Null" ));

       System. out .println(map.get( "NullThere" ));

       System. out .println(map.containsKey ( "Null" ));

System. out .println(map.containsKey ( "NullThere" ));

 

 

输出结果为:

Empty

null

null

true

false

 

 

 

HashMap

Hashtable

继承,实现

HashMap <K,V>    extends AbstractMap<K,V>    implements Map<K,V>, Cloneable, Serializable

Hashtable <K,V>

    extends Dictionary<K,V>

    implements Map<K,V>, Cloneable,Serializable

多线程,同步

未同步的,可以使用 Colletcions 进行同步

Map Collections.synchronizedMap(Map m)

已经同步过的可以安全使用

null 的处理

HashMap map = new HashMap();

map.put( null , "Null" );

map.put( "Null" , null );

map.containsKey( null );

map.containsValue( null );

以上这 5 条语句无论在编译期,还是在运行期都是没有错误的 .

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

Hashtable table = new Hashtable();

table.put(null, "Null");

table.put("Null", null);

table.contains(null);

table.containsKey(null);

table.containsValue(null);

后面的 5 句话在编译的时候不会有异常,可在运行的时候会报空指针异常

具体原因可以查看源代码

public synchronized V put(K key, V value) {

     // Make sure the value is not null

     if (value == null) {

         throw new NullPointerException();

     }

………….

增长率

void addEntry( int hash, K key, V value, int bucketIndex) {

      Entry<K,V> e = table [bucketIndex];

        table [bucketIndex] = new Entry<K,V>(hash, key, value, e);

        if ( size ++ >= threshold )

            resize (2 * table . length );

    }

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics