weakhashmapåhashmapçåºå«
ä¸.HashMap
1.HashMapæ¯åºäºKey-Valueçæ£å表(JDK7:æ°ç»+é¾è¡¨,源码小型bbs源码JDK8:æ°ç»+é¾è¡¨+红é»æ )ï¼éç¨æé¾æ³å®ç°çãä¸è¬ç¨äºå线ç¨å½ä¸ï¼é线ç¨å®å ¨ï¼HashMapçé®æ¯"强é®"ã
2.继æ¿äºæ½è±¡ç±»AbstractMapï¼å¹¶ä¸å®ç°Mapæ¥å£ãéåæ¶ï¼åå¾çæ°æ®å®å ¨æ¯éæºçã
3.é»è®¤å®¹é大å°æ¯ï¼å è½½å åæ¯0.ã
4.æå¤åªå 许ä¸æ¡key为Nullï¼å 许å¤æ¡value为Nullã
5.HashMapå®ç°äºCloneableåSerializableæ¥å£ï¼èWeakHashMap没æã
1).HashMapå®ç°Cloneableï¼è¯´æå®è½éè¿clone()å éèªå·±ã
2).HashMapå®ç°Serializableï¼è¯´æå®æ¯æåºååï¼è½éè¿åºååå»ä¼ è¾ã
6.æ·»å ãå é¤æä½æ¶é´å¤æ度é½æ¯O(1)ã
äº.weakHashMap
1.weakHashMapæ¯åºäºKey-Valueçæ£å表(æ°ç»+é¾è¡¨)ï¼éç¨æé¾æ³å®ç°çãä¸è¬ç¨äºå线ç¨å½ä¸ï¼é线ç¨å®å ¨ï¼weakHashMapä¸çé®æ¯"å¼±é®"ã
å¤æ³¨ï¼å½"å¼±é®"被GCä¼æ¶æ¶ï¼å®å¯¹åºçé®å¼ä¹ä¼ä»weakHashMapä¸å é¤ã
2.继æ¿äºæ½è±¡ç±»AbstractMapï¼å¹¶ä¸å®ç°Mapæ¥å£ã
3.é»è®¤å®¹é大å°æ¯ï¼å è½½å åæ¯0.ã
4.æå¤åªå 许ä¸æ¡key为Nullï¼å 许å¤æ¡value为Nullã
hashTable å hashMap ä½ç¼åï¼å®ç°ç两ç§åä¾çåºå«
HashtableåHashMapçåºå«ï¼1.Hashtableæ¯Dictionaryçåç±»ï¼HashMapæ¯Mapæ¥å£çä¸ä¸ªå®ç°ç±»ï¼2.Hashtableä¸çæ¹æ³æ¯åæ¥çï¼èHashMapä¸çæ¹æ³å¨ç¼ºçæ åµä¸æ¯éåæ¥çãå³æ¯è¯´ï¼å¨å¤çº¿ç¨åºç¨ç¨åºä¸ï¼ä¸ç¨ä¸é¨çæä½å°±å®å ¨å°å¯ä»¥ä½¿ç¨Hashtableäºï¼è对äºHashMapï¼åéè¦é¢å¤çåæ¥æºå¶ãä½HashMapçåæ¥é®é¢å¯éè¿Collectionsçä¸ä¸ªéææ¹æ³å¾å°è§£å³ï¼MapCollections.synchronizedMap(Mapm)è¿ä¸ªæ¹æ³è¿åä¸ä¸ªåæ¥çMapï¼è¿ä¸ªMapå°è£ äºåºå±çHashMapçæææ¹æ³ï¼ä½¿å¾åºå±çHashMapå³ä½¿æ¯å¨å¤çº¿ç¨çç¯å¢ä¸ä¹æ¯å®å ¨çã3.å¨HashMapä¸ï¼nullå¯ä»¥ä½ä¸ºé®ï¼è¿æ ·çé®åªæä¸ä¸ªï¼å¯ä»¥æä¸ä¸ªæå¤ä¸ªé®æ对åºçå¼ä¸ºnullãå½get()æ¹æ³è¿ånullå¼æ¶ï¼å³å¯ä»¥è¡¨ç¤ºHashMapä¸æ²¡æ该é®ï¼ä¹å¯ä»¥è¡¨ç¤ºè¯¥é®æ对åºçå¼ä¸ºnullãå æ¤ï¼å¨HashMapä¸ä¸è½ç±get()æ¹æ³æ¥å¤æHashMapä¸æ¯å¦åå¨æ个é®ï¼èåºè¯¥ç¨containsKey()æ¹æ³æ¥å¤æãMapâHashtableâHashMapâWeakHashMapMapæ¥å£ãã请注æï¼Map没æ继æ¿Collectionæ¥å£ï¼Mapæä¾keyå°valueçæ å°ãä¸ä¸ªMapä¸ä¸è½å å«ç¸åçkeyï¼æ¯ä¸ªkeyåªè½æ å°ä¸ä¸ªvalueãMapæ¥å£æä¾3ç§éåçè§å¾ï¼Mapçå 容å¯ä»¥è¢«å½ä½ä¸ç»keyéåï¼ä¸ç»valueéåï¼æè ä¸ç»key-valueæ å°ãHashtableç±»ããHashtable继æ¿Mapæ¥å£ï¼å®ç°ä¸ä¸ªkey-valueæ å°çåå¸è¡¨ãä»»ä½é空ï¼non-nullï¼ç对象é½å¯ä½ä¸ºkeyæè valueãããæ·»å æ°æ®ä½¿ç¨put(key,value)ï¼ååºæ°æ®ä½¿ç¨get(key)ï¼è¿ä¸¤ä¸ªåºæ¬æä½çæ¶é´å¼é为常æ°ãHashtableéè¿initialcapacityåloadfactor两个åæ°è°æ´æ§è½ãé常缺ççloadfactor0.è¾å¥½å°å®ç°äºæ¶é´å空é´çåè¡¡ãå¢å¤§loadfactorå¯ä»¥èç空é´ä½ç¸åºçæ¥æ¾æ¶é´å°å¢å¤§ï¼è¿ä¼å½±åågetåputè¿æ ·çæä½ã使ç¨Hashtableçç®å示ä¾å¦ä¸ï¼å°1ï¼2ï¼3æ¾å°Hashtableä¸ï¼ä»ä»¬çkeyåå«æ¯âoneâï¼âtwoâï¼âthreeâï¼ããããHashtablenumbers=newHashtable();ããããnumbers.put(âoneâ,newInteger(1));ããããnumbers.put(âtwoâ,newInteger(2));ããããnumbers.put(âthreeâ,newInteger(3));ããè¦ååºä¸ä¸ªæ°ï¼æ¯å¦2ï¼ç¨ç¸åºçkeyï¼ããããIntegern=(Integer)numbers.get(âtwoâ);ããããSystem.out.println(âtwo=â+n);ããç±äºä½ä¸ºkeyç对象å°éè¿è®¡ç®å ¶æ£åå½æ°æ¥ç¡®å®ä¸ä¹å¯¹åºçvalueçä½ç½®ï¼å æ¤ä»»ä½ä½ä¸ºkeyç对象é½å¿ é¡»å®ç°hashCodeåequalsæ¹æ³ãhashCodeåequalsæ¹æ³ç»§æ¿èªæ ¹ç±»Objectï¼å¦æä½ ç¨èªå®ä¹çç±»å½ä½keyçè¯ï¼è¦ç¸å½å°å¿ï¼æç §æ£åå½æ°çå®ä¹ï¼å¦æ两个对象ç¸åï¼å³obj1.equals(obj2)=trueï¼åå®ä»¬çhashCodeå¿ é¡»ç¸åï¼ä½å¦æ两个对象ä¸åï¼åå®ä»¬çhashCodeä¸ä¸å®ä¸åï¼å¦æ两个ä¸å对象çhashCodeç¸åï¼è¿ç§ç°è±¡ç§°ä¸ºå²çªï¼å²çªä¼å¯¼è´æä½åå¸è¡¨çæ¶é´å¼éå¢å¤§ï¼æ以尽éå®ä¹å¥½çhashCode()æ¹æ³ï¼è½å å¿«åå¸è¡¨çæä½ãããå¦æç¸åç对象æä¸åçhashCodeï¼å¯¹åå¸è¡¨çæä½ä¼åºç°ææ³ä¸å°çç»æï¼æå¾ çgetæ¹æ³è¿ånullï¼ï¼è¦é¿å è¿ç§é®é¢ï¼åªéè¦ç¢è®°ä¸æ¡ï¼è¦åæ¶å¤åequalsæ¹æ³åhashCodeæ¹æ³ï¼èä¸è¦åªåå ¶ä¸ä¸ä¸ªãããHashtableæ¯åæ¥çãHashMapç±»ããHashMapåHashtable类似ï¼ä¸åä¹å¤å¨äºHashMapæ¯éåæ¥çï¼å¹¶ä¸å 许nullï¼å³nullvalueånullkeyãï¼ä½æ¯å°HashMapè§ä¸ºCollectionæ¶ï¼values()æ¹æ³å¯è¿åCollectionï¼ï¼å ¶è¿ä»£åæä½æ¶é´å¼éåHashMapç容éææ¯ä¾ãå æ¤ï¼å¦æè¿ä»£æä½çæ§è½ç¸å½éè¦çè¯ï¼ä¸è¦å°HashMapçåå§å容é设å¾è¿é«ï¼æè loadfactorè¿ä½ãWeakHashMapç±»ããWeakHashMapæ¯ä¸ç§æ¹è¿çHashMapï¼å®å¯¹keyå®è¡âå¼±å¼ç¨âï¼å¦æä¸ä¸ªkeyä¸å被å¤é¨æå¼ç¨ï¼é£ä¹è¯¥keyå¯ä»¥è¢«GCåæ¶ã
2024-12-24 00:03
2024-12-23 23:47
2024-12-23 22:59
2024-12-23 22:44
2024-12-23 21:46