Collection
鈹淟ist
鈹傗敎LinkedList
鈹傗敎ArrayList
鈹傗敂Vector
鈹傘鈹擲tack
鈹擲et
Map
鈹淗ashtable
鈹淗ashMap
鈹擶eakHashMap
Collection鎺ュ彛
銆銆Collection鏄渶鍩烘湰鐨勯泦鍚堟帴鍙o紝涓涓狢ollection浠h〃涓緇凮bject錛屽嵆Collection鐨勫厓绱狅紙Elements錛夈備竴浜汣ollection鍏佽鐩稿悓鐨勫厓绱犺屽彟涓浜涗笉琛屻備竴浜涜兘鎺掑簭鑰屽彟涓浜涗笉琛屻侸ava SDK涓嶆彁渚涚洿鎺ョ戶鎵胯嚜Collection鐨勭被錛孞ava SDK鎻愪緵鐨勭被閮芥槸緇ф壙鑷狢ollection鐨勨滃瓙鎺ュ彛鈥濆List鍜孲et銆?
銆銆鎵鏈夊疄鐜癈ollection鎺ュ彛鐨勭被閮藉繀欏繪彁渚涗袱涓爣鍑嗙殑鏋勯犲嚱鏁幫細鏃犲弬鏁扮殑鏋勯犲嚱鏁扮敤浜庡垱寤轟竴涓┖鐨凜ollection錛屾湁涓涓狢ollection鍙傛暟鐨勬瀯閫犲嚱鏁扮敤浜庡垱寤轟竴涓柊鐨凜ollection錛岃繖涓柊鐨凜ollection涓庝紶鍏ョ殑Collection鏈夌浉鍚岀殑鍏冪礌銆傚悗涓涓瀯閫犲嚱鏁板厑璁哥敤鎴峰鍒朵竴涓狢ollection銆?
銆銆濡備綍閬嶅巻Collection涓殑姣忎竴涓厓绱狅紵涓嶈Collection鐨勫疄闄呯被鍨嬪浣曪紝瀹冮兘鏀寔涓涓猧terator()鐨勬柟娉曪紝璇ユ柟娉曡繑鍥炰竴涓凱浠e瓙錛屼嬌鐢ㄨ榪唬瀛愬嵆鍙愪竴璁塊棶Collection涓瘡涓涓厓绱犮傚吀鍨嬬殑鐢ㄦ硶濡備笅錛?
銆銆銆銆Iterator it = collection.iterator(); // 鑾峰緱涓涓凱浠e瓙
銆銆銆銆while(it.hasNext()) {
銆銆銆銆銆銆Object obj = it.next(); // 寰楀埌涓嬩竴涓厓绱?
銆銆銆銆}
銆銆鐢盋ollection鎺ュ彛媧劇敓鐨勪袱涓帴鍙f槸List鍜孲et銆?
List鎺ュ彛
銆銆List鏄湁搴忕殑Collection錛屼嬌鐢ㄦ鎺ュ彛鑳藉綺劇‘鐨勬帶鍒舵瘡涓厓绱犳彃鍏ョ殑浣嶇疆銆傜敤鎴瘋兘澶熶嬌鐢ㄧ儲寮曪紙鍏冪礌鍦↙ist涓殑浣嶇疆錛岀被浼間簬鏁扮粍涓嬫爣錛夋潵璁塊棶List涓殑鍏冪礌錛岃繖綾諱技浜嶫ava鐨勬暟緇勩?
鍜屼笅闈㈣鎻愬埌鐨凷et涓嶅悓錛孡ist鍏佽鏈夌浉鍚岀殑鍏冪礌銆?
銆銆闄や簡鍏鋒湁Collection鎺ュ彛蹇呭鐨刬terator()鏂規硶澶栵紝List榪樻彁渚涗竴涓猯istIterator()鏂規硶錛岃繑鍥炰竴涓狶istIterator鎺ュ彛錛屽拰鏍囧噯鐨処terator鎺ュ彛鐩告瘮錛孡istIterator澶氫簡涓浜沘dd()涔嬬被鐨勬柟娉曪紝鍏佽娣誨姞錛屽垹闄わ紝璁懼畾鍏冪礌錛岃繕鑳藉悜鍓嶆垨鍚戝悗閬嶅巻銆?
銆銆瀹炵幇List鎺ュ彛鐨勫父鐢ㄧ被鏈塋inkedList錛孉rrayList錛孷ector鍜孲tack銆?
LinkedList綾?
銆銆LinkedList瀹炵幇浜哃ist鎺ュ彛錛屽厑璁竛ull鍏冪礌銆傛澶朙inkedList鎻愪緵棰濆鐨刧et錛宺emove錛宨nsert鏂規硶鍦↙inkedList鐨勯閮ㄦ垨灝鵑儴銆傝繖浜涙搷浣滀嬌LinkedList鍙鐢ㄤ綔鍫嗘爤錛坰tack錛夛紝闃熷垪錛坬ueue錛夋垨鍙屽悜闃熷垪錛坉eque錛夈?
銆銆娉ㄦ剰LinkedList娌℃湁鍚屾鏂規硶銆傚鏋滃涓嚎紼嬪悓鏃惰闂竴涓狶ist錛屽垯蹇呴』鑷繁瀹炵幇璁塊棶鍚屾銆備竴縐嶈В鍐蟲柟娉曟槸鍦ㄥ垱寤篖ist鏃舵瀯閫犱竴涓悓姝ョ殑List錛?
銆銆銆銆List list = Collections.synchronizedList(new LinkedList(...));
ArrayList綾?
銆銆ArrayList瀹炵幇浜嗗彲鍙樺ぇ灝忕殑鏁扮粍銆傚畠鍏佽鎵鏈夊厓绱狅紝鍖呮嫭null銆侫rrayList娌℃湁鍚屾銆?
size錛宨sEmpty錛実et錛宻et鏂規硶榪愯鏃墮棿涓哄父鏁般備絾鏄痑dd鏂規硶寮閿涓哄垎鎽婄殑甯告暟錛屾坊鍔爊涓厓绱犻渶瑕丱(n)鐨勬椂闂淬傚叾浠栫殑鏂規硶榪愯鏃墮棿涓虹嚎鎬с?
銆銆姣忎釜ArrayList瀹炰緥閮芥湁涓涓閲忥紙Capacity錛夛紝鍗崇敤浜庡瓨鍌ㄥ厓绱犵殑鏁扮粍鐨勫ぇ灝忋傝繖涓閲忓彲闅忕潃涓嶆柇娣誨姞鏂板厓绱犺岃嚜鍔ㄥ鍔狅紝浣嗘槸澧為暱綆楁硶騫舵病鏈夊畾涔夈傚綋闇瑕佹彃鍏ュぇ閲忓厓绱犳椂錛屽湪鎻掑叆鍓嶅彲浠ヨ皟鐢╡nsureCapacity鏂規硶鏉ュ鍔燗rrayList鐨勫閲忎互鎻愰珮鎻掑叆鏁堢巼銆?
銆銆鍜孡inkedList涓鏍鳳紝ArrayList涔熸槸闈炲悓姝ョ殑錛坲nsynchronized錛夈?
Vector綾?
銆銆Vector闈炲父綾諱技ArrayList錛屼絾鏄疺ector鏄悓姝ョ殑銆傜敱Vector鍒涘緩鐨処terator錛岃櫧鐒跺拰ArrayList鍒涘緩鐨処terator鏄悓涓鎺ュ彛錛屼絾鏄紝鍥犱負Vector鏄悓姝ョ殑錛屽綋涓涓狪terator琚垱寤鴻屼笖姝e湪琚嬌鐢紝鍙︿竴涓嚎紼嬫敼鍙樹簡Vector鐨勭姸鎬侊紙渚嬪錛屾坊鍔犳垨鍒犻櫎浜嗕竴浜涘厓绱狅級錛岃繖鏃惰皟鐢↖terator鐨勬柟娉曟椂灝嗘姏鍑篊oncurrentModificationException錛屽洜姝ゅ繀欏繪崟鑾瘋寮傚父銆?
Stack 綾?
銆銆Stack緇ф壙鑷猇ector錛屽疄鐜頒竴涓悗榪涘厛鍑虹殑鍫嗘爤銆係tack鎻愪緵5涓澶栫殑鏂規硶浣垮緱Vector寰椾互琚綋浣滃爢鏍堜嬌鐢ㄣ傚熀鏈殑push鍜宲op鏂規硶錛岃繕鏈塸eek鏂規硶寰楀埌鏍堥《鐨勫厓绱狅紝empty鏂規硶嫻嬭瘯鍫嗘爤鏄惁涓虹┖錛宻earch鏂規硶媯嫻嬩竴涓厓绱犲湪鍫嗘爤涓殑浣嶇疆銆係tack鍒氬垱寤哄悗鏄┖鏍堛?
Set鎺ュ彛
銆銆Set鏄竴縐嶄笉鍖呭惈閲嶅鐨勫厓绱犵殑Collection錛屽嵆浠繪剰鐨勪袱涓厓绱爀1鍜宔2閮芥湁e1.equals(e2)=false錛孲et鏈澶氭湁涓涓猲ull鍏冪礌銆?
銆銆寰堟槑鏄撅紝Set鐨勬瀯閫犲嚱鏁版湁涓涓害鏉熸潯浠訛紝浼犲叆鐨凜ollection鍙傛暟涓嶈兘鍖呭惈閲嶅鐨勫厓绱犮?
銆銆璇鋒敞鎰忥細蹇呴』灝忓績鎿嶄綔鍙彉瀵硅薄錛圡utable Object錛夈傚鏋滀竴涓猄et涓殑鍙彉鍏冪礌鏀瑰彉浜嗚嚜韜姸鎬佸鑷碠bject.equals(Object)=true灝嗗鑷翠竴浜涢棶棰樸?
Map鎺ュ彛
銆銆璇鋒敞鎰忥紝Map娌℃湁緇ф壙Collection鎺ュ彛錛孧ap鎻愪緵key鍒皏alue鐨勬槧灝勩備竴涓狹ap涓笉鑳藉寘鍚浉鍚岀殑key錛屾瘡涓猭ey鍙兘鏄犲皠涓涓獀alue銆侻ap鎺ュ彛鎻愪緵3縐嶉泦鍚堢殑瑙嗗浘錛孧ap鐨勫唴瀹瑰彲浠ヨ褰撲綔涓緇刱ey闆嗗悎錛屼竴緇剉alue闆嗗悎錛屾垨鑰呬竴緇刱ey-value鏄犲皠銆?
Hashtable綾匯銆Hashtable緇ф壙Map鎺ュ彛錛屽疄鐜頒竴涓猭ey-value鏄犲皠鐨勫搱甯岃〃銆備換浣曢潪絀猴紙non-null錛夌殑瀵硅薄閮藉彲浣滀負key鎴栬卾alue銆?
銆銆娣誨姞鏁版嵁浣跨敤put(key, value)錛屽彇鍑烘暟鎹嬌鐢╣et(key)錛岃繖涓や釜鍩烘湰鎿嶄綔鐨勬椂闂村紑閿涓哄父鏁般?
Hashtable閫氳繃initial capacity鍜宭oad factor涓や釜鍙傛暟璋冩暣鎬ц兘銆傞氬父緙虹渷鐨刲oad factor 0.75杈冨ソ鍦板疄鐜頒簡鏃墮棿鍜岀┖闂寸殑鍧囪 銆傚澶oad factor鍙互鑺傜渷絀洪棿浣嗙浉搴旂殑鏌ユ壘鏃墮棿灝嗗澶э紝榪欎細褰卞搷鍍廹et鍜宲ut榪欐牱鐨勬搷浣溿?
浣跨敤Hashtable鐨勭畝鍗曠ず渚嬪涓嬶紝灝?錛?錛?鏀懼埌Hashtable涓紝浠栦滑鐨刱ey鍒嗗埆鏄漮ne鈥濓紝鈥漷wo鈥濓紝鈥漷hree鈥濓細
銆銆銆銆Hashtable numbers = new Hashtable();
銆銆銆銆numbers.put(鈥渙ne鈥? new Integer(1));
銆銆銆銆numbers.put(鈥渢wo鈥? new Integer(2));
銆銆銆銆numbers.put(鈥渢hree鈥? new Integer(3));
銆銆瑕佸彇鍑轟竴涓暟錛屾瘮濡?錛岀敤鐩稿簲鐨刱ey錛?
銆銆銆銆Integer n = (Integer)numbers.get(鈥渢wo鈥?;
銆銆銆銆System.out.println(鈥渢wo = 鈥?+ n);
銆銆鐢變簬浣滀負key鐨勫璞″皢閫氳繃璁$畻鍏舵暎鍒楀嚱鏁版潵紜畾涓庝箣瀵瑰簲鐨剉alue鐨勪綅緗紝鍥犳浠諱綍浣滀負key鐨勫璞¢兘蹇呴』瀹炵幇hashCode鍜宔quals鏂規硶銆俬ashCode鍜宔quals鏂規硶緇ф壙鑷牴綾籓bject錛屽鏋滀綘鐢ㄨ嚜瀹氫箟鐨勭被褰撲綔key鐨勮瘽錛岃鐩稿綋灝忓績錛屾寜鐓ф暎鍒楀嚱鏁扮殑瀹氫箟錛屽鏋滀袱涓璞$浉鍚岋紝鍗硂bj1.equals(obj2)=true錛屽垯瀹冧滑鐨刪ashCode蹇呴』鐩稿悓錛屼絾濡傛灉涓や釜瀵硅薄涓嶅悓錛屽垯瀹冧滑鐨刪ashCode涓嶄竴瀹氫笉鍚岋紝濡傛灉涓や釜涓嶅悓瀵硅薄鐨刪ashCode鐩稿悓錛岃繖縐嶇幇璞$О涓哄啿紿侊紝鍐茬獊浼氬鑷存搷浣滃搱甯岃〃鐨勬椂闂村紑閿澧炲ぇ錛屾墍浠ュ敖閲忓畾涔夊ソ鐨刪ashCode()鏂規硶錛岃兘鍔犲揩鍝堝笇琛ㄧ殑鎿嶄綔銆?
銆銆濡傛灉鐩稿悓鐨勫璞℃湁涓嶅悓鐨刪ashCode錛屽鍝堝笇琛ㄧ殑鎿嶄綔浼氬嚭鐜版剰鎯充笉鍒扮殑緇撴灉錛堟湡寰呯殑get鏂規硶榪斿洖null錛夛紝瑕侀伩鍏嶈繖縐嶉棶棰橈紝鍙渶瑕佺墷璁頒竴鏉★細瑕佸悓鏃跺鍐檈quals鏂規硶鍜宧ashCode鏂規硶錛岃屼笉瑕佸彧鍐欏叾涓竴涓?
銆銆Hashtable鏄悓姝ョ殑銆?
HashMap綾?
銆銆HashMap鍜孒ashtable綾諱技錛屼笉鍚屼箣澶勫湪浜嶩ashMap鏄潪鍚屾鐨勶紝騫朵笖鍏佽null錛屽嵆null value鍜宯ull key銆傦紝浣嗘槸灝咹ashMap瑙嗕負Collection鏃訛紙values()鏂規硶鍙繑鍥濩ollection錛夛紝鍏惰凱浠e瓙鎿嶄綔鏃墮棿寮閿鍜孒ashMap鐨勫閲忔垚姣斾緥銆傚洜姝わ紝濡傛灉榪唬鎿嶄綔鐨勬ц兘鐩稿綋閲嶈鐨勮瘽錛屼笉瑕佸皢HashMap鐨勫垵濮嬪寲瀹歸噺璁懼緱榪囬珮錛屾垨鑰卨oad factor榪囦綆銆?
WeakHashMap綾?
銆銆WeakHashMap鏄竴縐嶆敼榪涚殑HashMap錛屽畠瀵筴ey瀹炶鈥滃急寮曠敤鈥濓紝濡傛灉涓涓猭ey涓嶅啀琚閮ㄦ墍寮曠敤錛岄偅涔堣key鍙互琚獹C鍥炴敹銆?
鎬葷粨
銆銆濡傛灉娑夊強鍒板爢鏍堬紝闃熷垪絳夋搷浣滐紝搴旇鑰冭檻鐢↙ist錛屽浜庨渶瑕佸揩閫熸彃鍏ワ紝鍒犻櫎鍏冪礌錛屽簲璇ヤ嬌鐢↙inkedList錛屽鏋滈渶瑕佸揩閫熼殢鏈鴻闂厓绱狅紝搴旇浣跨敤ArrayList銆?
銆銆濡傛灉紼嬪簭鍦ㄥ崟綰跨▼鐜涓紝鎴栬呰闂粎浠呭湪涓涓嚎紼嬩腑榪涜錛岃冭檻闈炲悓姝ョ殑綾伙紝鍏舵晥鐜囪緝楂橈紝濡傛灉澶氫釜綰跨▼鍙兘鍚屾椂鎿嶄綔涓涓被錛屽簲璇ヤ嬌鐢ㄥ悓姝ョ殑綾匯?
銆銆瑕佺壒鍒敞鎰忓鍝堝笇琛ㄧ殑鎿嶄綔錛屼綔涓簁ey鐨勫璞¤姝g‘澶嶅啓equals鍜宧ashCode鏂規硶銆?
銆銆灝介噺榪斿洖鎺ュ彛鑰岄潪瀹為檯鐨勭被鍨嬶紝濡傝繑鍥濴ist鑰岄潪ArrayList錛岃繖鏍峰鏋滀互鍚庨渶瑕佸皢ArrayList鎹㈡垚LinkedList鏃訛紝瀹㈡埛绔唬鐮佷笉鐢ㄦ敼鍙樸傝繖灝辨槸閽堝鎶借薄緙栫▼銆?