`
fxly0401
  • 浏览: 144340 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

HashMap与HashTable的区别

阅读更多
1.hashMap去掉了HashTable 的contains方法,但是加上了containsValue()和containsKey()方法。
2.hashTable同步的,而HashMap是非同步的,可以使用Colletcions进行同步
Map Collections.synchronizedMap(Map m);效率上HashMap比Hashtable要高。
3.hashMap允许空键值,而hashTable不允许。
4.HashTable不允许null值(key和value都不可以),HashMap允许null值(key和value都可以)。
其实在编译期不会有任何的不一样,会照样执行,只是在运行期的时候Hashtable中设置的话回出现空指针异常
5.在HashMap中,可以有一个或多个键所对应的值为null。当get()方法返回null值时,即可以表示 HashMap中没有该键,也可以表示该键所对应的值为null。因此,在HashMap中不能由get()方法来判断HashMap中是否存在某个键, 而应该用containsKey()方法来判断。

Hashtable代码如下
        Hashtable h=new Hashtable();
        h.put("用户1",new Integer(90));
	h.put("用户2",new Integer(50));
	h.put("用户3",new Integer(60));
	h.put("用户4",new Integer(70));
	h.put("用户5",new Integer(80));
//	h.put("Null",null);
//	h.put(null,"Null");
//	h.put(null,"Empty");
        Enumeration e=h.elements();
	while(e.hasMoreElements()){
		System.out.println(e.nextElement());
	}
	System.out.println("****"+h.containsKey(new String("用户8")));
	System.out.println("****"+h.containsValue(new Integer(60)));

结果输出
80
70
60
50
90
****false
****true
注:因为Hashtable不允许null键和null值,所以如果去掉代码中的任意一行注释,会出现异常:java.lang.NullPointerException
-------------------------------------------------------------------------
HashMap代码如下:
        HashMap h = new HashMap();
	h.put("用户1",new Integer(90));
	h.put("用户2",new Integer(50));
	h.put("用户3",new Integer(60));
	h.put("用户4",new Integer(70));
	h.put("用户5",new Integer(80));
	h.put("Null",null);
	h.put(null,"Null");////////////////
	h.put(null,"Empty");
        System.out.println("****"+h.containsKey(new String("用户8")));
	System.out.println("****"+h.containsValue(new Integer(60)));
	Iterator e=h.entrySet().iterator();
	while(e.hasNext()){
		System.out.println(e.next());
	}

输入结果如下:
****false
****true
null=Empty
用户4=70
用户3=60
用户5=80
Null=null
用户2=50
用户1=90

注:因为HashMap允许null键和null值,所以不会出现异常,但是相同的键值就会出现第二次覆盖第一次的结果,所以////////////此行键值对不存在

同步HashMap代码示例:

import java.util.Collections;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

public class TableMap {

 public static void main(String... strings) {
  Map<Integer, Integer> map = Collections.synchronizedMap(new HashMap<Integer, Integer>());//为hashmap加上同步
  Hashtable<Integer, Integer> table = new Hashtable<Integer, Integer>();
  long before = System.currentTimeMillis();
  add(map);
  System.out.println("add map time=" + (System.currentTimeMillis() - before));
  before = System.currentTimeMillis();
  add(table);
  System.out.println("add table time=" + (System.currentTimeMillis() - before));

  before = System.currentTimeMillis();
  get(map);
  System.out.println("get map time=" + (System.currentTimeMillis() - before));

  before = System.currentTimeMillis();
  get(table);
  System.out.println("get table time=" + (System.currentTimeMillis() - before));
 }

 public static void add(Map<Integer, Integer> map) {
  for (int i = 0; i < 300000; i++) {
   map.put(i, i);
  }
 }

 public static void get(Map<Integer, Integer> map) {
  for (int i = 0; i < 300000; i++) {
   map.get(i);
  }
 }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics