package com.cavaness.quartzbook.chapter3;
import java.util.HashMap;
import java.util.Hashtable;
public class Test {
public static void main(String[] args) {
String string = null;
HashMap<String, Integer> hash = new HashMap<String, Integer>();
hash.put("", 1);
hash.put(string, 2); // 编译和运行都成长
Hashtable<String, Integer> hashtable = new Hashtable<String, Integer>();
hashtable.put("", 1);
hashtable.put(string, 2); // 编译通过,运行异常
/*
就HashMap与HashTable主要从三方面来说。
一.历史原因:Hashtable是基于陈旧的Dictionary类的,HashMap是Java 1.2引进的Map接口的一个实现
二.同步性:Hashtable是线程安全的,也就是说是同步的,而HashMap是线程序不安全的,不是同步的
三.值:只有HashMap可以让你将空值作为一个表的条目的key或value,HashTable不允许
*/
}
}
HashMap和Hashtable
最新推荐文章于 2025-05-25 14:15:27 发布