HashMap和HashTable的区别

HashMap中的方法都属于异步操作(非线程安全),HashMap允许保存有null数据;

HashTable中的方法都属于同步方法(线程安全),HashTable不允许保存null数据,否则会出现NullPointerException异常。

package com.iterator.demo;

import java.util.Hashtable;
import java.util.Map;

public class IteratorDemo {
    public static void main(String[] args) {
        Map<String, Integer> map = new Hashtable<String, Integer>();
        map.put("", 1);
        map.put("two", 2);
        //map.put(null, 3); //null
        System.out.println(map);
    }
}

运行结果:

{two=2, =1}

 

点赞