今天就跟大家聊聊有關(guān)Java 中怎么循環(huán)遍歷HashMap,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
成都創(chuàng)新互聯(lián)公司自2013年創(chuàng)立以來,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元石林做網(wǎng)站,已為上家服務(wù),為石林各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18980820575
HashMap的三種遍歷方式
(1)for each map.entrySet()
Map<String, String> map = new HashMap<String, String>(); for (Entry<String, String> entry : map.entrySet()) { entry.getKey(); entry.getValue(); }
(2)顯示調(diào)用map.entrySet()的集合迭代器
Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator(); while (iterator.hasNext()) { entry.getKey(); entry.getValue(); }
(3)for each map.keySet(),再調(diào)用get獲取
Map<String, String> map = new HashMap<String, String>(); for (String key : map.keySet()) { map.get(key); }
三種遍歷方式的性能測(cè)試及對(duì)比
測(cè)試環(huán)境:Windows7 32位系統(tǒng) 3.2G雙核CPU 4G內(nèi)存,Java 7,Eclipse -Xms512m -Xmx512m
測(cè)試結(jié)果:
map size | 10,000 | 100,000 | 1,000,000 | 2,000,000 |
for each entrySet | 2ms | 6ms | 36ms | 91ms |
for iterator entrySet | 0ms | 4ms | 35ms | 89ms |
for each keySet | 1ms | 6ms | 48ms | 126ms |
遍歷方式結(jié)果分析
由上表可知:
for each entrySet與for iterator entrySet性能等價(jià)
for each keySet由于要再調(diào)用get(key)獲取值,比較耗時(shí)(若hash散列算法較差,會(huì)更加耗時(shí))
在循環(huán)過程中若要對(duì)map進(jìn)行刪除操作,只能用for iterator entrySet(在HahsMap非線程安全里介紹)。
HashMap entrySet源碼
private final class EntryIterator extends HashIterator<Map.Entry<K,V>> { public Map.Entry<K,V> next() { return nextEntry(); } }
HashMap keySet源碼
private final class KeyIterator extends HashIterator<K> { public K next() { return nextEntry().getKey(); } }
由源碼可知:
keySet()與entrySet()都是返回set的迭代器。父類相同,只是返回值不同,因此性能差不多。只是keySet()多了一步根據(jù)key get value的操作而已。get的時(shí)間復(fù)雜度取決于for循環(huán)的次數(shù),即hash算法。
public V get(Object key) { if (key == null) return getForNullKey(); Entry<K,V> entry = getEntry(key); return null == entry ? null : entry.getValue(); } /** 1. Returns the entry associated with the specified key in the 2. HashMap. Returns null if the HashMap contains no mapping 3. for the key. */ final Entry<K,V> getEntry(Object key) { int hash = (key == null) ? 0 : hash(key); for (Entry<K,V> e = table[indexFor(hash, table.length)]; e != null; e = e.next) { Object k; if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) return e; } return null; }
結(jié)論
循環(huán)中需要key、value,但不對(duì)map進(jìn)行刪除操作,使用for each entrySet
循環(huán)中需要key、value,且要對(duì)map進(jìn)行刪除操作,使用for iterator entrySet
循環(huán)中只需要key,使用for each keySet
看完上述內(nèi)容,你們對(duì)Java 中怎么循環(huán)遍歷HashMap有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
當(dāng)前題目:Java中怎么循環(huán)遍歷HashMap
本文鏈接:http://jinyejixie.com/article34/podpse.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、移動(dòng)網(wǎng)站建設(shè)、網(wǎng)站排名、動(dòng)態(tài)網(wǎng)站、網(wǎng)站制作、用戶體驗(yàn)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)