和风细雨

世上本无难事,心以为难,斯乃真难。苟不存一难之见于心,则运用之术自出。

HashMap和Hashtable的实验比较结果

面试题中常有HashMap和Hashtable的异同比较题,今天闲着无事,做了一点小比较,实验结果如下:
   HashMap  Hashtable
 允许空键  允许  不允许
 允许空值  允许  不允许
 以空键取值  能取到值  
 取空值  能取得  
 插值速度  稍高  稍低
 取值速度  高  低
 遍历速度  两者差不多 两者差不多

测试代码如下:
package com.sitinspring;

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

public class Main {
    
public static void main(String[] args) {
        Map
<String, String> hashMap = new HashMap<String, String>();
        Map
<String, String> hashTable = new Hashtable<String, String>();

        
// 测试一:往两者中放空键,hashMap允许,hashTable不允许,第二句会抛出空指针异常
        hashMap.put(null"value");
        hashTable.put(
null"value");

        
// 测试二:往两者中放空值,hashMap允许,hashTable不允许,第二句也会抛出空指针异常
        hashMap.put("key"null);
        hashTable.put(
"key"null);

        
// 测试三:以空键取hashMap中的值,结果能够取到"value"
        String value = hashMap.get(null);
        System.out.println(
"取出的值等于" + value);

        
// 测试四:以键"key"取hashMap中的值,结果能够取到null
        String value2 = hashMap.get("key");
        System.out.println(
"取出的值等于" + value2);
        
        
// 测试五:插值速度比较,两者差别不大
        int max=100000;
        
        TimeTest tableTimeTest
=new TimeTest();
        setValuesToMap(hashTable,max);
        tableTimeTest.end(
"往hashTable插"+max+"值耗时");
        
        hashMap.clear();
// 清楚掉原来加入的值        
        TimeTest mapTimeTest=new TimeTest();
        setValuesToMap(hashMap,max);
        mapTimeTest.end(
"往hashMap插"+max+"个值耗时");        
        
        
// 测试六:取值速度比较,hashTable速度平均约为hashMap的四分之一到七分之一
        TimeTest tableTimeTest2=new TimeTest();
        getValuesFromMap(hashTable,max);
        tableTimeTest2.end(
"从hashTable取"+max+"值耗时");
        
        TimeTest mapTimeTest2
=new TimeTest();
        getValuesFromMap(hashMap,max);
        mapTimeTest2.end(
"往hashMap取"+max+"个值耗时");    
        
        
// 测试七:遍历速度比较,hashTable速度和hashMap的差不多
        TimeTest tableTimeTest3=new TimeTest();
        traversalMap(hashTable);
        tableTimeTest3.end(
"遍历hashTable耗时");
        
        TimeTest mapTimeTest3
=new TimeTest();
        traversalMap(hashMap);
        mapTimeTest3.end(
"遍历hashMap耗时");
    }
    
    
private static void setValuesToMap(Map<String,String> map,int max){
        
for(int i=0;i<max;i++){
            String str
=String.valueOf(i);
            map.put(str, str);
        }
    }
    
    
private static void getValuesFromMap(Map<String,String> map,int max){
        
for(int i=0;i<max;i++){
            String str
=map.get(i);
        }
    }
    
    
private static void traversalMap(Map<String,String> map){
        Iterator it
=map.keySet().iterator();
        
        
while(it.hasNext()){
            String key
=(String)it.next();
            String value
=map.get(key);
        }
    }
}

package com.sitinspring;

import java.util.Calendar;
import java.util.GregorianCalendar;

public class TimeTest {
    
private Calendar startTime;

    
public TimeTest() {
        startTime 
= new GregorianCalendar();
    }

    
public void end(String functionName) {

        Calendar endTime 
= new GregorianCalendar();

        
int miniteSpan = endTime.get(Calendar.MINUTE)
                
- startTime.get(Calendar.MINUTE);
        
int secondSpan = endTime.get(Calendar.SECOND)
                
- startTime.get(Calendar.SECOND);
        
int msecondSpan = endTime.get(Calendar.MILLISECOND)
                
- startTime.get(Calendar.MILLISECOND);

        System.out.println(functionName 
+ " " + String.valueOf(miniteSpan)
                
+ " 分 " + String.valueOf(secondSpan) + " 秒 "
                
+ String.valueOf(msecondSpan) + " 毫秒 ");
    }
}

代码下载:
http://www.blogjava.net/Files/sitinspring/HashMapHashtable20071215212107.rar

posted on 2008-02-22 10:28 和风细雨 阅读(1009) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: