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中 html
/** * 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的數字,感受好奇快,繼續跟到實現方法:java
/** * 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了一下,把答案記錄下來)有一個簡單的例子說明了具體答案:web
/* 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的區別,值得好好看看的。spring
找到一個跟我有一樣疑問的連接,問題以下:api
1> Why is load factor 0.75 for hashtable and not some other value which is varying.Quite strange Please clarify.oracle
"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)."less
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相同參數的構造函數的。ui
這樣就解釋了爲何是0.75哦~ 呵呵。
this