请教高手Hashtable、HashMap、HashSet的用法与区别?

来源:学生作业帮助网 编辑:作业帮 时间:2024/04/27 21:06:01

请教高手Hashtable、HashMap、HashSet的用法与区别?
请教高手Hashtable、HashMap、HashSet的用法与区别?

请教高手Hashtable、HashMap、HashSet的用法与区别?
他们的区别:HashTable和HashMap是Map接口的实现类,其中HashTable的键或值不能为null,HashMap的键或值可以为null;HashTable是线程安全的,HashMap是线程不安全的,因此HashMap的效率要比HashTable高.HashSet是Set接口的实现类,它里面的元素都是无序的.
HashMap,HashTable
用法:
Map map=newHashMap();或者HashMap map=newHashMap();
map.put("A","a");---->设置键A的值为a
map.put("B","b");---->设置键B的值为b
取值:
for(Object obj :map.keySet()){
System.out.print(obj+":"+map.get(obj)+" ");
}
输出结果:A:a B:b
HashTable的用法与HashMap一样
以上只是他们最简单最基础的用法,一般都会给他们加上泛型,如果想深入了解可以找个帮助文档,或者找我也行