1.Javaä¸HashMapåHashtableåHashSetçåºå«
Javaä¸HashMapåHashtableåHashSetçåºå«
hastTableåhashMapçåºå«:
(1)Hashtableæ¯åºäºéæ§çDictionaryç±»çï¼HashMapæ¯Java 1.2å¼è¿çMapæ¥å£çä¸ä¸ªå®ç°ã
(2)è¿ä¸ªä¸åå³æ¯æéè¦çä¸ç¹:Hashtableä¸çæ¹æ³æ¯åæ¥çï¼èHashMapæ¹æ³(å¨ç¼ºçæ åµä¸)æ¯éåæ¥çãå³æ¯è¯´ï¼å¨å¤çº¿ç¨åºç¨ç¨åºä¸ï¼ä¸ç¨ä¸é¨çæä½å°±å®å ¨å°å¯ä»¥ä½¿ç¨Hashtableäºï¼è对äºHashMapï¼åéè¦é¢å¤çåæ¥æºå¶ãä½HashMapçåæ¥é®é¢å¯éè¿Collectionsçä¸ä¸ªéææ¹æ³å¾å°è§£å³ï¼
Map Collections.synchronizedMap(Map m)
è¿ä¸ªæ¹æ³è¿åä¸ä¸ªåæ¥çMapï¼è¿ä¸ªMapå°è£ äºåºå±çHashMapçæææ¹æ³ï¼ä½¿å¾åºå±çHashMapå³ä½¿æ¯å¨å¤çº¿ç¨çç¯å¢ä¸ä¹æ¯å®å ¨çã
(3)åªæHashMapå¯ä»¥è®©ä½ å°ç©ºå¼ä½ä¸ºä¸ä¸ªè¡¨çæ¡ç®çkeyævalueãHashMapä¸åªæä¸æ¡è®°å½å¯ä»¥æ¯ä¸ä¸ªç©ºçkeyï¼ä½ä»»ææ°éçæ¡ç®å¯ä»¥æ¯ç©ºçvalueãè¿å°±æ¯è¯´ï¼å¦æå¨è¡¨ä¸æ²¡æåç°æç´¢é®ï¼æè å¦æåç°äºæç´¢é®ï¼ä½å®æ¯ä¸ä¸ªç©ºçå¼ï¼é£ä¹get()å°è¿ånullãå¦ææå¿ è¦ï¼ç¨containKey()æ¹æ³æ¥åºå«è¿ä¸¤ç§æ åµã
å ¶å®çä¸äºèµæ:
1.HashTableçæ¹æ³æ¯åæ¥çï¼HashMapæªç»åæ¥ï¼æ以å¨å¤çº¿ç¨åºåè¦æå¨åæ¥HashMapè¿ä¸ªåºå«å°±åVectoråArrayListä¸æ ·ãï¼æ主è¦çåºå«ï¼
2.HashTableä¸å 许nullå¼(keyåvalueé½ä¸å¯ä»¥),dnf芒果辅助源码HashMapå 许nullå¼(keyåvalueé½å¯ä»¥ï¼åªå®¹è®¸æä¸ä¸ªnullå¼çkeyï¼å¯ä»¥æå¤ä¸ªnullå¼çvalue)ã
3.HashTableæä¸ä¸ªcontains(Object value)ï¼åè½åcontainsValue(Object value)åè½ä¸æ ·ã
4.HashTable使ç¨Enumerationï¼HashMap使ç¨Iteratorã
以ä¸åªæ¯è¡¨é¢çä¸åï¼å®ä»¬çå®ç°ä¹æå¾å¤§çä¸åã
5.HashTableä¸hashæ°ç»é»è®¤å¤§å°æ¯ï¼å¢å çæ¹å¼æ¯ old*2+1ãHashMapä¸hashæ°ç»çé»è®¤å¤§å°æ¯ï¼èä¸ä¸å®æ¯2çææ°ã
6.åå¸å¼ç使ç¨ä¸åï¼HashTableç´æ¥ä½¿ç¨å¯¹è±¡çhashCodeï¼ä»£ç æ¯è¿æ ·çï¼
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;
èHashMapéæ°è®¡ç®hashå¼ï¼èä¸ç¨ä¸ä»£æ¿æ±æ¨¡ï¼
int hash = hash(k);
int i = indexFor(hash, table.length);
static int hash(Object x) {
ãã int h = x.hashCode();
ãã h += ~(h << 9);
ãã h ^= (h >>> );
ãã h += (h << 4);
ãã h ^= (h >>> );
ãã return h;
}
static int indexFor(int h, int length) {
ãã return h & (length-1);
}
以ä¸åªæ¯ä¸äºæ¯è¾çªåºçåºå«ï¼å½ç¶ä»ä»¬çå®ç°ä¸è¿æ¯æå¾å¤ä¸åçï¼æ¯å¦
HashMap对nullçæä½ã
2024-11-13 15:16
2024-11-13 14:27
2024-11-13 14:03
2024-11-13 13:55
2024-11-13 13:52
2024-11-13 13:43
2024-11-13 13:29
2024-11-13 12:44