package itcast07;
import java.util.HashMap;
import java.util.Hashtable;
/**
* 1. Hashtable 和HashMap的区别
* Hashtable: 线程安全,效率低。不允许使用null键和null值。
* HashMap: 线程不安全,效率高。允许使用null键和null值。
*
* 2. List, Set, Map等接口是否都继承自Map接口?
* List, Set不是继承自Map接口,它们继承自Collection接口
* Map接口本身就是一个顶层接口
*
* @author lgt
*
*/
public class HashtableDemo {
public static void main(String[] args) {
// 创建HashMap集合
HashMap<String, String> hm = new HashMap<String, String>();
// 添加元素
hm.put("hello", "world");
hm.put(null, null);
// 输出集合
System.out.println(hm);
// 创建Hashtable集合
Hashtable<String, String> ht = new Hashtable<String, String>();
// 添加元素
ht.put("java", "eclipse");
ht.put("2017", "0420");
// 出现错误
// ht.put(null, null);
// 输出集合
System.out.println(ht);
}
}
java中Hashtable的使用案例及与HashMap的区别
最新推荐文章于 2024-07-27 16:03:12 发布