Java Hashtable factor 0.75

原创
2012/01/16 11:43
阅读数 591
    今天在看spring源码是,发现有一处代码是这样写的(代码位置3.0.5 package org.springframework.jndi下JndiTemplate 129行):
protected Context createInitialContext() throws NamingException {
		Hashtable icEnv = null;
		Properties env = getEnvironment();
		if (env != null) {
			icEnv = new Hashtable(env.size());
			CollectionUtils.mergePropertiesIntoMap(env, icEnv);
		}
		return new InitialContext(icEnv);
	}

发现初始化一个Hashtable是一个int,然后跟到Hashtable中       

/**
     * Constructs a new, empty hashtable with the specified initial capacity
     * and default load factor (0.75).
     *
     * @param     initialCapacity   the initial capacity of the hashtable.
     * @exception IllegalArgumentException if the initial capacity is less
     *              than zero.
     */
    public Hashtable(int initialCapacity) {
	this(initialCapacity, 0.75f);
    }

看到一个神奇的0.75f的数字,感觉好奇快,继续跟到实现方法:

/**
     * Constructs a new, empty hashtable with the specified initial
     * capacity and the specified load factor.
     *
     * @param      initialCapacity   the initial capacity of the hashtable.
     * @param      loadFactor        the load factor of the hashtable.
     * @exception  IllegalArgumentException  if the initial capacity is less
     *             than zero, or if the load factor is nonpositive.
     */
    public Hashtable(int initialCapacity, float loadFactor) {
	if (initialCapacity < 0)
	    throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        if (loadFactor <= 0 || Float.isNaN(loadFactor))
            throw new IllegalArgumentException("Illegal Load: "+loadFactor);

        if (initialCapacity==0)
            initialCapacity = 1;
	this.loadFactor =     ;
	table = new Entry[initialCapacity];
	threshold = (int)(initialCapacity * loadFactor);
    }

在这里解释一下 Float.isNaN(loadFactor)(哈哈,是因为自己不是很清楚,所以google了一下,把答案记录下来)有一个简单的例子说明了具体答案:

/*
  Java Float isNaN method example
  This example shows how to use isNaN() method of the Float class.
*/
 
public class JavaFloatIsNaNExample {
 
  public static void main(String[] args) {
   
    /*
     boolean isNaN(float f) static method checks whether the agrument
     primitive float value is Not-a-Number value or not.
     It return true if the specified argument is Not-a-Number value, false otherwise.
    */
    float f = (float) Math.sqrt(-10);
    boolean b1 = Float.isNaN(f);
    System.out.println(b1);
 
    /*
     boolean isNaN() instance method checks whether the Float object is
     Not-a-Number value or not.
     It return true if the object is Not-a-Number value, false otherwise.
    */
    Float fObj = new Float(f);
    boolean b2 = fObj.isNaN();
    System.out.println(b2);
  }
}
 
/*
Output would be
true
true
*/

在找答案是还发现一篇详细讲述Hashmap与Hashtable的区别,值得好好看看的。

    找到一个跟我有同样疑问的链接,问题如下:

1> Why is load factor 0.75 for hashtable and not some other value which is varying.Quite strange Please clarify.

"As a general rule, the default load factor (.75) offers a good tradeoff between time and space costs. Higher values decrease the space overhead but increase the lookup cost (reflected in most of the operations of the HashMap class, including get and put)."

I understand that the number was determined by empirical testing rather than by theoretical analysis. (A thorough theoretical analysis would be difficult. However, a load factor of 0.75 combined with a good hash function is probably sufficient to keep hash chains down to 1 or 2 in the vast majority of cases, and that results in fast average lookup times.)

2> Why dont we have load factor for HashMap ?这个问题可能本身就有问题,因为你可以点击链接:实际HashMap是有一个与Hashtbale相同参数的构造函数的。

    这样就解释了为什么是0.75哦~ 呵呵。

   

展开阅读全文
打赏
1
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
1
分享
返回顶部
顶部