婧愮爜鍒嗘瀽涔?--HashMap鏌ユ壘銆佸垹闄ゃ€侀亶鍘?/h1> Posted 绋嬪簭鐚垮垎浜祫婧愭睜
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了婧愮爜鍒嗘瀽涔?--HashMap鏌ユ壘銆佸垹闄ゃ€侀亶鍘?/h1>
相关的知识,希望对你有一定的参考价值。
鎸佺画鍧氭寔鍘熷垱杈撳嚭锛岀偣鍑昏摑瀛楀叧娉ㄦ垜鍚?/span>
鉂?/span>
娌夋穩銆佸垎浜€佹垚闀匡紝璁╄嚜宸卞拰浠栦汉閮借兘鏈夋墍鏀惰幏锛侌煒?/p>
鉂?/span>
鐩綍
涓€銆佸墠瑷€
浜屻€丠ashMap婧愮爜鍒嗘瀽
1. 鎻掑叆
2. 鏌ユ壘
3. 鍒犻櫎
4. 閬嶅巻
涓夈€佹€荤粨
涓€銆佸墠瑷€
鍦ㄤ笂涓€绔犺妭鎴戜滑璁茶В骞剁敤鏁版嵁楠岃瘉浜嗭紝HashMap涓殑锛?code class="mq-25">鏁e垪琛ㄧ殑瀹炵幇銆?code class="mq-26">鎵板姩鍑芥暟銆?code class="mq-27">璐熻浇鍥犲瓙浠ュ強鎵╁鎷嗗垎
绛夋牳蹇冪煡璇嗙偣浠ュ強鐩稿簲鐨勪綔鐢ㄣ€?/p>
闄や簡浠ヤ笂杩欎簺鐭ヨ瘑鐐瑰锛孒ashMap杩樻湁鍩烘湰鐨勬暟鎹姛鑳斤紱瀛樺偍
銆?code class="mq-31">鍒犻櫎銆?code class="mq-32">鑾峰彇銆?code class="mq-33">閬嶅巻锛屽湪杩欎簺鍔熻兘涓粡甯镐細鍚埌閾捐〃銆佺孩榛戞爲銆佷箣闂磋浆鎹㈢瓑鍔熻兘銆傝€岀孩榛戞爲鏄湪jdk1.8寮曞叆鍒癏ashMap涓В鍐抽摼琛ㄨ繃闀块棶棰樼殑锛岀畝鍗曡褰撻摼琛ㄩ暱搴?code class="mq-34">>=8鏃讹紝灏嗛摼琛ㄨ浆鎹负绾㈤粦鏍?褰撶劧杩欓噷杩樻湁涓€涓墿瀹圭殑鐭ヨ瘑鐐癸紝涓嶄竴瀹氶兘浼氭爲鍖朳MIN_TREEIFY_CAPACITY])銆?/p>
閭d箞鏈珷鑺備細杩涜璁茶В浠ヤ笅鐭ヨ瘑鐐癸紱
-
鏁版嵁鎻掑叆娴佺▼鍜屾簮鐮佸垎鏋?
-
閾捐〃鏍戝寲浠ュ強鏍戣浆閾捐〃
-
閬嶅巻杩囩▼涓殑鏃犲簭Set鐨勬牳蹇冪煡璇?
銆岎煏垫敞鎰忥細銆?/strong> 寤鸿闃呰涓婁竴绡囧悗锛屽啀闃呰鏈瘒鏂囩珷
浜屻€丠ashMap婧愮爜鍒嗘瀽
1. 鏌ユ壘
涓婂浘灏辨槸HashMap鏌ユ壘鐨勪竴涓祦绋嬪浘锛岃繕鏄瘮杈冪畝鍗曠殑锛屽悓鏃朵篃鏄珮鏁堢殑銆?/p>
鎺ヤ笅鏉ユ垜浠湪缁撳悎浠g爜锛屾潵鍒嗘瀽杩欐娴佺▼锛屽涓嬶紱
public V get(Object key) {
Node<K,V> e;
// 鍚屾牱闇€瑕佺粡杩囨壈鍔ㄥ嚱鏁拌绠楀搱甯屽€?/span>
return (e = getNode(hash(key), key)) == null ? null : e.value;
}
final Node<K,V> getNode(int hash, Object key) {
Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
// 鍒ゆ柇妗舵暟缁勭殑鏄惁涓虹┖鍜岄暱搴﹀€?/span>
if ((tab = table) != null && (n = tab.length) > 0 &&
// 璁$畻涓嬫爣锛屽搱甯屽€间笌鏁扮粍闀垮害-1
(first = tab[(n - 1) & hash]) != null) {
if (first.hash == hash && // always check first node
((k = first.key) == key || (key != null && key.equals(k))))
return first;
if ((e = first.next) != null) {
// TreeNode 鑺傜偣鐩存帴璋冪敤绾㈤粦鏍戠殑鏌ユ壘鏂规硶锛屾椂闂村鏉傚害O(logn)
if (first instanceof TreeNode)
return ((TreeNode<K,V>)first).getTreeNode(hash, key);
// 濡傛灉鏄摼琛ㄥ氨渚濇閬嶅巻鏌ユ壘
do {
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e;
} while ((e = e.next) != null);
}
}
return null;
}
浠ヤ笂鏌ユ壘鐨勪唬鐮佽繕鏄瘮杈冪畝鍗曠殑锛屼富瑕佸寘鎷互涓嬬煡璇嗙偣锛?/p>
-
鎵板姩鍑芥暟鐨勪娇鐢紝鑾峰彇鏂扮殑鍝堝笇鍊硷紝杩欏湪涓婁竴绔犺妭宸茬粡璁茶繃
-
涓嬫爣鐨勮绠楋紝鍚屾牱涔熶粙缁嶈繃
tab[(n - 1) & hash])
-
纭畾浜嗘《鏁扮粍涓嬫爣浣嶇疆锛屾帴涓嬫潵灏辨槸瀵圭孩榛戞爲鍜岄摼琛ㄨ繘琛屾煡鎵惧拰閬嶅巻鎿嶄綔浜?
3. 鍒犻櫎
public V remove(Object key) {
Node<K,V> e;
return (e = removeNode(hash(key), key, null, false, true)) == null ?
null : e.value;
}
final Node<K,V> removeNode(int hash, Object key, Object value,
boolean matchValue, boolean movable) {
Node<K,V>[] tab; Node<K,V> p; int n, index;
// 瀹氫綅妗舵暟缁勪腑鐨勪笅鏍囦綅缃紝index = (n - 1) & hash
if ((tab = table) != null && (n = tab.length) > 0 &&
(p = tab[index = (n - 1) & hash]) != null) {
Node<K,V> node = null, e; K k; V v;
// 濡傛灉閿殑鍊间笌閾捐〃绗竴涓妭鐐圭浉绛夛紝鍒欏皢 node 鎸囧悜璇ヨ妭鐐?/span>
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
node = p;
else if ((e = p.next) != null) {
// 鏍戣妭鐐癸紝璋冪敤绾㈤粦鏍戠殑鏌ユ壘鏂规硶锛屽畾浣嶈妭鐐广€?/span>
if (p instanceof TreeNode)
node = ((TreeNode<K,V>)p).getTreeNode(hash, key);
else {
// 閬嶅巻閾捐〃锛屾壘鍒板緟鍒犻櫎鑺傜偣
do {
if (e.hash == hash &&
((k = e.key) == key ||
(key != null && key.equals(k)))) {
node = e;
break;
}
p = e;
} while ((e = e.next) != null);
}
}
// 鍒犻櫎鑺傜偣锛屼互鍙婄孩榛戞爲闇€瑕佷慨澶嶏紝鍥犱负鍒犻櫎鍚庝細鐮村潖骞宠 鎬с€傞摼琛ㄧ殑鍒犻櫎鏇村姞绠€鍗曘€?/span>
if (node != null && (!matchValue || (v = node.value) == value ||
(value != null && value.equals(v)))) {
if (node instanceof TreeNode)
((TreeNode<K,V>)node).removeTreeNode(this, tab, movable);
else if (node == p)
tab[index] = node.next;
else
p.next = node.next;
++modCount;
--size;
afterNodeRemoval(node);
return node;
}
}
return null;
}
-
鍒犻櫎鐨勬搷浣滀篃姣旇緝绠€鍗曪紝杩欓噷闈㈤兘娌℃湁澶鐨勫鏉傜殑閫昏緫銆?
-
鍙﹀绾㈤粦鏍戠殑鎿嶄綔鍥犱负琚寘瑁呬簡锛屽彧鐪嬩娇鐢ㄤ笂涔熸槸寰堝鏄撱€?
4. 閬嶅巻
4.1 闂鐐?/span>
HashMap涓殑閬嶅巻涔熸槸闈炲父甯哥敤鐨凙PI鏂规硶锛屽寘鎷紱
銆孠eySet銆?/strong>
for (String key : map.keySet()) {
System.out.print(key + " ");
}
銆孍ntrySet銆?/strong>
for (HashMap.Entry entry : map.entrySet()) {
System.out.print(entry + " ");
}
浠庢柟娉曚笂浠ュ強鏃ュ父浣跨敤閮界煡閬擄紝KeySet鏄亶鍘嗘槸鏃犲簭鐨勶紝浣嗘瘡娆′娇鐢ㄤ笉鍚屾柟寮忛亶鍘嗗寘鎷?code class="mq-195">keys.iterator()锛屽畠浠亶鍘嗙殑缁撴灉鏄浐瀹氱殑銆?/p>
閭d箞浠庡疄鐜扮殑瑙掑害鏉ョ湅锛岃繖浜涚閬嶅巻閮芥槸浠庢暎鍒楄〃涓殑閾捐〃鍜岀孩榛戞爲鑾峰彇闆嗗悎鍊硷紝閭d箞浠栦滑鏈変竴涓粈涔堝浐瀹氱殑瑙勫緥鍚楋紵
4.2 鐢ㄤ唬鐮佹祴璇?/span>
娴嬭瘯鐨勫満鏅拰鍓嶆彁锛?/p>
-
杩欓噷鎴戜滑瑕佽瀹氫竴涓棦鏈夌孩榛戞爲鍙堟湁閾捐〃缁撴瀯鐨勬暟鎹満鏅?
-
涓轰簡鍙互鏈夎繖鏍风殑鏁版嵁缁撴瀯锛屾垜浠渶濂芥妸HashMap鐨勫垵濮嬮暱搴﹁瀹氫负64锛岄伩鍏嶅湪閾捐〃瓒呰繃8浣嶅悗鎵╁锛岃€屾槸鐩存帴璁╁叾杞崲涓虹孩榛戞爲銆?
-
鎵惧埌18涓厓绱狅紝鍒嗗埆鏀惧湪涓嶅悓鑺傜偣(杩欎簺鏁版嵁閫氳繃绋嬪簭璁$畻寰楁潵)锛?
-
妗舵暟缁?2鑺傜偣锛?4銆?6銆?8
-
妗舵暟缁?7鑺傜偣锛?9
-
妗舵暟缁?2鑺傜偣锛?50銆?72銆?94銆?71銆?93銆?70銆?92銆?91銆?90
銆屼唬鐮佹祴璇曘€?/strong>
@Test
public void test_Iterator() {
Map<String, String> map = new HashMap<String, String>(64);
map.put("24", "Idx锛?");
map.put("46", "Idx锛?");
map.put("68", "Idx锛?");
map.put("29", "Idx锛?");
map.put("150", "Idx锛?2");
map.put("172", "Idx锛?2");
map.put("194", "Idx锛?2");
map.put("271", "Idx锛?2");
System.out.println("鎺掑簭01锛?);
for (String key : map.keySet()) {
System.out.print(key + " ");
}
map.put("293", "Idx锛?2");
map.put("370", "Idx锛?2");
map.put("392", "Idx锛?2");
map.put("491", "Idx锛?2");
map.put("590", "Idx锛?2");
System.out.println("
鎺掑簭02锛?);
for (String key : map.keySet()) {
System.out.print(key + " ");
}
map.remove("293");
map.remove("370");
map.remove("392");
map.remove("491");
map.remove("590");
System.out.println("
鎺掑簭03锛?);
for (String key : map.keySet()) {
System.out.print(key + " ");
}
}
杩欐浠g爜鍒嗗埆娴嬭瘯浜嗕笁绉嶅満鏅紝濡備笅锛?/p>
-
娣诲姞鍏冪礌锛屽湪HashMap杩樻槸鍙摼琛ㄧ粨鏋勬椂锛岃緭鍑烘祴璇曠粨鏋?1
-
娣诲姞鍏冪礌锛屽湪HashMap杞崲涓虹孩榛戞爲鏃跺€欙紝杈撳嚭娴嬭瘯缁撴灉02
-
鍒犻櫎鍏冪礌锛屽湪HashMap杞崲涓洪摼琛ㄧ粨鏋勬椂锛岃緭鍑烘祴璇曠粨鏋?3
4.3 娴嬭瘯缁撴灉鍒嗘瀽
鎺掑簭01锛?br>24 46 68 29 150 172 194 271
鎺掑簭02锛?br>24 46 68 29 271 150 172 194 293 370 392 491 590
鎺掑簭03锛?br>24 46 68 29 172 271 150 194
Process finished with exit code 0
浠巑ap.keySet()娴嬭瘯缁撴灉鍙互鐪嬪埌锛屽涓嬩俊鎭紱
-
01鎯呭喌涓嬶紝鎺掑簭瀹氫綅鍝堝笇鍊间笅鏍囧拰閾捐〃淇℃伅
-
02鎯呭喌涓嬶紝鍥犱负閾捐〃杞崲涓虹孩榛戞爲锛屾爲鏍逛細绉诲姩鍒版暟缁勫ご閮ㄣ€?
moveRootToFront()鏂规硶
-
03鎯呭喌涓嬶紝鍥犱负鍒犻櫎浜嗛儴鍒嗗厓绱狅紝绾㈤粦鏍戦€€鍖栨垚閾捐〃銆?
以上是关于婧愮爜鍒嗘瀽涔?--HashMap鏌ユ壘銆佸垹闄ゃ€侀亶鍘?/h1>
Posted 绋嬪簭鐚垮垎浜祫婧愭睜
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了婧愮爜鍒嗘瀽涔?--HashMap鏌ユ壘銆佸垹闄ゃ€侀亶鍘?/h1>
鎸佺画鍧氭寔鍘熷垱杈撳嚭锛岀偣鍑昏摑瀛楀叧娉ㄦ垜鍚?/span>
鉂?/span>娌夋穩銆佸垎浜€佹垚闀匡紝璁╄嚜宸卞拰浠栦汉閮借兘鏈夋墍鏀惰幏锛侌煒?/p> 鉂?/span>
鐩綍
涓€銆佸墠瑷€
浜屻€丠ashMap婧愮爜鍒嗘瀽
1. 鎻掑叆
2. 鏌ユ壘
3. 鍒犻櫎
4. 閬嶅巻
涓夈€佹€荤粨
涓€銆佸墠瑷€
鍦ㄤ笂涓€绔犺妭鎴戜滑璁茶В骞剁敤鏁版嵁楠岃瘉浜嗭紝HashMap涓殑锛?code class="mq-25">鏁e垪琛ㄧ殑瀹炵幇銆?code class="mq-26">鎵板姩鍑芥暟銆?code class="mq-27">璐熻浇鍥犲瓙浠ュ強鎵╁鎷嗗垎
绛夋牳蹇冪煡璇嗙偣浠ュ強鐩稿簲鐨勪綔鐢ㄣ€?/p>
闄や簡浠ヤ笂杩欎簺鐭ヨ瘑鐐瑰锛孒ashMap杩樻湁鍩烘湰鐨勬暟鎹姛鑳斤紱瀛樺偍
銆?code class="mq-31">鍒犻櫎銆?code class="mq-32">鑾峰彇銆?code class="mq-33">閬嶅巻锛屽湪杩欎簺鍔熻兘涓粡甯镐細鍚埌閾捐〃銆佺孩榛戞爲銆佷箣闂磋浆鎹㈢瓑鍔熻兘銆傝€岀孩榛戞爲鏄湪jdk1.8寮曞叆鍒癏ashMap涓В鍐抽摼琛ㄨ繃闀块棶棰樼殑锛岀畝鍗曡褰撻摼琛ㄩ暱搴?code class="mq-34">>=8鏃讹紝灏嗛摼琛ㄨ浆鎹负绾㈤粦鏍?褰撶劧杩欓噷杩樻湁涓€涓墿瀹圭殑鐭ヨ瘑鐐癸紝涓嶄竴瀹氶兘浼氭爲鍖朳MIN_TREEIFY_CAPACITY])銆?/p>
閭d箞鏈珷鑺備細杩涜璁茶В浠ヤ笅鐭ヨ瘑鐐癸紱
-
鏁版嵁鎻掑叆娴佺▼鍜屾簮鐮佸垎鏋? -
閾捐〃鏍戝寲浠ュ強鏍戣浆閾捐〃 -
閬嶅巻杩囩▼涓殑鏃犲簭Set鐨勬牳蹇冪煡璇?
銆岎煏垫敞鎰忥細銆?/strong> 寤鸿闃呰涓婁竴绡囧悗锛屽啀闃呰鏈瘒鏂囩珷
浜屻€丠ashMap婧愮爜鍒嗘瀽
1. 鏌ユ壘
涓婂浘灏辨槸HashMap鏌ユ壘鐨勪竴涓祦绋嬪浘锛岃繕鏄瘮杈冪畝鍗曠殑锛屽悓鏃朵篃鏄珮鏁堢殑銆?/p>
鎺ヤ笅鏉ユ垜浠湪缁撳悎浠g爜锛屾潵鍒嗘瀽杩欐娴佺▼锛屽涓嬶紱
public V get(Object key) {
Node<K,V> e;
// 鍚屾牱闇€瑕佺粡杩囨壈鍔ㄥ嚱鏁拌绠楀搱甯屽€?/span>
return (e = getNode(hash(key), key)) == null ? null : e.value;
}
final Node<K,V> getNode(int hash, Object key) {
Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
// 鍒ゆ柇妗舵暟缁勭殑鏄惁涓虹┖鍜岄暱搴﹀€?/span>
if ((tab = table) != null && (n = tab.length) > 0 &&
// 璁$畻涓嬫爣锛屽搱甯屽€间笌鏁扮粍闀垮害-1
(first = tab[(n - 1) & hash]) != null) {
if (first.hash == hash && // always check first node
((k = first.key) == key || (key != null && key.equals(k))))
return first;
if ((e = first.next) != null) {
// TreeNode 鑺傜偣鐩存帴璋冪敤绾㈤粦鏍戠殑鏌ユ壘鏂规硶锛屾椂闂村鏉傚害O(logn)
if (first instanceof TreeNode)
return ((TreeNode<K,V>)first).getTreeNode(hash, key);
// 濡傛灉鏄摼琛ㄥ氨渚濇閬嶅巻鏌ユ壘
do {
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e;
} while ((e = e.next) != null);
}
}
return null;
}
浠ヤ笂鏌ユ壘鐨勪唬鐮佽繕鏄瘮杈冪畝鍗曠殑锛屼富瑕佸寘鎷互涓嬬煡璇嗙偣锛?/p>
-
鎵板姩鍑芥暟鐨勪娇鐢紝鑾峰彇鏂扮殑鍝堝笇鍊硷紝杩欏湪涓婁竴绔犺妭宸茬粡璁茶繃 -
涓嬫爣鐨勮绠楋紝鍚屾牱涔熶粙缁嶈繃 tab[(n - 1) & hash])
-
纭畾浜嗘《鏁扮粍涓嬫爣浣嶇疆锛屾帴涓嬫潵灏辨槸瀵圭孩榛戞爲鍜岄摼琛ㄨ繘琛屾煡鎵惧拰閬嶅巻鎿嶄綔浜?
3. 鍒犻櫎
public V remove(Object key) {
Node<K,V> e;
return (e = removeNode(hash(key), key, null, false, true)) == null ?
null : e.value;
}
final Node<K,V> removeNode(int hash, Object key, Object value,
boolean matchValue, boolean movable) {
Node<K,V>[] tab; Node<K,V> p; int n, index;
// 瀹氫綅妗舵暟缁勪腑鐨勪笅鏍囦綅缃紝index = (n - 1) & hash
if ((tab = table) != null && (n = tab.length) > 0 &&
(p = tab[index = (n - 1) & hash]) != null) {
Node<K,V> node = null, e; K k; V v;
// 濡傛灉閿殑鍊间笌閾捐〃绗竴涓妭鐐圭浉绛夛紝鍒欏皢 node 鎸囧悜璇ヨ妭鐐?/span>
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
node = p;
else if ((e = p.next) != null) {
// 鏍戣妭鐐癸紝璋冪敤绾㈤粦鏍戠殑鏌ユ壘鏂规硶锛屽畾浣嶈妭鐐广€?/span>
if (p instanceof TreeNode)
node = ((TreeNode<K,V>)p).getTreeNode(hash, key);
else {
// 閬嶅巻閾捐〃锛屾壘鍒板緟鍒犻櫎鑺傜偣
do {
if (e.hash == hash &&
((k = e.key) == key ||
(key != null && key.equals(k)))) {
node = e;
break;
}
p = e;
} while ((e = e.next) != null);
}
}
// 鍒犻櫎鑺傜偣锛屼互鍙婄孩榛戞爲闇€瑕佷慨澶嶏紝鍥犱负鍒犻櫎鍚庝細鐮村潖骞宠 鎬с€傞摼琛ㄧ殑鍒犻櫎鏇村姞绠€鍗曘€?/span>
if (node != null && (!matchValue || (v = node.value) == value ||
(value != null && value.equals(v)))) {
if (node instanceof TreeNode)
((TreeNode<K,V>)node).removeTreeNode(this, tab, movable);
else if (node == p)
tab[index] = node.next;
else
p.next = node.next;
++modCount;
--size;
afterNodeRemoval(node);
return node;
}
}
return null;
}
-
鍒犻櫎鐨勬搷浣滀篃姣旇緝绠€鍗曪紝杩欓噷闈㈤兘娌℃湁澶鐨勫鏉傜殑閫昏緫銆? -
鍙﹀绾㈤粦鏍戠殑鎿嶄綔鍥犱负琚寘瑁呬簡锛屽彧鐪嬩娇鐢ㄤ笂涔熸槸寰堝鏄撱€?
4. 閬嶅巻
4.1 闂鐐?/span>
HashMap涓殑閬嶅巻涔熸槸闈炲父甯哥敤鐨凙PI鏂规硶锛屽寘鎷紱
銆孠eySet銆?/strong>
for (String key : map.keySet()) {
System.out.print(key + " ");
}
銆孍ntrySet銆?/strong>
for (HashMap.Entry entry : map.entrySet()) {
System.out.print(entry + " ");
}
浠庢柟娉曚笂浠ュ強鏃ュ父浣跨敤閮界煡閬擄紝KeySet鏄亶鍘嗘槸鏃犲簭鐨勶紝浣嗘瘡娆′娇鐢ㄤ笉鍚屾柟寮忛亶鍘嗗寘鎷?code class="mq-195">keys.iterator()锛屽畠浠亶鍘嗙殑缁撴灉鏄浐瀹氱殑銆?/p>
閭d箞浠庡疄鐜扮殑瑙掑害鏉ョ湅锛岃繖浜涚閬嶅巻閮芥槸浠庢暎鍒楄〃涓殑閾捐〃鍜岀孩榛戞爲鑾峰彇闆嗗悎鍊硷紝閭d箞浠栦滑鏈変竴涓粈涔堝浐瀹氱殑瑙勫緥鍚楋紵
4.2 鐢ㄤ唬鐮佹祴璇?/span>
娴嬭瘯鐨勫満鏅拰鍓嶆彁锛?/p>
-
杩欓噷鎴戜滑瑕佽瀹氫竴涓棦鏈夌孩榛戞爲鍙堟湁閾捐〃缁撴瀯鐨勬暟鎹満鏅? -
涓轰簡鍙互鏈夎繖鏍风殑鏁版嵁缁撴瀯锛屾垜浠渶濂芥妸HashMap鐨勫垵濮嬮暱搴﹁瀹氫负64锛岄伩鍏嶅湪閾捐〃瓒呰繃8浣嶅悗鎵╁锛岃€屾槸鐩存帴璁╁叾杞崲涓虹孩榛戞爲銆? -
鎵惧埌18涓厓绱狅紝鍒嗗埆鏀惧湪涓嶅悓鑺傜偣(杩欎簺鏁版嵁閫氳繃绋嬪簭璁$畻寰楁潵)锛? -
妗舵暟缁?2鑺傜偣锛?4銆?6銆?8 -
妗舵暟缁?7鑺傜偣锛?9 -
妗舵暟缁?2鑺傜偣锛?50銆?72銆?94銆?71銆?93銆?70銆?92銆?91銆?90
銆屼唬鐮佹祴璇曘€?/strong>
@Test
public void test_Iterator() {
Map<String, String> map = new HashMap<String, String>(64);
map.put("24", "Idx锛?");
map.put("46", "Idx锛?");
map.put("68", "Idx锛?");
map.put("29", "Idx锛?");
map.put("150", "Idx锛?2");
map.put("172", "Idx锛?2");
map.put("194", "Idx锛?2");
map.put("271", "Idx锛?2");
System.out.println("鎺掑簭01锛?);
for (String key : map.keySet()) {
System.out.print(key + " ");
}
map.put("293", "Idx锛?2");
map.put("370", "Idx锛?2");
map.put("392", "Idx锛?2");
map.put("491", "Idx锛?2");
map.put("590", "Idx锛?2");
System.out.println("
鎺掑簭02锛?);
for (String key : map.keySet()) {
System.out.print(key + " ");
}
map.remove("293");
map.remove("370");
map.remove("392");
map.remove("491");
map.remove("590");
System.out.println("
鎺掑簭03锛?);
for (String key : map.keySet()) {
System.out.print(key + " ");
}
}
杩欐浠g爜鍒嗗埆娴嬭瘯浜嗕笁绉嶅満鏅紝濡備笅锛?/p>
-
娣诲姞鍏冪礌锛屽湪HashMap杩樻槸鍙摼琛ㄧ粨鏋勬椂锛岃緭鍑烘祴璇曠粨鏋?1 -
娣诲姞鍏冪礌锛屽湪HashMap杞崲涓虹孩榛戞爲鏃跺€欙紝杈撳嚭娴嬭瘯缁撴灉02 -
鍒犻櫎鍏冪礌锛屽湪HashMap杞崲涓洪摼琛ㄧ粨鏋勬椂锛岃緭鍑烘祴璇曠粨鏋?3
4.3 娴嬭瘯缁撴灉鍒嗘瀽
鎺掑簭01锛?br>24 46 68 29 150 172 194 271
鎺掑簭02锛?br>24 46 68 29 271 150 172 194 293 370 392 491 590
鎺掑簭03锛?br>24 46 68 29 172 271 150 194
Process finished with exit code 0
浠巑ap.keySet()娴嬭瘯缁撴灉鍙互鐪嬪埌锛屽涓嬩俊鎭紱
-
01鎯呭喌涓嬶紝鎺掑簭瀹氫綅鍝堝笇鍊间笅鏍囧拰閾捐〃淇℃伅
-
02鎯呭喌涓嬶紝鍥犱负閾捐〃杞崲涓虹孩榛戞爲锛屾爲鏍逛細绉诲姩鍒版暟缁勫ご閮ㄣ€? moveRootToFront()鏂规硶
-
03鎯呭喌涓嬶紝鍥犱负鍒犻櫎浜嗛儴鍒嗗厓绱狅紝绾㈤粦鏍戦€€鍖栨垚閾捐〃銆?
以上是关于婧愮爜鍒嗘瀽涔?--HashMap鏌ユ壘銆佸垹闄ゃ€侀亶鍘?/h1>